Project Files
src / path / validation.ts
import path from "node:path"
/**
* Resolves a relative repository name to an absolute path and validates that it remains within the working directory.
*
* @param repoName The repository directory name relative to the working directory.
* @param workingDirectory The allowed working directory from LM Studio.
* @returns The absolute path to the repository.
* @throws {Error} If the resolved path escapes the working directory.
*/
export function resolveAndValidateRepoPath(repoName: string, workingDirectory: string): string {
const resolvedWorking = path.resolve(workingDirectory)
const resolvedRepo = path.resolve(resolvedWorking, repoName)
if (!resolvedRepo.startsWith(resolvedWorking)) {
throw new Error(`Repository "${repoName}" resolves outside the allowed working directory "${workingDirectory}".`)
}
return resolvedRepo
}