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.

4.4 KB
Raw Blame
"""Exercise native Bandit reports using harmless, never-executed source."""

# allow: no-sut-import — scans fixtures and invokes the standalone policy.

import json
from pathlib import Path
import shutil
import subprocess
import sys
import tempfile
import unittest

ROOT = Path(__file__).resolve().parents[3]


@unittest.skipUnless(shutil.which("bandit"), "Native Bandit runs in scanner CI")
class NativeScannerTests(unittest.TestCase):
    def scan(self, source):
        directory = tempfile.TemporaryDirectory()
        self.addCleanup(directory.cleanup)
        root = Path(directory.name)
        target = root / "src"
        target.mkdir()
        (target / "probe.py").write_text(source, encoding="utf-8")
        report, sarif = root / "report.json", root / "report.sarif"
        for format, path in (("json", report), ("sarif", sarif)):
            run = subprocess.run(
                [
                    "bandit",
                    "-r",
                    str(target),
                    "--severity-level",
                    "medium",
                    "--exit-zero",
                    "-f",
                    format,
                    "-o",
                    str(path),
                ],
                capture_output=True,
                text=True,
                timeout=30,
                check=False,
            )
            self.assertEqual(run.returncode, 0, run.stdout + run.stderr)
        checked = subprocess.run(
            [
                sys.executable,
                str(ROOT / ".github/scripts/check_bandit_report.py"),
                str(report),
                str(sarif),
                "--source",
                str(target),
            ],
            capture_output=True,
            text=True,
            timeout=10,
            check=False,
        )
        return json.loads(report.read_text(encoding="utf-8")), checked

    def test_medium_and_high_findings_survive_native_sarif(self):
        report, checked = self.scan(
            "import subprocess\ncommand = input()\n"
            "subprocess.run(command, shell=True)\n"
            "open('/tmp/bandit-fixture')\n"
            "hosts = (\n    '127.0.0.1',\n    '0.0.0.0',\n)\n"
        )
        self.assertEqual(checked.returncode, 0, checked.stdout + checked.stderr)
        self.assertEqual(
            {row["issue_severity"] for row in report["results"]},
            {"HIGH", "MEDIUM"},
        )
        self.assertTrue(
            {"B108", "B602"}.issubset(
                {row["test_id"] for row in report["results"]}
            )
        )

    def test_reviewed_rule_families_still_detect_unsafe_examples(self):
        report, checked = self.scan(
            "import requests\nfrom datasets import load_dataset\n"
            "from markupsafe import Markup\n"
            "name = input()\nquery = f'SELECT value FROM {name}'\n"
            "host = '0.0.0.0'\nrequests.get('https://example.invalid')\n"
            "Markup(input())\nload_dataset('fixture/data')\n"
        )
        self.assertEqual(checked.returncode, 0, checked.stdout + checked.stderr)
        rules = {row["test_id"] for row in report["results"]}
        self.assertTrue(
            {"B104", "B113", "B608", "B615", "B704"}.issubset(rules)
        )

    def test_narrow_annotation_keeps_other_rules_and_statements(self):
        report, checked = self.scan(
            "import requests\nfrom markupsafe import Markup\n"
            "requests.get('https://example.invalid', verify=False)  # nosec B113\n"
            "def render(value):\n"
            "    reviewed = Markup(value)  # nosec B704\n"
            "    return Markup(value)\n"
        )
        self.assertEqual(checked.returncode, 0, checked.stdout + checked.stderr)
        rules = [row["test_id"] for row in report["results"]]
        self.assertIn("B501", rules)
        self.assertNotIn("B113", rules)
        self.assertEqual(rules.count("B704"), 1)

    def test_complete_clean_scan_is_valid(self):
        report, checked = self.scan("value = 1\n")
        self.assertEqual(report["results"], [])
        self.assertEqual(checked.returncode, 0, checked.stdout + checked.stderr)

    def test_parse_failure_is_rejected_even_with_exit_zero(self):
        report, checked = self.scan("broken (\n")
        self.assertTrue(report["errors"])
        self.assertEqual(checked.returncode, 1)
        self.assertIn("scan errors", checked.stdout)


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