|
| 1 | +# SPDX-FileCopyrightText: 2025-present Maikel Mardjan(https://nocomplexity.com/) and all contributors! |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + |
| 5 | + |
| 6 | +from pathlib import Path |
| 7 | +from codeaudit.api_helpers import _codeaudit_scan_wasm |
| 8 | + |
| 9 | + |
| 10 | +def test_basic_working_scanning(): |
| 11 | + """Checks file name in dict""" |
| 12 | + current_file_directory = Path(__file__).parent |
| 13 | + |
| 14 | + # validation1.py is in a subfolder: |
| 15 | + validation_file_path = current_file_directory / "validationfiles" / "eval.py" |
| 16 | + |
| 17 | + result = _codeaudit_scan_wasm(validation_file_path, False) |
| 18 | + |
| 19 | + # actual_data = find_constructs(source, constructs) |
| 20 | + actual_data = result["file_name"] |
| 21 | + |
| 22 | + # This is the expected dictionary |
| 23 | + expected_data = "eval.py" # Assert that the actual data matches the expected data |
| 24 | + assert actual_data == expected_data |
| 25 | + |
| 26 | + |
| 27 | +def test_scan_wasm(): |
| 28 | + current_file_directory = Path(__file__).parent |
| 29 | + |
| 30 | + # validation file path |
| 31 | + validation_file_path = current_file_directory / "validationfiles" / "eval.py" |
| 32 | + |
| 33 | + result = _codeaudit_scan_wasm(str(validation_file_path), False) |
| 34 | + actual_data = {int(k): v for k, v in result["sast_result"].items()} |
| 35 | + |
| 36 | + # Expected dictionary |
| 37 | + expected_data = { |
| 38 | + 5: { |
| 39 | + "line": 5, |
| 40 | + "validation": "exec", |
| 41 | + "severity": "High", |
| 42 | + "info": "This function can execute arbitrary code and should be used only with validated constructs.", |
| 43 | + "code": "<pre><code class='language-python'>b = builtins\nb.exec("2+2") # flag 1</code></pre>", |
| 44 | + }, |
| 45 | + 8: { |
| 46 | + "line": 8, |
| 47 | + "validation": "eval", |
| 48 | + "severity": "High", |
| 49 | + "info": "This function can execute arbitrary code. Never safe with untrusted input.", |
| 50 | + "code": "<pre><code class='language-python'>x = 1\nresult = b.eval("x + 2") # flag 2\nprint(result) </code></pre>", |
| 51 | + }, |
| 52 | + 11: { |
| 53 | + "line": 11, |
| 54 | + "validation": "eval", |
| 55 | + "severity": "High", |
| 56 | + "info": "This function can execute arbitrary code. Never safe with untrusted input.", |
| 57 | + "code": "<pre><code class='language-python'>print(eval("1+1")) # flag 3\nprint(eval("os.getcwd()")) # flag 4</code></pre>", |
| 58 | + }, |
| 59 | + 12: { |
| 60 | + "line": 12, |
| 61 | + "validation": "eval", |
| 62 | + "severity": "High", |
| 63 | + "info": "This function can execute arbitrary code. Never safe with untrusted input.", |
| 64 | + "code": "<pre><code class='language-python'>print(eval("1+1")) # flag 3\nprint(eval("os.getcwd()")) # flag 4\nprint(eval("os.chmod('%s', 0777)" % 'test.txt')) # flag 5</code></pre>", |
| 65 | + }, |
| 66 | + 13: { |
| 67 | + "line": 13, |
| 68 | + "validation": "eval", |
| 69 | + "severity": "High", |
| 70 | + "info": "This function can execute arbitrary code. Never safe with untrusted input.", |
| 71 | + "code": "<pre><code class='language-python'>print(eval("os.getcwd()")) # flag 4\nprint(eval("os.chmod('%s', 0777)" % 'test.txt')) # flag 5</code></pre>", |
| 72 | + }, |
| 73 | + 21: { |
| 74 | + "line": 21, |
| 75 | + "validation": "eval", |
| 76 | + "severity": "High", |
| 77 | + "info": "This function can execute arbitrary code. Never safe with untrusted input.", |
| 78 | + "code": "<pre><code class='language-python'> def foo(self):\n self.eval() # flag 6 - but a false flag , but since builtins is imported a known issue! So #nosec</code></pre>", |
| 79 | + }, |
| 80 | + 23: { |
| 81 | + "line": 23, |
| 82 | + "validation": "eval", |
| 83 | + "severity": "High", |
| 84 | + "info": "This function can execute arbitrary code. Never safe with untrusted input.", |
| 85 | + "code": "<pre><code class='language-python'>Test().eval() # flag 7 - due to eval is in contructs list, edge case and hard to solve in a simple way (for now), so marker nosec </code></pre>", |
| 86 | + }, |
| 87 | + 26: { |
| 88 | + "line": 26, |
| 89 | + "validation": "eval", |
| 90 | + "severity": "High", |
| 91 | + "info": "This function can execute arbitrary code. Never safe with untrusted input.", |
| 92 | + "code": "<pre><code class='language-python'>eval("os.system('rm -rf /')") # flag 8</code></pre>", |
| 93 | + }, |
| 94 | + 28: { |
| 95 | + "line": 28, |
| 96 | + "validation": "eval", |
| 97 | + "severity": "High", |
| 98 | + "info": "This function can execute arbitrary code. Never safe with untrusted input.", |
| 99 | + "code": "<pre><code class='language-python'>__builtins__.eval(...) # flag 9 due to eval statement</code></pre>", |
| 100 | + }, |
| 101 | + 30: [ |
| 102 | + { |
| 103 | + "line": 30, |
| 104 | + "validation": "eval", |
| 105 | + "severity": "High", |
| 106 | + "info": "This function can execute arbitrary code. Never safe with untrusted input.", |
| 107 | + "code": "<pre><code class='language-python'>nasty = __import__("builtins").eval # flag 10 + flag 11 , flag due to __import__ and for eval!</code></pre>", |
| 108 | + }, |
| 109 | + { |
| 110 | + "line": 30, |
| 111 | + "validation": "__import__", |
| 112 | + "severity": "Medium", |
| 113 | + "info": "Importing modules dynamically can load untrusted code.", |
| 114 | + "code": "<pre><code class='language-python'>nasty = __import__("builtins").eval # flag 10 + flag 11 , flag due to __import__ and for eval!</code></pre>", |
| 115 | + }, |
| 116 | + ], |
| 117 | + } |
| 118 | + |
| 119 | + assert actual_data == expected_data |
0 commit comments