Skip to content

fix(editor): harden Windows subprocess argument quoting and prevent command injection on Windows - #29510

Open
zainnadeem786 wants to merge 3 commits into
google-gemini:mainfrom
zainnadeem786:fix-editor-windows-quoting
Open

zainnadeem786 wants to merge 3 commits into
google-gemini:mainfrom
zainnadeem786:fix-editor-windows-quoting

Conversation

@zainnadeem786

@zainnadeem786 zainnadeem786 commented Sep 26, 2026 •

Copy link
Copy Markdown

Summary

This PR hardens Windows subprocess execution in packages/core/src/utils/editor.ts by introducing a robust argument quoting helper (quoteCmdArg) for shell: true invocations.

Problem / Vulnerability Addressed

When spawning diff commands on Windows using shell: true, file paths and arguments containing spaces, quotes, or shell metacharacters (&, |, ^, %VAR%) were passed unquoted to cmd.exe. This created risks of:

  1. Argument Splitting: Paths with spaces (e.g., C:\Program Files\...) were split into multiple arguments.
  2. Command Injection / Metacharacter Evaluation: Special characters in filenames could be interpreted as command separators or redirection operators by cmd.exe.
  3. Environment Variable Expansion: %VAR% sequences inside paths could trigger unintended variable expansion.

Solution & Key Changes

  1. quoteCmdArg Implementation:
    • Safely preserves alphanumeric characters, hyphens, underscores, dots, colons, and forward/backward slashes.
    • Automatically wraps arguments with spaces or special characters in double quotes.
    • Escapes internal double quotes (" -> "") and carets (% -> ^%) to neutralize cmd.exe variable expansion.
    • Properly handles trailing backslashes so cmd.exe / CommandLineToArgvW does not misinterpret quotes.
  2. Platform-Specific Quoting: Refactored openDiff to map quoteCmdArg across diffCommand.args exclusively when process.platform === 'win32'.
  3. Automated Testing: Added extensive unit test suites in packages/core/src/utils/editor.test.ts covering safe paths, edge cases, special characters, and cross-platform consistency.

Test Verification

  • Core editor unit tests (editor.test.ts): 233 / 233 passed (100%)
  • Linter & Typecheck: Clean (0 errors, 0 warnings)

Relates to #29625

@zainnadeem786
zainnadeem786 requested a review from a team as a code owner September 26, 2026 06:51
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 security vulnerability in Windows subprocess execution where file paths and arguments were passed unquoted to cmd.exe. By implementing a specialized quoting mechanism, the changes prevent malicious command injection and ensure that paths containing spaces or shell metacharacters are handled correctly by the Windows shell.

Highlights

  • Security Hardening: Introduced a robust quoteCmdArg utility to safely escape and quote arguments when spawning subprocesses on Windows, mitigating risks of command injection and argument splitting.
  • Platform-Specific Logic: Updated openDiff to conditionally apply argument quoting only when running on Windows, ensuring cross-platform compatibility.
  • Automated Testing: Added comprehensive unit tests covering various edge cases, including special shell characters, environment variable patterns, and path formatting.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the 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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. 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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@github-actions github-actions Bot added the size/m A medium sized PR label Sep 26, 2026
@github-actions

github-actions Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

📊 PR Size: size/M

  • Lines changed: 144
  • Additions: +142
  • Deletions: -2
  • Files changed: 2

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a quoteCmdArg utility in packages/core/src/utils/editor.ts to escape and quote command-line arguments when launching diff tools via cmd.exe on Windows, along with corresponding unit tests. However, the feedback highlights critical security and correctness vulnerabilities in the implementation of quoteCmdArg, including potential command injection via newlines, incorrect handling of percent signs, and nested double quotes. The reviewer has provided suggestions to address these issues and update the test assertions accordingly.

Comment thread packages/core/src/utils/editor.ts
Comment thread packages/core/src/utils/editor.test.ts Outdated
@zainnadeem786

Copy link
Copy Markdown
Author

@gemini-code-assist review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new utility function quoteCmdArg to safely quote arguments when spawning editor processes on Windows, preventing command injection and argument splitting. The reviewer identified an issue where escaping percent signs (%) with carets (^%) inside double quotes is incorrect because cmd.exe treats carets literally inside double quotes, which would corrupt paths containing %. The reviewer suggested removing this escaping and updating the corresponding test cases.

Comment thread packages/core/src/utils/editor.ts Outdated
Comment thread packages/core/src/utils/editor.test.ts Outdated
@gemini-cli

gemini-cli Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

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 only guarantee review and consideration of pull requests for issues that are explicitly labeled as 'help wanted'.

This PR will be closed in 7 days if it remains without that designation. We encourage you to find and contribute to existing 'help wanted' issues in our backlog! Thank you for your understanding.

@gemini-cli gemini-cli Bot added priority/p2 Important but can be addressed in a future release. area/core Issues related to User Interface, OS Support, Core Functionality and removed status/need-issue Pull requests that need to have an associated issue. labels Oct 4, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality priority/p2 Important but can be addressed in a future release. size/m A medium sized PR status/pr-nudge-sent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant