[Spec Resync] 06-25-2025#2407
Conversation
| pr_body += "The following specs were changed:\n- " | ||
| process = subprocess.run( | ||
| ["git diff --name-only | awk -F'/' '{print $2}' | sort | uniq"], | ||
| shell=True, |
There was a problem hiding this comment.
Semgrep identified an issue in your code:
Found 'subprocess' function 'run' with 'shell=True'. This is dangerous because this call will spawn the command using a shell process. Doing so propagates current shell settings and variables, which makes it much easier for a malicious actor to execute commands. Use 'shell=False' instead.
To resolve this comment:
💡 Follow autofix suggestion
| shell=True, | |
| shell=False, |
View step-by-step instructions
- Change the
subprocess.runcall to useshell=False(which is the default), and provide the command as a list rather than a string. - Split the long shell command (
git diff --name-only | awk -F'/' '{print $2}' | sort | uniq) into individual arguments so you can pass it as a list, or use Python modules likesubprocess.PIPEand multiplesubprocesscalls to replicate the shell pipeline. - For your example, you can achieve similar results in Python by using multiple subprocess runs:
- Run
git diff --name-only - Pass the output to
awkvia subprocess, or process it in Python - Sort and deduplicate results in Python
- Run
- Replace the vulnerable code:
With safer code like:
process = subprocess.run( ["git diff --name-only | awk -F'/' '{print $2}' | sort | uniq"], shell=True, capture_output=True, text=True)process1 = subprocess.run(["git", "diff", "--name-only"], capture_output=True, text=True) lines = [line.strip().split('/')[1] for line in process1.stdout.strip().splitlines() if '/' in line] unique_sorted = sorted(set(lines)) process_stdout = '\n'.join(unique_sorted) + '\n' if unique_sorted else '' pr_body += process_stdout.replace("\n", "\n- ")
Alternatively, if you must use shell pipelines, make absolutely sure the arguments are constant and trusted, but avoid shell=True unless absolutely necessary. Using direct argument lists is safer and avoids shell injection risks.
💬 Ignore this finding
Reply with Semgrep commands to ignore this finding.
/fp <comment>for false positive/ar <comment>for acceptable risk/other <comment>for all other reasons
Alternatively, triage in Semgrep AppSec Platform to ignore the finding created by subprocess-shell-true.
You can view more details about this finding in the Semgrep AppSec Platform.
The following specs were changed:
The following spec syncs encountered errors: