fix(core): reject traversal host segments to prevent cache path escape - #30
Open
detail-app[bot] wants to merge 1 commit into
Open
detail-app[bot] wants to merge 1 commit into
detail-app[bot] wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Your trial has ended. Reactivate Greptile to resume code reviews.
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.
Detail bug report: View on Detail
Issue for this PR
Type of change
What does this PR do?
The
safeHostvalidator inpackages/core/src/repository.tsaccepted"."and".."as host values, even though the symmetricsafeSegmentvalidator already rejects them. Sincehostbecomes the first path segment incachePath, an input like"../log"produced aReferencewithhost === "..", makingcachePathcomputepath.join(root, "..", "log")— escaping the repos cache root by one level.RepositoryCache.ensurethen operated on that escaped path: recursively deleting a pre-existing non-git directory there and cloning a git repo into it. On a default install"../log"resolves to the app's own$XDG_DATA_HOME/opencode/logdirectory, which is created at startup.The fix makes
safeHostreject"."and".."exactly likesafeSegmentdoes, closing the traversal at the parser boundary (the trust boundary all user input crosses). The same one-line change was applied to the byte-for-byte identical latent duplicate inpackages/opencode/src/util/repository.ts.Regression tests were added in both packages asserting that:
"../log","./log","..:owner/repo",".:owner/repo","git@..:owner/repo") are rejected at parse with the correct typed error.How did you verify your code works?
tsgo --noEmit), and lint (oxlint) all pass for bothpackages/coreandpackages/opencode. No new lint warnings were introduced (8 pre-existingconsistent-returnwarnings on untouched functions are unchanged — confirmed via a baseline comparison with the changes stashed).parseRemote("../log")now throwsInvalidReferenceErrorwhere it previously returnedhost="..", and that all happy-path references (owner/repo,github.com/owner/repo,git@github.com:owner/repo.git,https://github.com/owner/repo.git,gitlab.com/group/repo) produce cache paths contained under the root. Also confirmed deeper traversal forms ("../../etc","git@github.com:../../../etc/passwd") and file references are unaffected.Checklist
Automatic Fixes PRs can be configured here.