|
| 1 | +import mock |
| 2 | + |
| 3 | +from codemodder.codemods.test import BaseSASTCodemodTest |
| 4 | +from codemodder.dependency import Security |
| 5 | +from core_codemods.semgrep.semgrep_sandbox_process_creation import ( |
| 6 | + SemgrepSandboxProcessCreation, |
| 7 | +) |
| 8 | + |
| 9 | + |
| 10 | +class TestSonarSandboxProcessCreation(BaseSASTCodemodTest): |
| 11 | + codemod = SemgrepSandboxProcessCreation |
| 12 | + tool = "semgrep" |
| 13 | + |
| 14 | + def test_name(self): |
| 15 | + assert self.codemod.name == "sandbox-process-creation" |
| 16 | + |
| 17 | + @mock.patch("codemodder.codemods.api.FileContext.add_dependency") |
| 18 | + def test_simple(self, adds_dependency, tmpdir): |
| 19 | + input_code = """ |
| 20 | + import os |
| 21 | + from flask import render_template, request |
| 22 | +
|
| 23 | + @app.route('/vuln', methods=['GET', 'POST']) |
| 24 | + def vuln(): |
| 25 | + output = "" |
| 26 | + if request.method == 'POST': |
| 27 | + command = request.form.get('command') |
| 28 | + output = os.popen(command).read() |
| 29 | + return render_template('vuln.html', output=output) |
| 30 | + """.lstrip( |
| 31 | + "\n" |
| 32 | + ) |
| 33 | + expected = """ |
| 34 | + import os |
| 35 | + from flask import render_template, request |
| 36 | + from security import safe_command |
| 37 | +
|
| 38 | + @app.route('/vuln', methods=['GET', 'POST']) |
| 39 | + def vuln(): |
| 40 | + output = "" |
| 41 | + if request.method == 'POST': |
| 42 | + command = request.form.get('command') |
| 43 | + output = safe_command.run(os.popen, command).read() |
| 44 | + return render_template('vuln.html', output=output) |
| 45 | + """.lstrip( |
| 46 | + "\n" |
| 47 | + ) |
| 48 | + self.run_and_assert(tmpdir, input_code, expected, results=SARIF) |
| 49 | + adds_dependency.assert_called_once_with(Security) |
| 50 | + |
| 51 | + |
| 52 | +SARIF = """ |
| 53 | +{ |
| 54 | + "runs": [ |
| 55 | + { |
| 56 | + "automationDetails": { |
| 57 | + "id": ".github/workflows/semgrep.yml:semgrep_scan/" |
| 58 | + }, |
| 59 | + "conversion": { |
| 60 | + "tool": { |
| 61 | + "driver": { |
| 62 | + "name": "GitHub Code Scanning" |
| 63 | + } |
| 64 | + } |
| 65 | + }, |
| 66 | + "results": [ |
| 67 | + { |
| 68 | + "correlationGuid": "a90240a2-8d09-47eb-a1c5-0af9d5b225c9", |
| 69 | + "level": "error", |
| 70 | + "locations": [ |
| 71 | + { |
| 72 | + "physicalLocation": { |
| 73 | + "artifactLocation": { |
| 74 | + "index": 1, |
| 75 | + "uri": "code.py" |
| 76 | + }, |
| 77 | + "region": { |
| 78 | + "endColumn": 35, |
| 79 | + "endLine": 9, |
| 80 | + "startColumn": 18, |
| 81 | + "startLine": 9 |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + ], |
| 86 | + "message": { |
| 87 | + "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." |
| 88 | + }, |
| 89 | + "partialFingerprints": { |
| 90 | + "primaryLocationLineHash": "b897622e8906ac69:1" |
| 91 | + }, |
| 92 | + "properties": { |
| 93 | + "github/alertNumber": 2, |
| 94 | + "github/alertUrl": "https://api.github.com/repos/nahsra/vulnerable-app-sample/code-scanning/alerts/2" |
| 95 | + }, |
| 96 | + "rule": { |
| 97 | + "id": "python.lang.security.dangerous-system-call.dangerous-system-call", |
| 98 | + "index": 723 |
| 99 | + }, |
| 100 | + "ruleId": "python.lang.security.dangerous-system-call.dangerous-system-call" |
| 101 | + } |
| 102 | + ] |
| 103 | + } |
| 104 | + ] |
| 105 | +} |
| 106 | +""" |
0 commit comments