Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

5.5 KB
Raw Blame
"""Standalone policy checks for complete native Bandit reports."""

# allow: no-sut-import — imports only the standalone report policy.

import copy
import importlib.util
from pathlib import Path
import tempfile
import unittest

ROOT = Path(__file__).resolve().parents[3]
SPEC = importlib.util.spec_from_file_location(
    "bandit_policy", ROOT / ".github/scripts/check_bandit_report.py"
)
POLICY = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(POLICY)


class ReportTests(unittest.TestCase):
    def setUp(self):
        self.directory = tempfile.TemporaryDirectory()
        self.addCleanup(self.directory.cleanup)
        self.source = Path(self.directory.name)
        self.filename = self.source / "probe %.py"
        self.filename.write_text("value = 1\n", encoding="utf-8")
        metrics = {"_totals": {"loc": 1}, str(self.filename): {"loc": 1}}
        self.report = {"errors": [], "results": [], "metrics": metrics}
        self.sarif = {
            "version": "2.1.0",
            "runs": [
                {
                    "tool": {"driver": {"name": "Bandit", "version": "1.9.4"}},
                    "results": [],
                    "invocations": [{"executionSuccessful": True}],
                    "properties": {"metrics": copy.deepcopy(metrics)},
                }
            ],
        }

    def check(self):
        return POLICY.validate(self.report, self.sarif, self.source)

    def finding(self, severity="MEDIUM"):
        self.report["results"] = [
            {
                "test_id": "B108",
                "filename": str(self.filename),
                "line_number": 1,
                "line_range": [1],
                "issue_severity": severity,
            }
        ]
        self.sarif["runs"][0]["tool"]["driver"]["rules"] = [{"id": "B108"}]
        self.sarif["runs"][0]["results"] = [
            {
                "ruleId": "B108",
                "level": POLICY.LEVELS.get(severity, "note"),
                "properties": {"issue_severity": severity},
                "locations": [
                    {
                        "physicalLocation": {
                            "artifactLocation": {"uri": self.filename.as_uri()},
                            "region": {"startLine": 1},
                        }
                    }
                ],
            }
        ]

    def test_clean_scan_and_native_uri_encoding(self):
        self.assertEqual(self.check()[0], 1)
        self.finding()
        self.assertEqual(self.check()[1], {"MEDIUM": 1})

    def test_native_medium_level_uses_sarif_warning_default(self):
        self.finding()
        self.sarif["runs"][0]["results"][0].pop("level")
        self.assertEqual(self.check()[1], {"MEDIUM": 1})

    def test_errors_and_missing_error_status_fail(self):
        for value in (None, [{"reason": "syntax error"}]):
            with self.subTest(value=value):
                self.report["errors"] = value
                with self.assertRaisesRegex(ValueError, "scan errors"):
                    self.check()

    def test_unscanned_source_file_fails(self):
        (self.source / "unscanned.py").write_text(
            "value = 2\n", encoding="utf-8"
        )
        with self.assertRaisesRegex(ValueError, "every target"):
            self.check()

    def test_no_source_metrics_fail(self):
        self.report["metrics"] = {"_totals": {"loc": 0}}
        with self.assertRaisesRegex(ValueError, "any source code"):
            self.check()

    def test_reports_must_cover_same_source(self):
        self.sarif["runs"][0]["properties"]["metrics"]["_totals"]["loc"] = 2
        with self.assertRaisesRegex(ValueError, "different source"):
            self.check()

    def test_dropped_or_duplicated_sarif_findings_fail(self):
        self.finding()
        rows = self.sarif["runs"][0]["results"]
        for changes in ([], rows * 2):
            with self.subTest(rows=len(changes)):
                self.sarif["runs"][0]["results"] = changes
                with self.assertRaisesRegex(ValueError, "findings differ"):
                    self.check()

    def test_sarif_level_cannot_downgrade_medium_finding(self):
        self.finding()
        self.sarif["runs"][0]["results"][0]["level"] = "note"
        with self.assertRaisesRegex(ValueError, "inconsistent severity"):
            self.check()

    def test_failed_execution_and_error_notifications_fail(self):
        run = self.sarif["runs"][0]
        for invocation in (
            {},
            {"executionSuccessful": False},
            {
                "executionSuccessful": True,
                "toolConfigurationNotifications": [{"level": "error"}],
            },
            {
                "executionSuccessful": True,
                "toolExecutionNotifications": [{"level": "error"}],
            },
        ):
            with self.subTest(invocation=invocation):
                run["invocations"] = [invocation]
                with self.assertRaises(ValueError):
                    self.check()

    def test_finding_requires_rule_and_scanned_local_location(self):
        self.finding()
        run = self.sarif["runs"][0]
        run["tool"]["driver"]["rules"] = []
        with self.assertRaisesRegex(ValueError, "rule descriptor"):
            self.check()
        self.finding()
        location = run["results"][0]["locations"][0]["physicalLocation"]
        location["artifactLocation"]["uri"] = "https://example.invalid/probe.py"
        with self.assertRaisesRegex(ValueError, "non-local"):
            self.check()


if __name__ == "__main__":
    unittest.main()