Break CodeQL taint flow in /paths/compare so path-injection alerts resolve - #4324
Merged
Conversation
…solve Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Collaborator
📊 Performance Test ResultsComparing cbb78e0 vs trunk app-size
site-editor
site-startup
Results are median values from multiple test runs. Legend: 🟢 Improvement (faster) | 🔴 Regression (slower) | ⚪ No change (<50ms diff) |
sejas
approved these changes
Jul 28, 2026
Comment on lines
90
to
94
| // True when `candidate` resolves to `root` or a descendant. Used to confine | ||
| // untrusted paths to a safe root, guarding against `../` traversal escapes. | ||
| export function isPathWithin( root: string, candidate: string ): boolean { | ||
| const resolvedRoot = path.resolve( root ); | ||
| const resolvedCandidate = path.resolve( candidate ); | ||
| return ( | ||
| resolvedCandidate === resolvedRoot || resolvedCandidate.startsWith( resolvedRoot + path.sep ) | ||
| ); | ||
| return confineToRoot( root, candidate ) !== null; | ||
| } |
Member
There was a problem hiding this comment.
Let's remove isPathWithin since it's not used anywhere in the code. It's only referenced in packages/common/lib/tests/fs-utils.test.ts
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.
Related issues
How AI was used in this PR
Claude Code confirmed via the GitHub API that both alerts were still
openon the latesttrunkscan (which already contains #4267), traced why CodeQL's taint tracker did not treat the previousisPathWithinguard as a barrier, designed the restructured barrier pattern, wrote the helper and tests, and ran lint/typecheck/tests. I reviewed the approach and the diff.Proposed Changes
PR #4267 confined the unauthenticated, loopback-only
POST /api/paths/compareinputs to the sites root, which is a correct runtime mitigation — but the two high-severityjs/path-injectionalerts stayed open after it merged. CodeQL does not recognize the standaloneisPathWithin(...)boolean check as a sanitizer: the untrusted values that actually reachedfs.statSync(viapath.resolveinsidearePathsEqual) were the raw request values, so the taint flowreq.body → path1/path2 → statSyncwas never broken in CodeQL's model.This restructures the endpoint into the resolve → confine-to-root → use-the-returned-value pattern that CodeQL treats as a barrier:
confineToRoot( root, candidate )helper resolves the candidate against the root, checks it is the root or a descendant, and returns the normalized absolute path (ornullwhen it escapes).isPathWithinnow delegates to it./paths/comparehandler passes only the confined, resolved paths intoarePathsEqual— the raw, untrusted request values never reach any filesystem call.No user-visible behavior change for legitimate use: the UI only ever compares real site paths under the sites root, which still compare correctly. The shared
arePathsEqualhelper is intentionally left untouched, since its other callers (CLI, desktop IPC, AI sessions) legitimately compare arbitrary absolute site paths that come from trusted local config.Testing Instructions
npm test -- packages/common/lib/tests/fs-utils.test.ts— newconfineToRoottests cover the root itself, descendants, in-root..normalization,../escapes, the sibling-prefix edge case (studio-sites-othermust not count as insidestudio-sites), and relative-candidate resolution against the root. ExistingisPathWithintests still pass.npm run typecheckpasses across all workspaces.POST /api/paths/comparewith two real site paths under the sites root returns{ equal: true }when they match; a body with a../traversal path returns{ equal: false }.Pre-merge Checklist