fix(opencode): prevent cross-drive path bypass in Filesystem.contains on Windows#14581
fix(opencode): prevent cross-drive path bypass in Filesystem.contains on Windows#14581Nicoo01x wants to merge 1 commit intoanomalyco:devfrom
Conversation
… on Windows On Windows, path.relative() across different drive letters returns the absolute child path instead of a ".." prefixed relative path, causing Filesystem.contains() to incorrectly report that a path on a different drive is inside the project directory. Add a drive letter comparison guard before the path.relative() check. Remove resolved TODO comments from file/index.ts. Add Windows-specific tests for cross-drive, same-drive, and case-insensitive drive letter scenarios. Closes anomalyco#14579
|
The following comment was made by an LLM, it may be inaccurate: Based on the search results, I found the following potentially related PRs (excluding the current PR #14581): Potential Duplicates/Related PRs:
Recommendation: Check PR #8316 first, as it explicitly mentions preventing both symlinks AND cross-drive paths - this may be a prior fix that's being re-addressed, or it may have been incomplete/reverted. |
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
Issue for this PR
Closes #14579
Type of change
What does this PR do?
Filesystem.contains()usespath.relative(parent, child).startsWith("..")to check if a child path is inside a parent directory. On Windows, when the paths are on different drive letters,path.relative()returns the absolute child path instead of a..-prefixed relative path:This means
contains()incorrectly returnstrue, allowingFile.read()andFile.list()to access files on any drive.The fix adds a drive letter comparison before the
path.relative()call. This is simpler thanrealpathSync-based approaches (like #8316) because it doesn't depend on the target file existing and has no fallback path that reintroduces the bug.How did you verify your code works?
Filesystem.contains("D:\project", "C:\evil\file.txt")returnstrue(bug) on Windowsfalsebun test test/file/path-traversal.test.ts— 19/19 passbun test test/util/filesystem.test.ts— 38/38 passScreenshots / recordings
N/A — not a UI change.
Checklist