(projectRoot: string, relativePath: string)
| 292 | * the path escapes the root by traversal or symlink. |
| 293 | */ |
| 294 | export function resolveProjectFile(projectRoot: string, relativePath: string): string { |
| 295 | if (typeof relativePath !== 'string' || relativePath.trim() === '') { |
| 296 | throw new PathRefusalError('No file path was given.'); |
| 297 | } |
| 298 | if (!isUsableSourcePath(relativePath)) { |
| 299 | throw new PathRefusalError(`Refusing to read an unusable path: ${relativePath}`); |
| 300 | } |
| 301 | |
| 302 | // Sensitive-directory refusal, same list the MCP entry points use. Checked on |
| 303 | // the ROOT rather than the leaf: a root of `/etc` makes every path under it |
| 304 | // sensitive, and a leaf check would have to enumerate the world. |
| 305 | const rootError = validateProjectPath(projectRoot); |
| 306 | if (rootError) throw new PathRefusalError(rootError); |
| 307 | |
| 308 | if (path.isAbsolute(relativePath)) { |
| 309 | throw new PathRefusalError(`Refusing to read an absolute path: ${relativePath}`); |
| 310 | } |
| 311 | |
| 312 | const absolute = validatePathWithinRoot(projectRoot, relativePath); |
| 313 | if (!absolute) { |
| 314 | throw new PathRefusalError(`Refusing to read a path outside the project: ${relativePath}`); |
| 315 | } |
| 316 | return absolute; |
| 317 | } |
no test coverage detected