Skip to content

fix(policy): enhance shell command safety and parsing#15034

Merged
allenhutchison merged 20 commits intomainfrom
adh/fix/pr-15006-feedback
Jan 12, 2026
Merged

fix(policy): enhance shell command safety and parsing#15034
allenhutchison merged 20 commits intomainfrom
adh/fix/pr-15006-feedback

Conversation

@allenhutchison
Copy link
Copy Markdown
Contributor

Addressing feedback from PR #15006.

Changes:

  • Implemented recursive checking for shell command substitutions (e.g., echo $(rm -rf /)).
  • Enforced stricter validation for chained commands.
  • Added fail-safe mechanisms: if command parsing fails, the decision defaults to ASK_USER.
  • Refactored toml-loader for better testability.
  • Added comprehensive tests for command injection and substitution scenarios.

…d injection and parsing, and refactor test setup.
@allenhutchison allenhutchison requested a review from a team as a code owner December 13, 2025 01:39
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @allenhutchison, 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 significantly enhances the security and robustness of the policy engine's handling of shell commands. It introduces advanced parsing capabilities to detect and prevent various forms of command injection and substitution, ensuring that only explicitly allowed commands and their sub-components are executed. The changes also include a critical fail-safe mechanism to handle unparseable commands securely, alongside refactoring for better maintainability and comprehensive new tests to validate these security improvements.

Highlights

  • Enhanced Shell Command Safety: Implemented recursive checking for shell command substitutions (e.g., echo $(rm -rf /)) and enforced stricter validation for chained commands (e.g., cmd1; cmd2).
  • Fail-Safe Mechanism: Introduced a fail-safe where if shell command parsing fails, the policy decision defaults to ASK_USER to prevent unsafe execution of unparseable commands.
  • Improved Testability and Modularity: Refactored the toml-loader module by extracting argsPattern building logic into a dedicated helper function, improving code organization and testability.
  • Comprehensive Test Coverage: Added extensive new test cases to cover various command injection and substitution scenarios, ensuring robust validation of shell commands.
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 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 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

  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
Copy link
Copy Markdown

github-actions Bot commented Dec 13, 2025

Size Change: +1.8 kB (+0.01%)

Total Size: 22.3 MB

Filename Size Change
./bundle/gemini.js 22.3 MB +1.8 kB (+0.01%)
ℹ️ View Unchanged
Filename Size
./bundle/sandbox-macos-permissive-closed.sb 1.03 kB
./bundle/sandbox-macos-permissive-open.sb 890 B
./bundle/sandbox-macos-permissive-proxied.sb 1.31 kB
./bundle/sandbox-macos-restrictive-closed.sb 3.29 kB
./bundle/sandbox-macos-restrictive-open.sb 3.36 kB
./bundle/sandbox-macos-restrictive-proxied.sb 3.56 kB

compressed-size-action

Copy link
Copy Markdown
Contributor

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

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 significantly enhances shell command safety by implementing recursive parsing and checking for chained commands and substitutions. The introduction of a fail-safe mechanism for parsing errors and the comprehensive new tests are excellent additions for security and robustness. The refactoring in toml-loader also improves maintainability. I've identified one high-severity issue where the wrong policy rule could be reported for a denied composite command, and I've provided a suggestion to fix it.

Comment thread packages/core/src/policy/policy-engine.ts Outdated
allenhutchison and others added 2 commits December 15, 2025 09:05
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Comment thread packages/core/src/policy/shell-safety.test.ts
This change fixes an issue where the PolicyEngine would not correctly attribute the rule that caused an ASK_USER decision when processing compound shell commands (e.g., 'git status && unknown_cmd').

It ensures that if a subcommand triggers an ASK_USER decision (either by default or explicit rule), the 'matchedRule' reflects that specific cause, rather than being overwritten or left undefined.

Also adds a 'name' property to PolicyRule for better debugging and updates tests to cover these scenarios.
This ensures that the tests, which use Bash-specific syntax (chained commands, subshells), always use the Bash parser in shell-utils. This avoids parsing failures on Windows where the logic would otherwise default to PowerShell parsing.
Copy link
Copy Markdown
Contributor

@abhipatel12 abhipatel12 left a comment

Choose a reason for hiding this comment

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

LGTM overall! Had a few comments

Comment thread packages/core/src/policy/shell-safety.test.ts
Comment thread packages/core/src/policy/shell-safety.test.ts Outdated
@gemini-cli gemini-cli Bot added status/need-issue Pull requests that need to have an associated issue. and removed status/need-issue Pull requests that need to have an associated issue. labels Jan 7, 2026
@allenhutchison allenhutchison requested a review from a team as a code owner January 8, 2026 22:27
Copy link
Copy Markdown
Contributor

@abhipatel12 abhipatel12 left a comment

Choose a reason for hiding this comment

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

LGTM! Couple of comment nits

Comment thread packages/core/src/policy/shell-safety.test.ts Outdated
Comment thread packages/core/src/policy/shell-safety.test.ts Outdated
@allenhutchison allenhutchison added this pull request to the merge queue Jan 12, 2026
Merged via the queue into main with commit 64cde8d Jan 12, 2026
26 checks passed
@allenhutchison allenhutchison deleted the adh/fix/pr-15006-feedback branch January 12, 2026 19:30
theerud pushed a commit to theerud/gemini-cli that referenced this pull request Jan 13, 2026
…15034)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Abhi <43648792+abhipatel12@users.noreply.github.com>
thacio added a commit to thacio/auditaria that referenced this pull request Jan 24, 2026
kuishou68 pushed a commit to iOfficeAI/aioncli that referenced this pull request Feb 27, 2026
…15034)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Abhi <43648792+abhipatel12@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants