-
-
Notifications
You must be signed in to change notification settings - Fork 262
fix: add shell pipe support with security validation #211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @vitali87, 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 shell command execution functionality by introducing support for piped commands while simultaneously bolstering security measures. It ensures that all commands within a pipeline are validated against an allowlist and explicitly prevents potentially dangerous subshell executions. This change broadens the utility of the shell command tool without compromising system integrity. 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 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.
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 adds valuable support for shell pipelines, including comprehensive validation and security checks. The changes are well-tested with new unit and integration tests. However, I've identified a critical security vulnerability where the 'dangerous command' check (rm -rf) is bypassed for commands within a pipeline. This could allow for unintended destructive operations. I've provided a detailed comment with a suggested fix to ensure every command in a pipeline is properly vetted. Addressing this issue is crucial for the security of this feature.
|
/gemini review |
There was a problem hiding this 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 introduces support for piped shell commands, which is a great feature. The implementation correctly identifies the need for security validation by checking for subshells and validating all commands in a pipeline against an allowlist.
However, I've identified several critical security vulnerabilities related to command parsing and the choice of allowlisted commands.
- The allowlisting of powerful commands like
xargs,awk, andsedcreates a command injection vulnerability because their arguments are not validated. - Malformed command segments can bypass both the allowlist check and the dangerous command check due to how parsing errors are currently handled.
I've provided detailed comments with suggestions to fix these critical issues. Additionally, I've included a medium-severity comment to improve the regex used for splitting piped commands. Addressing these security concerns is crucial before this change is merged.
|
/gemini review |
There was a problem hiding this 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 the shell command execution security by introducing a comprehensive set of dangerous command checks and expanding the allowlist for common Unix utilities. Key changes include adding bandit for security scanning, extending the SHELL_COMMAND_ALLOWLIST in config.py with commands like wc, head, tail, sort, uniq, cut, tr, xargs, awk, sed, and tee. New constants were added in constants.py to define SHELL_DANGEROUS_COMMANDS, SHELL_RM_DANGEROUS_FLAGS, and a detailed list of SHELL_DANGEROUS_PATTERNS using regex to detect malicious commands or patterns (e.g., rm -rf /, dd to device, remote script execution, awk system() calls, subshell patterns). The core logic in shell_command.py was refactored to parse complex shell commands with pipes and logical operators, check for subshell execution, validate all commands in a pipeline against the allowlist, and apply the new dangerous command and pattern checks. The execute method now uses asyncio.create_subprocess_shell to support these features. Extensive new unit and integration tests were added to cover these security validations and piped command execution scenarios. Review comments highlighted that the awk pipe regex r"awk\s+.*\|" in SHELL_DANGEROUS_PATTERNS is too broad and could cause false positives by blocking legitimate pipelines, suggesting its removal. Another comment noted redundant logic in _check_dangerous_patterns, as it's called on both the full command and individual segments, recommending a refactoring to use distinct pattern sets for clarity and efficiency.
|
/gemini review |
There was a problem hiding this 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 introduces support for piped shell commands, which is a significant feature enhancement. The implementation shows a strong focus on security, with multiple layers of validation including command allowlisting, subshell blocking, and dangerous pattern matching using regular expressions. The accompanying tests are thorough and cover a wide range of scenarios, including many security-related edge cases. My review includes a critical fix for the pre-commit configuration, a suggestion to harden one of the security regex patterns, and a recommendation to properly acknowledge a security scanner warning for maintainability. Overall, this is a well-executed and security-conscious implementation.
| pass_filenames: false | ||
| always_run: true | ||
| - repo: https://github.com/PyCQA/bandit | ||
| rev: 1.9.2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # (H) Dangerous patterns for individual segments (per-command patterns) | ||
| SHELL_DANGEROUS_PATTERNS_SEGMENT = ( | ||
| (r"rm\s+.*-[rf]+\s+/($|\s)", "rm with root path"), | ||
| (r"rm\s+.*-[rf]+\s+/[a-z]+($|\s)", "rm with system directory"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This regex to detect rm on a system directory is too restrictive and can be easily bypassed. The pattern /[a-z]+ will not match common system directories like /usr, /etc, or any directory with uppercase letters or numbers. I recommend using a more explicit pattern that lists common Unix/Linux system directories for better protection.
| (r"rm\s+.*-[rf]+\s+/[a-z]+($|\s)", "rm with system directory"), | |
| (r"rm\s+.*-[rf]+\s+/(bin|boot|dev|etc|home|lib|lib64|media|mnt|opt|proc|root|run|sbin|srv|sys|tmp|usr|var)($|/|\s)", "rm with system directory"), |
| process = await asyncio.create_subprocess_shell( | ||
| command, | ||
| stdout=asyncio.subprocess.PIPE, | ||
| stderr=asyncio.subprocess.PIPE, | ||
| cwd=self.project_root, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using create_subprocess_shell will be flagged by security scanners like Bandit (rule B602). Since you've implemented extensive sanitization before this call, it's good practice to explicitly acknowledge and suppress this warning for future maintainers by adding a # nosec B602 comment. This signals that the use of shell=True is intentional and has been secured.
| process = await asyncio.create_subprocess_shell( | |
| command, | |
| stdout=asyncio.subprocess.PIPE, | |
| stderr=asyncio.subprocess.PIPE, | |
| cwd=self.project_root, | |
| process = await asyncio.create_subprocess_shell( # nosec B602 | |
| command, | |
| stdout=asyncio.subprocess.PIPE, | |
| stderr=asyncio.subprocess.PIPE, | |
| cwd=self.project_root, |
Summary
find . -name "*.c" | wc -l)$(...)and backticks) for securitywc,head,tail,sort,uniq,cut,tr,xargs,awk,sed,teeChanges
shell_command.py: Switch fromcreate_subprocess_exec()tocreate_subprocess_shell()with proper validationconfig.py: Add pipe-friendly commands to allowlistconstants.py: Add shell pipe operators and subshell patternstool_errors.py: Add subshell rejection error messageTest plan
_extract_commands()function_has_subshell()functionFixes #207