fix(core): preserve full path in workspace validation for non-existent segments#21639
fix(core): preserve full path in workspace validation for non-existent segments#21639AnushkaPandit-21 wants to merge 2 commits intogoogle-gemini:mainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical path validation bug affecting Windows users, particularly those interacting with WSL UNC paths. By ensuring that Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request addresses an important path validation issue on Windows, especially for UNC paths, where fs.realpathSync can return a truncated path for non-existent files. The approach of reconstructing the full path is correct.
However, the implementation has a significant flaw on case-insensitive filesystems like Windows. It doesn't account for potential casing differences between the user-provided path and the canonical path returned in the error object, which can lead to creating a malformed path. I've left a critical comment with a suggested fix to make the path reconstruction robust against casing differences, referencing relevant rules on path sanitization and internal validation.
| const resolvedPrefix = e.path; | ||
| const remainder = fullPath.substring(resolvedPrefix.length); | ||
| if (remainder) { | ||
| return resolvedPrefix + remainder; | ||
| } | ||
| return resolvedPrefix; |
There was a problem hiding this comment.
This logic for reconstructing the path is not safe on case-insensitive filesystems like Windows. fs.realpathSync (which sets e.path) returns a path with canonical casing (e.g., C:\Users), while fullPath (from path.resolve) preserves the user's input casing (e.g., c:\users).
If the casing of the prefix differs, fullPath.substring(resolvedPrefix.length) will produce an incorrect remainder because substring is case-sensitive, leading to a malformed path (e.g., C:\Userss\...). This could cause incorrect workspace validation.
To fix this, we should perform a case-insensitive check to ensure resolvedPrefix is a prefix of fullPath before attempting to get the remainder.
const resolvedPrefix = e.path;
// On case-insensitive filesystems (e.g. Windows), `e.path` and `fullPath`
// can have different casing, making `substring` unsafe.
if (fullPath.toLowerCase().startsWith(resolvedPrefix.toLowerCase())) {
return resolvedPrefix + fullPath.substring(resolvedPrefix.length);
}
// Fallback to the truncated path to avoid creating a malformed path.
return resolvedPrefix;References
- Sanitize user-provided file paths used in file system operations to prevent path traversal vulnerabilities.
- Utility functions that perform file system operations should validate their path inputs internally to prevent path traversal vulnerabilities, rather than relying solely on callers to perform validation.
There was a problem hiding this comment.
Good catch — fixed in 83d500c. Now using a case-insensitive startsWith check before reconstructing the path, with a fallback to the truncated prefix if casing doesn't align.
…t segments When realpathSync fails with ENOENT on non-existent paths, Node's error object (e.path) only contains the path up to the last existing segment, truncating the remaining portions. This causes incorrect path resolution on UNC paths such as WSL (\wsl.localhost\...) where deeply nested non-existent file paths lose their trailing segments. Append the remaining non-existent segments from the original resolved path to the prefix returned by realpathSync, ensuring the full intended path is preserved for workspace boundary checks. Fixes google-gemini#18594
5ff77ca to
2097427
Compare
Address review feedback: on case-insensitive filesystems (Windows), e.path from realpathSync may have different casing than the original resolved path. Use a case-insensitive startsWith check before substring to avoid creating malformed paths.
|
Closing this — the underlying issue was addressed by #21487 which refactored The deeply-nested path tests I added here might still provide useful coverage — happy to resubmit those as a standalone test PR if the team thinks it's worth it. |
Summary
Fixes path validation failures on Windows when accessing non-existent files through UNC paths (e.g. WSL
\\wsl.localhost\...).Root cause: When
fs.realpathSyncfails withENOENTon a non-existent path, Node's error object (e.path) only contains the resolved path up to the last existing segment, silently truncating the rest. For example:fullyResolvedPath()inworkspaceContext.tswas returning this truncatede.pathdirectly, causing downstream path comparisons to operate on incomplete paths. On regular Windows drive paths (C:\...) the truncation is less visible becausepath.relativestill produces a valid sub-path, but on UNC paths (especially WSL paths accessed via MinGW) this leads to confusing "Path not in workspace" errors.Fix: Compute the full resolved path before calling
realpathSync, then on ENOENT append the remaining non-existent segments from the original path toe.path, preserving the full intended path for workspace boundary checks.Changes
packages/core/src/utils/workspaceContext.ts— FixfullyResolvedPath()to append remaining segments whenrealpathSyncreturns a truncatede.pathpackages/core/src/utils/workspaceContext.test.ts— Add tests for deeply nested non-existent paths (both inside and outside workspace)Reproduction
On Windows with WSL installed:
Test plan
workspaceContext.test.tstests pass (29 passed, 11 skipped on Windows)Fixes #18594