|
| 1 | +"""Acceptance tests for issue #258: a summary keeps nothing of the blocks constant |
| 2 | +propagation proves unreachable. |
| 3 | +
|
| 4 | +A project function's summary carries its sinks, its return, the arguments it mutates |
| 5 | +and the values merged at a join to its callers. A block that can never run contributes |
| 6 | +none of them: no flow is reported at a caller for code under ``if False:`` or |
| 7 | +``while False:``, through one or several functions. A block that may run is kept, |
| 8 | +whatever decides it at run time: a parameter, or a module-level name the analysis of |
| 9 | +one function does not resolve. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +from coretrace_python import engine |
| 17 | +from coretrace_python.source import SourceManager |
| 18 | + |
| 19 | + |
| 20 | +def injections(body: str) -> list[tuple[int, str]]: |
| 21 | + text = "import os\n\n\n" + body |
| 22 | + findings = engine.check(SourceManager().add_source("app.py", text), [engine.BUNDLED_PLUGINS]) |
| 23 | + return [(f.span.start_line, f.metadata["verdict"]) for f in findings if f.rule_id == "command-injection"] |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.parametrize( |
| 27 | + "body", |
| 28 | + [ |
| 29 | + # A sink. |
| 30 | + "def helper(cmd):\n if False:\n os.system(cmd)\n\n\ndef caller():\n helper(input())\n", |
| 31 | + "def helper(cmd):\n while False:\n os.system(cmd)\n\n\ndef caller():\n helper(input())\n", |
| 32 | + # A return. |
| 33 | + "def helper(cmd):\n if False:\n return cmd\n return 'ls'\n\n\ndef caller():\n os.system(helper(input()))\n", |
| 34 | + # A value merged at a join from the block that cannot run. |
| 35 | + ( |
| 36 | + "def helper(cmd):\n result = 'ls'\n if False:\n result = cmd\n return result\n\n\n" |
| 37 | + "def caller():\n os.system(helper(input()))\n" |
| 38 | + ), |
| 39 | + # A mutated argument. |
| 40 | + ( |
| 41 | + "def helper(items, cmd):\n if False:\n items.append(cmd)\n\n\n" |
| 42 | + "def caller():\n items = []\n helper(items, input())\n os.system(items[0])\n" |
| 43 | + ), |
| 44 | + # Through two functions. |
| 45 | + ( |
| 46 | + "def inner(cmd):\n if False:\n os.system(cmd)\n\n\ndef outer(cmd):\n inner(cmd)\n\n\n" |
| 47 | + "def caller():\n outer(input())\n" |
| 48 | + ), |
| 49 | + ], |
| 50 | +) |
| 51 | +def test_nothing_of_a_block_that_cannot_run_reaches_the_callers(body: str) -> None: |
| 52 | + assert injections(body) == [] |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.parametrize( |
| 56 | + ("body", "line"), |
| 57 | + [ |
| 58 | + # Decided at run time by a parameter. |
| 59 | + ("def helper(cmd, flag):\n if flag:\n os.system(cmd)\n\n\ndef caller(flag):\n helper(input(), flag)\n", 10), |
| 60 | + # A module-level name, not resolved within the function. |
| 61 | + ("DEBUG = False\n\n\ndef helper(cmd):\n if DEBUG:\n os.system(cmd)\n\n\ndef caller():\n helper(input())\n", 13), |
| 62 | + # The same effects in blocks that run. |
| 63 | + ("def helper(cmd):\n if True:\n os.system(cmd)\n\n\ndef caller():\n helper(input())\n", 10), |
| 64 | + ( |
| 65 | + ( |
| 66 | + "def helper(cmd):\n result = 'ls'\n if True:\n result = cmd\n return result\n\n\n" |
| 67 | + "def caller():\n os.system(helper(input()))\n" |
| 68 | + ), |
| 69 | + 12, |
| 70 | + ), |
| 71 | + ( |
| 72 | + ( |
| 73 | + "def helper(items, cmd):\n if True:\n items.append(cmd)\n\n\n" |
| 74 | + "def caller():\n items = []\n helper(items, input())\n os.system(items[0])\n" |
| 75 | + ), |
| 76 | + 12, |
| 77 | + ), |
| 78 | + ], |
| 79 | +) |
| 80 | +def test_a_block_that_may_run_keeps_its_effects(body: str, line: int) -> None: |
| 81 | + assert injections(body) == [(line, "vulnerability")] |
0 commit comments