Fix injection vulnerability in API generation script#11
Open
Fix injection vulnerability in API generation script#11
Conversation
…rocess.run em generate_api.py
Reviewer's guide (collapsed on small PRs)Reviewer's GuideRefactors git metadata helpers in docs/generate_api.py to use subprocess.run instead of subprocess.check_output, improving argument safety and error handling for git commands. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 2 security issues, and left some high level feedback:
Security issues:
- Detected subprocess function 'run' without a static string. If this data can be controlled by a malicious actor, it may be an instance of command injection. Audit the use of this call to ensure it is not controllable by an external resource. You may consider using 'shlex.escape()'. (link)
- Detected subprocess function 'run' without a static string. If this data can be controlled by a malicious actor, it may be an instance of command injection. Audit the use of this call to ensure it is not controllable by an external resource. You may consider using 'shlex.escape()'. (link)
General comments:
- The security rationale in the comments and PR description is slightly misleading, since the original
subprocess.check_outputalready used an argument list withoutshell=True; consider clarifying that this is primarily a refactor (e.g., standardizing onsubprocess.run) rather than a security hardening. - The new comments above both
git_head_commitandgit_project_rootare nearly identical and somewhat verbose; consider reducing duplication or tightening the wording to focus on whysubprocess.runis preferred in this codebase.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The security rationale in the comments and PR description is slightly misleading, since the original `subprocess.check_output` already used an argument list without `shell=True`; consider clarifying that this is primarily a refactor (e.g., standardizing on `subprocess.run`) rather than a security hardening.
- The new comments above both `git_head_commit` and `git_project_root` are nearly identical and somewhat verbose; consider reducing duplication or tightening the wording to focus on why `subprocess.run` is preferred in this codebase.
## Individual Comments
### Comment 1
<location> `docs/generate_api.py:1153` </location>
<code_context>
completed = subprocess.run(process_args, stdout=subprocess.PIPE, text=True, check=True)
</code_context>
<issue_to_address>
**security (python.lang.security.audit.dangerous-subprocess-use-audit):** Detected subprocess function 'run' without a static string. If this data can be controlled by a malicious actor, it may be an instance of command injection. Audit the use of this call to ensure it is not controllable by an external resource. You may consider using 'shlex.escape()'.
*Source: opengrep*
</issue_to_address>
### Comment 2
<location> `docs/generate_api.py:1171` </location>
<code_context>
completed = subprocess.run(cmd, stdout=subprocess.PIPE, text=True, check=True)
</code_context>
<issue_to_address>
**security (python.lang.security.audit.dangerous-subprocess-use-audit):** Detected subprocess function 'run' without a static string. If this data can be controlled by a malicious actor, it may be an instance of command injection. Audit the use of this call to ensure it is not controllable by an external resource. You may consider using 'shlex.escape()'.
*Source: opengrep*
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changing
subprocess.check_outputtosubprocess.runenhances security by avoiding shell invocation and safely passing arguments.Summary by Sourcery
Enhancements: