|
| 1 | +"""Acceptance tests for issue #243: vendored code is still analysed, and its findings say |
| 2 | +where they come from. |
| 3 | +
|
| 4 | +Code a project keeps under a ``third_party``, ``third-party``, ``vendor`` or ``vendored`` |
| 5 | +directory is someone else's: its findings carry ``origin: vendored`` in their metadata, |
| 6 | +in the JSON report and in SARIF. The origin is told apart from the context of #244: a |
| 7 | +vendored project's tests carry both ``context: test`` and ``origin: vendored``, so a |
| 8 | +reader and a refiner still see why they are test code. Nothing is excluded: the |
| 9 | +findings and the flows through vendored modules stay as they were. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import json |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from coretrace_python import engine |
| 20 | +from coretrace_python.findings import Confidence, Finding |
| 21 | +from coretrace_python.findings.context import code_context, code_origin |
| 22 | +from coretrace_python.reporters.sarif import render_sarif |
| 23 | + |
| 24 | +XML_TEST = """\ |
| 25 | +from xml.dom import minidom |
| 26 | +
|
| 27 | +PASSWORD = "Zx81kQpLw0RtY7vB" |
| 28 | +EXPECTED = "<testsuites><testsuite name='Filter'/></testsuites>" |
| 29 | +
|
| 30 | +
|
| 31 | +def expected_document(): |
| 32 | + return minidom.parseString(EXPECTED) |
| 33 | +""" |
| 34 | + |
| 35 | +PARSER = "from xml.dom import minidom\n\n\ndef parse(document):\n return minidom.parseString(document)\n" |
| 36 | +RUNNER = "import os\n\n\ndef run(cmd):\n os.system(cmd)\n" |
| 37 | +VIEWS = ( |
| 38 | + "from flask import request\n\nfrom third_party.tools.runner import run\n\n\n" |
| 39 | + "def view():\n run(request.args['c'])\n" |
| 40 | +) |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.parametrize( |
| 44 | + ("path", "origin", "context"), |
| 45 | + [ |
| 46 | + ("third_party/gtest/test/xml_output_test.py", "vendored", "test"), |
| 47 | + ("third-party/lib/parse.py", "vendored", None), |
| 48 | + ("vendor/requests/api.py", "vendored", None), |
| 49 | + ("src/vendored/six.py", "vendored", None), |
| 50 | + ("vendor/lib/tests/test_api.py", "vendored", "test"), |
| 51 | + ("app/vendor_api.py", None, None), |
| 52 | + ("external/client.py", None, None), |
| 53 | + ("tests/test_app.py", None, "test"), |
| 54 | + ], |
| 55 | +) |
| 56 | +def test_origin_and_context_are_told_apart(path: str, origin: str | None, context: str | None) -> None: |
| 57 | + assert (code_origin(path), code_context(path)) == (origin, context) |
| 58 | + |
| 59 | + |
| 60 | +def write(root: Path, files: dict[str, str]) -> Path: |
| 61 | + for name, text in files.items(): |
| 62 | + (root / name).parent.mkdir(parents=True, exist_ok=True) |
| 63 | + (root / name).write_text(text, encoding="utf-8") |
| 64 | + return root |
| 65 | + |
| 66 | + |
| 67 | +def located(findings: tuple[Finding, ...], name: str) -> dict[str, Finding]: |
| 68 | + return {f.rule_id: f for f in findings if Path(str(f.span.source_id)).as_posix().endswith(name)} |
| 69 | + |
| 70 | + |
| 71 | +def situation(finding: Finding) -> tuple[str | None, str | None]: |
| 72 | + return finding.metadata.get("context"), finding.metadata.get("origin") |
| 73 | + |
| 74 | + |
| 75 | +def test_vendored_findings_are_kept_and_carry_both_facts(tmp_path: Path) -> None: |
| 76 | + root = write( |
| 77 | + tmp_path / "project", |
| 78 | + { |
| 79 | + "third_party/gtest/test/xml_output_test.py": XML_TEST, |
| 80 | + "vendor/lib/vendored_output.py": XML_TEST, |
| 81 | + "app/own_output.py": XML_TEST, |
| 82 | + }, |
| 83 | + ) |
| 84 | + |
| 85 | + findings = engine.analyze_project(root, [engine.BUNDLED_PLUGINS]).findings |
| 86 | + vendored_test = located(findings, "third_party/gtest/test/xml_output_test.py") |
| 87 | + vendored = located(findings, "vendor/lib/vendored_output.py") |
| 88 | + own = located(findings, "app/own_output.py") |
| 89 | + |
| 90 | + assert set(vendored_test) == set(vendored) == set(own) == {"hardcoded-credential", "unsafe-xml"} |
| 91 | + assert {rule: situation(f) for rule, f in vendored_test.items()} == dict.fromkeys(vendored_test, ("test", "vendored")) |
| 92 | + assert {rule: situation(f) for rule, f in vendored.items()} == dict.fromkeys(vendored, (None, "vendored")) |
| 93 | + assert {rule: situation(f) for rule, f in own.items()} == dict.fromkeys(own, (None, None)) |
| 94 | + # The origin changes no verdict: the credential is low as test code, medium otherwise. |
| 95 | + assert vendored_test["hardcoded-credential"].confidence is Confidence.LOW |
| 96 | + assert vendored["hardcoded-credential"].confidence is Confidence.MEDIUM |
| 97 | + assert vendored["unsafe-xml"].confidence == own["unsafe-xml"].confidence |
| 98 | + |
| 99 | + |
| 100 | +def test_a_flow_through_a_vendored_module_is_still_reported(tmp_path: Path) -> None: |
| 101 | + root = write( |
| 102 | + tmp_path / "project", |
| 103 | + { |
| 104 | + "third_party/__init__.py": "", |
| 105 | + "third_party/tools/__init__.py": "", |
| 106 | + "third_party/tools/runner.py": RUNNER, |
| 107 | + "app/__init__.py": "", |
| 108 | + "app/views.py": VIEWS, |
| 109 | + }, |
| 110 | + ) |
| 111 | + |
| 112 | + findings = engine.analyze_project(root, [engine.BUNDLED_PLUGINS]).findings |
| 113 | + flows = [f for f in findings if f.rule_id == "command-injection"] |
| 114 | + |
| 115 | + assert [(Path(str(f.span.source_id)).name, f.span.start_line) for f in flows] == [("views.py", 7)] |
| 116 | + assert situation(flows[0]) == (None, None) |
| 117 | + |
| 118 | + |
| 119 | +def test_the_sarif_result_carries_both_facts(tmp_path: Path) -> None: |
| 120 | + root = write(tmp_path / "project", {"vendor/lib/tests/test_output.py": XML_TEST}) |
| 121 | + analysis = engine.analyze_project(root, [engine.BUNDLED_PLUGINS]) |
| 122 | + |
| 123 | + results = json.loads(render_sarif(engine.report(analysis.findings, root=root)))["runs"][0]["results"] |
| 124 | + |
| 125 | + assert {r["ruleId"]: r["properties"] for r in results} == { |
| 126 | + "hardcoded-credential": {"context": "test", "origin": "vendored"}, |
| 127 | + "unsafe-xml": {"context": "test", "origin": "vendored"}, |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | +REFINER_MANIFEST = ( |
| 132 | + 'name = "test-confidence"\nversion = "1.0.0"\nplugin_api = ">=1,<2"\nrequires = []\n' |
| 133 | + 'provides = []\n\n[entrypoint]\nmodule = "refiner"\nclass = "TestConfidence"\n' |
| 134 | +) |
| 135 | +REFINER = ''' |
| 136 | +from dataclasses import replace |
| 137 | +from typing import ClassVar |
| 138 | +
|
| 139 | +from coretrace_python.findings import Confidence |
| 140 | +from coretrace_python.plugins import ProjectPlugin |
| 141 | +
|
| 142 | +
|
| 143 | +class TestConfidence(ProjectPlugin): |
| 144 | + name: ClassVar[str] = "test-confidence" |
| 145 | +
|
| 146 | + def analyze_project(self, ctx): |
| 147 | + return () |
| 148 | +
|
| 149 | + def refine(self, ctx, findings): |
| 150 | + return [ |
| 151 | + replace(f, confidence=Confidence.LOW) if f.metadata.get("context") == "test" else f |
| 152 | + for f in findings |
| 153 | + ] |
| 154 | +''' |
| 155 | + |
| 156 | + |
| 157 | +def test_a_refiner_still_recognises_vendored_test_code(tmp_path: Path) -> None: |
| 158 | + plugin = tmp_path / "plugins" / "refiner" |
| 159 | + plugin.mkdir(parents=True) |
| 160 | + (plugin / "plugin.toml").write_text(REFINER_MANIFEST, encoding="utf-8") |
| 161 | + (plugin / "refiner.py").write_text(REFINER, encoding="utf-8") |
| 162 | + root = write( |
| 163 | + tmp_path / "project", |
| 164 | + {"vendor/lib/tests/test_parser.py": PARSER, "vendor/lib/parser.py": PARSER}, |
| 165 | + ) |
| 166 | + |
| 167 | + findings = engine.analyze_project(root, [engine.BUNDLED_PLUGINS, tmp_path / "plugins"]).findings |
| 168 | + |
| 169 | + assert located(findings, "vendor/lib/tests/test_parser.py")["unsafe-xml"].confidence is Confidence.LOW |
| 170 | + assert located(findings, "vendor/lib/parser.py")["unsafe-xml"].confidence is Confidence.HIGH |
0 commit comments