Fix(policy): fix commandPrefix matching by preserving spaces and hyphens in regex patterns#20760
Conversation
…ens in regex patterns
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 critical bug where policy DENY rules using 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
|
There was a problem hiding this comment.
Code Review
This pull request correctly fixes a bug where policy rules with commandPrefix were failing to match due to incorrect escaping of spaces and hyphens. The changes in escapeRegex and buildArgsPatterns effectively resolve the mismatch between the generated regex and the stringified tool arguments. The accompanying test updates properly validate the fix. The suggestion to refactor the implementation in packages/core/src/policy/utils.ts to improve code clarity and maintainability is valid and has been retained.
| // Escape regex special characters but preserve spaces since JSON | ||
| // contains literal spaces (not escaped) in command strings. | ||
| const escapedPrefix = escapeRegex(jsonPrefix).replace(/\\ /g, ' '); |
There was a problem hiding this comment.
The current approach of escaping spaces in escapeRegex and then immediately un-escaping them here is functionally correct but indirect and could be confusing for future maintenance.
A cleaner implementation would be to prevent escapeRegex from escaping spaces in the first place. This would simplify the logic in buildArgsPatterns and make the overall flow more intuitive.
Here's a suggested refactoring:
-
In
packages/core/src/policy/utils.ts, updateescapeRegex:
ModifyescapeRegexon line 11 to not escape spaces.return text.replace(/[[\\\]{}()*+?.,^$|#"]/g, '\\$&');
-
In
packages/core/src/policy/utils.ts, simplifybuildArgsPatterns:
With the above change, you can remove the.replace()call from this line (line 74).const escapedPrefix = escapeRegex(jsonPrefix);
-
In
packages/core/src/policy/utils.test.ts, update the test case:
The test forescapeRegexwill need to be updated to reflect that spaces are no longer escaped (around line 15).expect(escaped).toBe( '\.-\*\+\?\^\$\{\}\(\)\|\[\]\\ ',
There was a problem hiding this comment.
Updated according to you review.
|
Hi @Adib234 . Can you reivew this PR whenever you get time. Waiting for your feedback. |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request addresses a bug in policy matching for commandPrefix where commands containing spaces or hyphens were not being correctly matched by DENY rules. The fix involves modifying escapeRegex to no longer escape spaces and hyphens, and updating buildArgsPatterns to correctly handle prefixes with trailing spaces by adjusting the boundary check logic. The accompanying test updates in persistence.test.ts and utils.test.ts correctly reflect these changes. The implementation appears solid and effectively resolves the described issue.
|
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:
Policy DENY rules using commandPrefix were silently failing to match shell commands, allowing blocked commands like git branch -d to execute despite explicit deny rules. The root cause was that escapeRegex() was escaping spaces as \ and hyphens as -, but stableStringify() produces JSON with literal spaces and unescaped hyphens. This mismatch meant the generated regex pattern never matched the actual JSON string being tested.
Details:
Two changes were made in packages/core/src/policy/utils.ts:
Removed - from escapeRegex() character class — hyphens don't need escaping outside regex character classes
Added .replace(/\ /g, ' ') in buildArgsPatterns() to unescape spaces after regex escaping, since JSON strings contain literal spaces
When a prefix ends with a space (e.g. "git branch -d "), the word boundary check (?:[\s"]|\") is skipped since the trailing space already acts as a delimiter, allowing the branch name to follow naturally.
Tests in utils.test.ts and persistence.test.ts were updated to reflect the corrected pattern format.
Related Issues:
Fixes #20355
How to Validate:
Add to ~/.gemini/settings.json or policy TOML:
toml[[rule]]
toolName = "run_shell_command"
commandPrefix = "git branch -d "
decision = "deny"
priority = 999
Launch gemini and ask it to delete a git branch
The command should now be blocked with a DENY decision
Without the fix, the command would execute silently
Pre-Merge Checklist