fix: prevent crash when message contains image from clipboard on Windows#24825
fix: prevent crash when message contains image from clipboard on Windows#24825enjoykumawat wants to merge 3 commits intogoogle-gemini:mainfrom
Conversation
When pasting a clipboard image, it is saved to a temp directory outside the project. The @path reference resolves to an absolute path, which the `ignore` library rejects with a RangeError when checking gitignore rules. Fix by converting absolute paths to relative before the ignore check, and skipping the check entirely for paths outside the project. Fixes google-gemini#24817
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 addresses a runtime crash occurring on Windows when users paste images from the clipboard into the CLI. The issue was caused by the ignore library failing to process absolute paths pointing to temporary directories outside the project root. The changes ensure that such paths are correctly handled by converting them to relative paths or bypassing the ignore check entirely for out-of-project files. Highlights
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. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request prevents crashes when handling absolute paths outside the project directory, such as temporary clipboard images, by converting them to relative paths before performing ignore checks. A review comment identified that the current logic is insufficient for Windows environments where paths across different drives can cause path.relative to return absolute paths. It is recommended to use a more robust resolution method to ensure consistent behavior and mitigate path traversal risks.
| // Convert absolute paths to relative for the ignore check. The `ignore` | ||
| // library throws RangeError on absolute or `..`-prefixed paths, which | ||
| // happens for clipboard images saved to a temp directory outside the project. | ||
| let ignoreCheckPath = pathName; | ||
| if (path.isAbsolute(pathName)) { | ||
| const targetDir = config.getWorkspaceContext().getDirectories()[0]; | ||
| if (targetDir) { | ||
| ignoreCheckPath = path.relative(targetDir, pathName); | ||
| } | ||
| } | ||
| const isOutsideProject = ignoreCheckPath.startsWith('..'); |
There was a problem hiding this comment.
The current logic for determining if a path is outside the project is incomplete for Windows environments where path.relative can return an absolute path for different drives. To ensure consistent and robust path resolution and to prevent path traversal vulnerabilities, use a single function like resolveToRealPath for all path validations as per repository standards. This ensures that path traversal checks are handled uniformly and correctly across different operating systems.
let ignoreCheckPath = pathName;
if (path.isAbsolute(pathName)) {
const targetDir = resolveToRealPath(config.getTargetDir());
const resolvedPath = resolveToRealPath(pathName);
ignoreCheckPath = path.relative(targetDir, resolvedPath);
}
const isOutsideProject = ignoreCheckPath.startsWith('..') || path.isAbsolute(ignoreCheckPath);References
- Ensure consistent path resolution by using a single, robust function (e.g., resolveToRealPath) for all related path validations, including internal validations in components like WorkspaceContext.
- Sanitize user-provided file paths used in file system operations to prevent path traversal vulnerabilities.
There was a problem hiding this comment.
Good catch — addressed in commit 684bae1:
-
Cross-drive check: Added
|| path.isAbsolute(ignoreCheckPath)toisOutsideProject— on Windows,path.relative()between different drives (e.g.,C:vsD:) returns an absolute path instead of a..-prefixed one. -
Consistent resolution: Now using
resolveToRealPath()for bothtargetDirandpathNamebefore computing the relative path, ensuring symlinks and junction points are resolved uniformly. -
New test: Added a cross-drive path test that verifies the ignore check does not throw for paths on a different drive.
On Windows, path.relative() between different drives (e.g., C: vs D:) returns an absolute path instead of a `..`-prefixed one. Add path.isAbsolute() check to isOutsideProject and use resolveToRealPath for consistent path resolution across platforms.
…n cross-drive Windows On Windows, path.relative() between different drives (e.g., C: and D:) returns the source path unchanged as an absolute path, not a relative one. Guard both the file-stat and glob code paths with path.isAbsolute() and fall back to the absolute path so downstream tools receive a usable path spec.
|
Thanks for the review! Addressed the cross-drive path concern in commit The issue was that
Both cases now use |
|
Hi there! Thank you for your interest in contributing to Gemini CLI. To ensure we maintain high code quality and focus on our prioritized roadmap, we have updated our contribution policy (see Discussion #17383). We only guarantee review and consideration of pull requests for issues that are explicitly labeled as 'help wanted'. All other community pull requests are subject to closure after 14 days if they do not align with our current focus areas. For this reason, we strongly recommend that contributors only submit pull requests against issues explicitly labeled as 'help-wanted'. This pull request is being closed as it has been open for 14 days without a 'help wanted' designation. We encourage you to find and contribute to existing 'help wanted' issues in our backlog! Thank you for your understanding and for being part of our community! |
Summary
Fixes #24817 — On Windows, pasting a clipboard image into the CLI causes a
RangeError: path should be a path.relative()'d stringcrash.Root cause: Clipboard images are saved to a temp directory outside the project (e.g.,
C:/Users/.../.gemini/tmp/editor/images/clipboard-xxx.png). WhenresolveFilePaths()inatCommandProcessor.tspasses this absolute path toFileDiscoveryService.shouldIgnoreFile(), theignorelibrary'scheckPath()throws aRangeErrorbecause it only accepts relative paths within the project root.Fix: Convert absolute
@pathreferences to relative paths (relative to the workspace root) before calling the ignore check. If the resulting relative path starts with..(meaning it's outside the project), skip the ignore check entirely — files outside the project can't be gitignored/geminiignored anyway.Changes
packages/cli/src/ui/hooks/atCommandProcessor.ts: Convert absolute paths to relative before ignore checks, skip check for out-of-project pathspackages/cli/src/ui/hooks/atCommandProcessor.test.ts: Add test for absolute clipboard image paths outside the projectTest plan
atCommandProcessor.test.tstests pass (59 existing + 1 new)RangeError