fix(security): resolve symlinks in path validation to block out-of-root reads (#527)#724
Merged
Merged
Conversation
…ot reads (#527) validatePathWithinRoot was purely lexical (path.resolve + startsWith), so an in-repo symlink whose logical path is inside the project root but whose real target escapes it passed validation — and both content-serving read sinks (codegraph_node includeCode, codegraph_explore source) then readFileSync'd it, leaking out-of-root file contents (e.g. ~/.ssh, /etc) to the agent. Add a realpath layer: after the lexical check, resolve symlinks on both the candidate path and the root and re-compare, rejecting anything whose real path escapes the root. An in-root symlink is still allowed (no over-blocking). Comparison is case-insensitive on Windows (NTFS + realpath casing). Not-yet- existing paths (ENOENT) fall back to the lexical result so about-to-be-written files still validate; other resolution errors reject. Removes the dead, never-called isPathWithinRoot / isPathWithinRootReal helpers (the latter a footgun — it returned true on realpath failure). Adds RED->GREEN tests: in->out file/dir symlinks rejected, in->in allowed, ../ rejected, ENOENT allowed, plus an end-to-end test proving getCode no longer serves an out-of-root file reached through a dir symlink. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This was referenced Jun 8, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
validatePathWithinRootwas purely lexical (path.resolve+startsWith), so an in-repo symlink whose logical path is inside the project root but whose real target escapes it passed validation. Both content-serving read sinks (codegraph_nodeincludeCode,codegraph_exploresource) thenreadFileSync'd the returned path and followed the symlink, leaking out-of-root file contents (e.g.~/.ssh,/etc) to the agent.Fix
A realpath layer on
validatePathWithinRoot: after the cheap lexical check, resolve symlinks on both the candidate path and the root and re-compare — rejecting anything whose real path escapes the root, while still allowing symlinks that stay within the root (no over-blocking). Comparison is case-insensitive on Windows (NTFS + realpath casing). Not-yet-existing paths (ENOENT) fall back to the lexical result so about-to-be-written files still validate; other resolution errors reject. Removes the dead, never-calledisPathWithinRoot/isPathWithinRootRealhelpers (the latter a footgun — it returnedtrueon realpath failure).Tests (RED → GREEN)
New tests in
security.test.ts, written first and failing on unfixedmain— the end-to-end case proved the leak (getCodereturned the full out-of-root file body). Coverage: in→out file & dir symlinks rejected, in→in allowed,../rejected, ENOENT allowed, plus the end-to-endgetCodeguarantee.Cross-platform validation
node:22-bookworm,--init)origin/mainbaseline) or a flaky daemon raceAddresses #527.
🤖 Generated with Claude Code