-
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 codemod: Sonar pythonsecurity:S2076 (#830)
New codemod for Sonar pythonsecurity:S2076
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from core_codemods.process_creation_sandbox import ProcessSandbox | ||
from core_codemods.sonar.api import SonarCodemod | ||
|
||
SonarSandboxProcessCreation = SonarCodemod.from_core_codemod( | ||
name="sandbox-process-creation", | ||
other=ProcessSandbox(), | ||
rule_id="pythonsecurity:S2076", | ||
rule_name="OS commands should not be vulnerable to command injection attacks", | ||
) |
66 changes: 66 additions & 0 deletions
66
tests/codemods/sonar/test_sonar_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,66 @@ | ||
import json | ||
|
||
import mock | ||
|
||
from codemodder.codemods.test import BaseSASTCodemodTest | ||
from codemodder.dependency import Security | ||
from core_codemods.sonar.sonar_sandbox_process_creation import ( | ||
SonarSandboxProcessCreation, | ||
) | ||
|
||
|
||
class TestSonarSandboxProcessCreation(BaseSASTCodemodTest): | ||
codemod = SonarSandboxProcessCreation | ||
tool = "sonar" | ||
|
||
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" | ||
) | ||
issues = { | ||
"issues": [ | ||
{ | ||
"rule": "pythonsecurity:S2076", | ||
"status": "OPEN", | ||
"component": "code.py", | ||
"textRange": { | ||
"startLine": 9, | ||
"endLine": 9, | ||
"startOffset": 17, | ||
"endOffset": 34, | ||
}, | ||
} | ||
] | ||
} | ||
self.run_and_assert(tmpdir, input_code, expected, results=json.dumps(issues)) | ||
adds_dependency.assert_called_once_with(Security) |