Skip to content

fix(grep): prevent command-line option injection by passing search patterns with explicit -e delimiter - #29536

Open
zainnadeem786 wants to merge 4 commits into
google-gemini:mainfrom
zainnadeem786:fix-grep-option-injection
Open

zainnadeem786 wants to merge 4 commits into
google-gemini:mainfrom
zainnadeem786:fix-grep-option-injection

Conversation

@zainnadeem786

@zainnadeem786 zainnadeem786 commented Sep 28, 2026 •

Copy link
Copy Markdown

Summary

This PR hardens the local grep execution module in packages/core/src/tools/grep.ts against Command-Line Option / Argument Injection (CWE-88) by enforcing strict argument separation using the explicit pattern terminator (-e) for both git grep and system grep pipelines.

Problem / Vulnerability Addressed

When constructing search queries, both git grep and system grep previously appended user-supplied or workspace-derived search patterns directly as raw positional arguments:

  • gitArgs = ['grep', '--untracked', '-n', '-E', '--ignore-case', pattern]
  • grepArgs.push(pattern)

Because POSIX and GNU command-line parsers process arguments sequentially from left to right, any token beginning with a hyphen (- or --) is interpreted as a command-line flag or option rather than a literal search string.

Impact:

  1. Command Behavior Alteration / Flag Injection: Attackers or untrusted workspaces containing files or search inputs starting with hyphens (e.g., -v, --max-count) could force the underlying tool to misinterpret inputs as flags, altering search output or triggering inverted matches.
  2. Denial of Service (DoS): Passing invalid flag structures (such as --max-count without a trailing integer value) caused the underlying subprocess to crash with fatal errors (fatal: option '--max-count' requires a value), disrupting the active CLI session.
  3. Defense-in-Depth Violation: Failing to isolate positional match patterns from command option parsers violated secure subprocess spawning guidelines.

Solution & Key Changes

  1. Explicit -e Delimiter Enforcement:
    • Updated git grep command building to push -e immediately before the search pattern:
      gitArgs.push('-e', pattern);
    • Updated system grep command building to prepend -e before the pattern:
      grepArgs.push('-e', pattern);
  2. Comprehensive Unit Testing:
    • Added robust unit test cases in packages/core/src/tools/grep.test.ts validating that leading-hyphen search patterns (e.g., -v and --flag) are safely preceded by the -e flag in spawned arguments.

Test Verification

  • Core grep test suite (grep.test.ts): 35 / 35 tests passed (100%)
  • Linter & Typecheck: Clean (0 errors, 0 warnings)

Relates to #29627

@zainnadeem786
zainnadeem786 requested a review from a team as a code owner September 28, 2026 08:35
@github-actions github-actions Bot added the size/m A medium sized PR label Sep 28, 2026
@github-actions

github-actions Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

📊 PR Size: size/M

  • Lines changed: 87
  • Additions: +78
  • Deletions: -9
  • Files changed: 2

@gemini-cli gemini-cli Bot added the status/need-issue Pull requests that need to have an associated issue. label Sep 28, 2026
@github-actions

github-actions Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

🛑 Action Required: Evaluation Approval

Steering changes have been detected in this PR. To prevent regressions, a maintainer must approve the evaluation run before this PR can be merged.

Maintainers:

  1. Go to the Workflow Run Summary.
  2. Click the yellow 'Review deployments' button.
  3. Select the 'eval-gate' environment and click 'Approve'.

Once approved, the evaluation results will be posted here automatically.

@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 the grep execution module where user-provided search patterns could be misinterpreted as command-line flags. By enforcing the use of the '-e' pattern terminator, the system now ensures that all search inputs are treated as positional arguments, significantly improving the robustness and security of the subprocess execution logic.

Highlights

  • Security Hardening: Implemented the use of the explicit '-e' delimiter for both 'git grep' and system 'grep' commands to prevent command-line option injection (CWE-88).
  • Vulnerability Mitigation: Ensured that search patterns starting with hyphens are treated as literal strings rather than command flags, preventing potential behavior alteration or subprocess crashes.
  • Testing: Added comprehensive unit tests to verify that leading-hyphen patterns are correctly isolated with the '-e' flag in both 'git' and 'system' grep execution paths.
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. ↩

@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 modifies the grep tool to safely handle patterns with leading hyphens by prepending them with the -e flag in both system grep and git grep executions, and adds corresponding unit tests. The review feedback recommends validating and trimming the search pattern to prevent performance issues from whitespace-only inputs, clearing the execStreaming mock in tests to avoid state leakage, and using synchronous filesystem methods for test setup to enhance readability.

Comment thread packages/core/src/tools/grep.ts
Comment thread packages/core/src/tools/grep.test.ts
Comment thread packages/core/src/tools/grep.test.ts
@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 improves the grep tool by validating that the search pattern is not empty or whitespace-only, and by passing the -e flag before the pattern in both git grep and system grep commands to safely handle leading hyphens. It also adds corresponding unit tests. The review feedback recommends avoiding monkey-patching node:fs/promises in the test file, suggesting instead to import and use mkdirSync directly from node:fs to prevent potential runtime errors in strict ES module environments.

Comment thread packages/core/src/tools/grep.test.ts Outdated
Comment thread packages/core/src/tools/grep.test.ts Outdated
@gemini-cli

gemini-cli Bot commented Oct 6, 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.

@zainnadeem786

Copy link
Copy Markdown
Author

Hi team, just following up on this security hardening PR (#29536). It has already received an approving review from @svessebror-maker. Could a maintainer please help add the appropriate label or merge this in before the 7-day automated closure window? Thank you!

This branch is waiting to be deployed

1 waiting deployment
eval-gate — f1966fcb Waiting Sep 29, 2026 by zainnadeem786 via Evaluate Steering & Regressions #2103
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/security Issues related to security 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.

2 participants