|
| 1 | +import json |
| 2 | + |
| 3 | +import mock |
| 4 | + |
| 5 | +from codemodder.codemods.test import BaseSASTCodemodTest |
| 6 | +from codemodder.dependency import Security |
| 7 | +from core_codemods.sonar.sonar_sandbox_process_creation import ( |
| 8 | + SonarSandboxProcessCreation, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +class TestSonarSandboxProcessCreation(BaseSASTCodemodTest): |
| 13 | + codemod = SonarSandboxProcessCreation |
| 14 | + tool = "sonar" |
| 15 | + |
| 16 | + def test_name(self): |
| 17 | + assert self.codemod.name == "sandbox-process-creation" |
| 18 | + |
| 19 | + @mock.patch("codemodder.codemods.api.FileContext.add_dependency") |
| 20 | + def test_simple(self, adds_dependency, tmpdir): |
| 21 | + input_code = """ |
| 22 | + import os |
| 23 | + from flask import render_template, request |
| 24 | +
|
| 25 | + @app.route('/vuln', methods=['GET', 'POST']) |
| 26 | + def vuln(): |
| 27 | + output = "" |
| 28 | + if request.method == 'POST': |
| 29 | + command = request.form.get('command') |
| 30 | + output = os.popen(command).read() |
| 31 | + return render_template('vuln.html', output=output) |
| 32 | + """.lstrip( |
| 33 | + "\n" |
| 34 | + ) |
| 35 | + expected = """ |
| 36 | + import os |
| 37 | + from flask import render_template, request |
| 38 | + from security import safe_command |
| 39 | +
|
| 40 | + @app.route('/vuln', methods=['GET', 'POST']) |
| 41 | + def vuln(): |
| 42 | + output = "" |
| 43 | + if request.method == 'POST': |
| 44 | + command = request.form.get('command') |
| 45 | + output = safe_command.run(os.popen, command).read() |
| 46 | + return render_template('vuln.html', output=output) |
| 47 | + """.lstrip( |
| 48 | + "\n" |
| 49 | + ) |
| 50 | + issues = { |
| 51 | + "issues": [ |
| 52 | + { |
| 53 | + "rule": "pythonsecurity:S2076", |
| 54 | + "status": "OPEN", |
| 55 | + "component": "code.py", |
| 56 | + "textRange": { |
| 57 | + "startLine": 9, |
| 58 | + "endLine": 9, |
| 59 | + "startOffset": 17, |
| 60 | + "endOffset": 34, |
| 61 | + }, |
| 62 | + } |
| 63 | + ] |
| 64 | + } |
| 65 | + self.run_and_assert(tmpdir, input_code, expected, results=json.dumps(issues)) |
| 66 | + adds_dependency.assert_called_once_with(Security) |
0 commit comments