-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Semgrep subprocess shell False codemod (#706)
* refactor codemod into core codemod * semgrep subprocess shell false codemod
- Loading branch information
1 parent
897a0d6
commit c647ec2
Showing
6 changed files
with
115 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from core_codemods.semgrep.api import SemgrepCodemod | ||
from core_codemods.subprocess_shell_false import SubprocessShellFalse | ||
|
||
SemgrepSubprocessShellFalse = SemgrepCodemod.from_core_codemod( | ||
name="subprocess-shell-false", | ||
other=SubprocessShellFalse, | ||
rule_id="python.lang.security.audit.subprocess-shell-true.subprocess-shell-true", | ||
rule_name="subprocess-shell-true", | ||
) |
This file contains 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
66 changes: 66 additions & 0 deletions
66
tests/codemods/semgrep/test_semgrep_subprocess_shell_false.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import json | ||
|
||
from codemodder.codemods.test import BaseSASTCodemodTest | ||
from core_codemods.semgrep.semgrep_subprocess_shell_false import ( | ||
SemgrepSubprocessShellFalse, | ||
) | ||
|
||
|
||
class TestSemgrepSubprocessShellFalse(BaseSASTCodemodTest): | ||
codemod = SemgrepSubprocessShellFalse | ||
tool = "semgrep" | ||
|
||
def test_name(self): | ||
assert self.codemod.name == "subprocess-shell-false" | ||
|
||
def test_import(self, tmpdir): | ||
input_code = """\ | ||
from subprocess import run | ||
run(args, shell=True) | ||
""" | ||
expexted_output = """\ | ||
from subprocess import run | ||
run(args, shell=False) | ||
""" | ||
|
||
results = { | ||
"runs": [ | ||
{ | ||
"results": [ | ||
{ | ||
"fingerprints": {"matchBasedId/v1": "123"}, | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "code.py", | ||
"uriBaseId": "%SRCROOT%", | ||
}, | ||
"region": { | ||
"endColumn": 22, | ||
"endLine": 2, | ||
"snippet": { | ||
"text": "run(args, shell=True)" | ||
}, | ||
"startColumn": 1, | ||
"startLine": 2, | ||
}, | ||
} | ||
} | ||
], | ||
"message": { | ||
"text": "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." | ||
}, | ||
"properties": {}, | ||
"ruleId": "python.lang.security.audit.subprocess-shell-true.subprocess-shell-true", | ||
} | ||
] | ||
} | ||
] | ||
} | ||
self.run_and_assert( | ||
tmpdir, | ||
input_code, | ||
expexted_output, | ||
results=json.dumps(results), | ||
) |