-
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.
New Semgrep codemod: sandbox process creation (#831)
- Loading branch information
Showing
4 changed files
with
129 additions
and
2 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
16 changes: 16 additions & 0 deletions
16
src/core_codemods/semgrep/semgrep_sandbox_process_creation.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,16 @@ | ||
from core_codemods.process_creation_sandbox import ProcessSandbox | ||
from core_codemods.semgrep.api import SemgrepCodemod, ToolRule, semgrep_url_from_id | ||
|
||
SemgrepSandboxProcessCreation = SemgrepCodemod.from_core_codemod( | ||
name="sandbox-process-creation", | ||
other=ProcessSandbox(), | ||
rules=[ | ||
ToolRule( | ||
id=( | ||
rule_id := "python.lang.security.dangerous-system-call.dangerous-system-call" | ||
), | ||
name="dangerous-system-call", | ||
url=semgrep_url_from_id(rule_id), | ||
), | ||
], | ||
) |
106 changes: 106 additions & 0 deletions
106
tests/codemods/semgrep/test_semgrep_sandbox_process_creation.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,106 @@ | ||
import mock | ||
|
||
from codemodder.codemods.test import BaseSASTCodemodTest | ||
from codemodder.dependency import Security | ||
from core_codemods.semgrep.semgrep_sandbox_process_creation import ( | ||
SemgrepSandboxProcessCreation, | ||
) | ||
|
||
|
||
class TestSonarSandboxProcessCreation(BaseSASTCodemodTest): | ||
codemod = SemgrepSandboxProcessCreation | ||
tool = "semgrep" | ||
|
||
def test_name(self): | ||
assert self.codemod.name == "sandbox-process-creation" | ||
|
||
@mock.patch("codemodder.codemods.api.FileContext.add_dependency") | ||
def test_simple(self, adds_dependency, tmpdir): | ||
input_code = """ | ||
import os | ||
from flask import render_template, request | ||
@app.route('/vuln', methods=['GET', 'POST']) | ||
def vuln(): | ||
output = "" | ||
if request.method == 'POST': | ||
command = request.form.get('command') | ||
output = os.popen(command).read() | ||
return render_template('vuln.html', output=output) | ||
""".lstrip( | ||
"\n" | ||
) | ||
expected = """ | ||
import os | ||
from flask import render_template, request | ||
from security import safe_command | ||
@app.route('/vuln', methods=['GET', 'POST']) | ||
def vuln(): | ||
output = "" | ||
if request.method == 'POST': | ||
command = request.form.get('command') | ||
output = safe_command.run(os.popen, command).read() | ||
return render_template('vuln.html', output=output) | ||
""".lstrip( | ||
"\n" | ||
) | ||
self.run_and_assert(tmpdir, input_code, expected, results=SARIF) | ||
adds_dependency.assert_called_once_with(Security) | ||
|
||
|
||
SARIF = """ | ||
{ | ||
"runs": [ | ||
{ | ||
"automationDetails": { | ||
"id": ".github/workflows/semgrep.yml:semgrep_scan/" | ||
}, | ||
"conversion": { | ||
"tool": { | ||
"driver": { | ||
"name": "GitHub Code Scanning" | ||
} | ||
} | ||
}, | ||
"results": [ | ||
{ | ||
"correlationGuid": "a90240a2-8d09-47eb-a1c5-0af9d5b225c9", | ||
"level": "error", | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"index": 1, | ||
"uri": "code.py" | ||
}, | ||
"region": { | ||
"endColumn": 35, | ||
"endLine": 9, | ||
"startColumn": 18, | ||
"startLine": 9 | ||
} | ||
} | ||
} | ||
], | ||
"message": { | ||
"text": "Found user-controlled data used in a system call. This could allow a malicious actor to execute commands. Use the 'subprocess' module instead, which is easier to use without accidentally exposing a command injection vulnerability." | ||
}, | ||
"partialFingerprints": { | ||
"primaryLocationLineHash": "b897622e8906ac69:1" | ||
}, | ||
"properties": { | ||
"github/alertNumber": 2, | ||
"github/alertUrl": "https://api.github.com/repos/nahsra/vulnerable-app-sample/code-scanning/alerts/2" | ||
}, | ||
"rule": { | ||
"id": "python.lang.security.dangerous-system-call.dangerous-system-call", | ||
"index": 723 | ||
}, | ||
"ruleId": "python.lang.security.dangerous-system-call.dangerous-system-call" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
""" |