Repository navigation
fix(cli): prevent CPU hang and quote swallowing on @ within code (#29434) - #29557
Conversation
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 performance issue where the CLI would hang at 100% CPU usage when processing specific code snippets containing scoped package imports. By tightening the path-parsing regex and adding defensive validation before invoking recursive glob searches, the changes prevent the parser from greedily consuming large blocks of code and protect the system from combinatorial explosions in the glob matching engine. 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 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 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
|
|
📊 PR Size: size/M
|
There was a problem hiding this comment.
Code Review
This pull request addresses issue #29434 by updating the '@' command path regex to prevent catastrophic multi-line matches and introducing constraints on glob search paths (such as a maximum length of 255 characters and excluding newlines/null bytes) to avoid runaway searches. Regression tests are also added to verify these fixes. The reviewer identified a potential Denial of Service vulnerability where paths containing curly braces could still trigger CPU hangs via brace expansion in 'minimatch', and suggested explicitly disallowing '{' and '}' in the glob suitability check.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request addresses issue #29434 by updating the @ command path regex to prevent catastrophic multi-line matches and runaway glob searches when processing quoted strings (e.g., imports). It also introduces constraints on recursive glob searches, limiting path lengths to 255 characters and rejecting paths with newlines, null bytes, or curly braces, supported by comprehensive regression tests. Feedback on these changes suggests further securing the glob search path validation by explicitly checking for and rejecting directory traversal sequences (..) to prevent path traversal vulnerabilities.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request addresses issue #29434 by preventing runaway glob searches and hangs when processing @ commands. It updates the path regex to avoid matching across quotes and multi-line imports, adds validation to skip recursive glob searches for unsuitable paths (such as excessively long paths, directory traversals, or invalid characters), and includes comprehensive regression tests. The review feedback suggests further strengthening the path validation by also excluding absolute paths and glob metacharacters (such as *, ?, [, ]) to avoid unexpected matching behavior and unnecessary CPU usage.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request resolves issue #29434 by preventing runaway glob searches and hangs when processing @ commands. It refactors the regular expression in atCommandProcessor.ts to avoid catastrophic multi-line matches on quoted strings and restricts unquoted paths from containing unescaped quotes. It also introduces safety checks to skip recursive glob searches for paths that are excessively long, absolute, contain directory traversal sequences, or contain invalid/wildcard characters. Robust regression tests have been added to cover these scenarios. I have no feedback to provide on these changes.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request addresses issue #29434 by preventing runaway glob searches and hangs when processing '@' commands. It updates the regular expression in atCommandProcessor.ts to avoid catastrophic multi-line matches on quoted strings (such as package imports) and introduces strict criteria (isPathSuitableForGlob) to skip recursive glob searches on unsuitable paths (e.g., excessively long paths, absolute paths, directory traversals, or paths containing invalid characters). Comprehensive regression tests have also been added to validate these safety checks. No review comments were provided, so I have no additional feedback to offer.
Summary
Fixes an uninterruptible 100% CPU lockup in headless / non-interactive mode (
-pwith piped stdin) caused by catastrophic quote-swallowing when code contains scoped packages (@scope/pkg) followed by quoted strings. Incorporates reviewer feedback to protect against brace-expansion ReDoS inminimatch, sanitize paths against directory traversal (..), and exclude absolute paths and glob metacharacters from fallback searches.Details
Problem
gemini -p,atCommandProcessorscans for@<path>references.AT_COMMAND_PATH_REGEX_SOURCE, the double-quoted string branch"(?:[^"]*)"was nested inside a repeated outer group(?: ... )+and allowed arbitrary characters (including newlines and delimiters) across unescaped quotes.import { useThing } from "@scope/pkg";followed by subsequent imports or quoted strings, the@inside quotes was matched and the closing quote afterpkgwas parsed as the start of a"(?:[^"]*)"quoted span. This greedily swallowed entire source files (thousands of characters across tens or hundreds of lines) into a single@pathtoken.resolveFilePathsfell back to recursive glob search (config.getEnableRecursiveFileSearch()), constructing**/*${pathName}*with the unvalidated multi-kilobyte string.glob/minimatch, the presence of dozens of import statements with curly braces ({ a, b, c }) triggered combinatorial explosion inbrace-expansion(reaching its ceiling of 100,000 patterns) and synchronous preprocessing inminimatchon the Node main V8 thread. This starved the event loop, pegged CPU at 100%, and ignoredSIGINT/SIGTERM/AbortSignal.Solution
AT_COMMAND_PATH_REGEX_SOURCE(atCommandProcessor.ts):"[^"\n\r]*").",',`) to delimiters for unquoted path tokens so unquoted paths stop immediately before quotes.resolveFilePathsinatCommandProcessor.ts):pathName.length > 0 && pathName.length <= MAX_GLOB_SEARCH_PATH_LENGTH (255) && !path.isAbsolute(pathName) && !pathName.includes('..') && !/[\r\n\t\0{}*?\[\]]/.test(pathName)before invokingglobTool.buildAndExecute.{,}) in recursive glob search paths, eliminating combinatorial brace expansion inminimatch(per code review feedback #5355588500)...) to prevent path traversal during glob fallback (per code review feedback #5356587921).!path.isAbsolute(pathName)) and glob metacharacters (*,?,[,],\t) to prevent invalid search patterns and runaway wildcard matching (per code review feedback #5356733854).Related Issues
Fixes #29434
How to Validate
1. Automated Vitest Tests
Run the updated regression test suite:
npm test -w @google/gemini-cli -- src/ui/hooks/atCommandProcessor.test.tsExpected: All 72 tests pass, including:
does not greedily consume code across quotes when @ is inside quotes (#29434)does not hang when input contains @scope/pkg followed by many imports (#29434)does not invoke recursive glob search on paths exceeding MAX_GLOB_SEARCH_PATH_LENGTH (#29434)does not invoke recursive glob search on paths containing newlines (#29434)does not invoke recursive glob search on paths containing curly braces (#29434)does not invoke recursive glob search on paths containing directory traversal sequences (#29434)does not invoke recursive glob search on absolute paths that do not exist (#29434)does not invoke recursive glob search on paths containing glob wildcards (#29434)2. Manual Reproduction Verification
Create a test file with 60 TypeScript/ESM imports:
Expected: The CLI processes the input and dispatches to the model in < 1 second instead of freezing at 100% CPU.
3. File Reference Regressions
Verify that standard
@path/to/file.txt, escaped spaces (@path\ with\ spaces/file.txt), and Windows quoted paths (@"path with spaces/file.txt") continue to resolve and attach correctly.Pre-Merge Checklist