MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / validatePathWithinRoot

Function validatePathWithinRoot

src/utils.ts:190–247  ·  view source on GitHub ↗
(
  projectRoot: string,
  filePath: string,
  options?: { allowSymlinkEscape?: boolean; readOnly?: boolean }
)

Source from the content-addressed store, hash-verified

188 * path under a root that does not exist is null.
189 */
190export function validatePathWithinRoot(
191 projectRoot: string,
192 filePath: string,
193 options?: { allowSymlinkEscape?: boolean; readOnly?: boolean }
194): string | null {
195 // 1. Lexical containment — cheap, catches `../` traversal. Applies even on
196 // the indexing read path: a crafted `../` escape is still rejected.
197 const resolved = lexicalPathWithinRoot(projectRoot, filePath);
198 if (resolved === null) {
199 return null;
200 }
201 const normalizedRoot = path.resolve(projectRoot);
202
203 // 2. Symlink-aware containment — resolve symlinks on both sides and re-check,
204 // so an in-repo symlink whose real target escapes the root is rejected.
205 // The indexing read path (allowSymlinkEscape) skips only this rejection so
206 // it stays consistent with the directory walk, which already followed the
207 // in-root symlink to enumerate these files (#935).
208 let realRoot: string;
209 try {
210 realRoot = fs.realpathSync(normalizedRoot);
211 } catch {
212 return null;
213 }
214
215 try {
216 const realResolved = fs.realpathSync(resolved);
217 if (options?.allowSymlinkEscape) {
218 return realResolved;
219 }
220 return isWithinDir(realResolved, realRoot) ? realResolved : null;
221 } catch (err) {
222 // A path under a file (ENOTDIR on POSIX) is a missing leaf too.
223 if (!isMissingPathError(err)) return null;
224 }
225
226 // Nothing to read, and with the root in place the indexing read path takes
227 // whatever ancestor a walk would find, so neither needs one.
228 if (options?.readOnly || options?.allowSymlinkEscape) return resolved;
229
230 // A missing leaf may still sit below an EXISTING symlink. Resolve the
231 // nearest existing ancestor before allowing a write path: otherwise
232 // `project/.codegraph/ui -> /outside` plus a missing `trails/` directory
233 // passes the lexical check and mkdir follows the symlink out of the root.
234 let ancestor = path.dirname(resolved);
235 while (isWithinDir(ancestor, normalizedRoot)) {
236 try {
237 const realAncestor = fs.realpathSync(ancestor);
238 return isWithinDir(realAncestor, realRoot) ? resolved : null;
239 } catch (err) {
240 if (!isMissingPathError(err)) return null;
241 const parent = path.dirname(ancestor);
242 if (parent === ancestor) return null;
243 ancestor = parent;
244 }
245 }
246 return null;
247}

Callers 15

probeMethod · 0.90
indexAllMethod · 0.90
indexFileMethod · 0.90
indexFileWithContentMethod · 0.90
extractNodeCodeMethod · 0.90
pathIsProjectFileFunction · 0.90
whenLabelMethod · 0.90
handleExploreMethod · 0.90
readFileMethod · 0.90
handleFileViewMethod · 0.90
validateAnswerFilesFunction · 0.90

Calls 4

lexicalPathWithinRootFunction · 0.85
isWithinDirFunction · 0.85
isMissingPathErrorFunction · 0.85
resolveMethod · 0.65

Tested by

no test coverage detected