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.

15.1 KB
Raw Blame
"""Exercise the installed scanner against harmless synthetic fixtures in CI."""

# allow: no-sut-import — invokes the scanner and its CI report policy.

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

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


@unittest.skipUnless(
    shutil.which("semgrep"), "Semgrep integration runs in scanner CI"
)
class ScannerTests(unittest.TestCase):
    def test_github_upload_honors_only_the_selected_source_suppression(self):
        with tempfile.TemporaryDirectory() as directory:
            root = Path(directory)
            rules = root / "rules.yml"
            rules.write_text(
                "rules:\n"
                + "".join(
                    f"  - id: probe-{severity.lower()}\n"
                    f"    languages: [python]\n    pattern: probe_{severity.lower()}()\n"
                    f"    message: Synthetic {severity} finding\n    severity: {severity}\n"
                    for severity in ("ERROR", "WARNING", "INFO")
                ),
                encoding="utf-8",
            )
            target = root / "example.py"
            target.write_text(
                "probe_error()  # nosemgrep: probe-error, reason: reviewed probe\n"
                "probe_error()\n"
                "probe_warning()\nprobe_warning()\nprobe_info()\nprobe_info()\n",
                encoding="utf-8",
            )
            report_path, sarif_path = (
                root / "report.json",
                root / "report.sarif",
            )
            upload_path = root / "github.sarif"
            scanned = subprocess.run(
                [
                    "semgrep",
                    "scan",
                    "--config",
                    str(rules),
                    "--strict",
                    "--metrics=off",
                    "--disable-version-check",
                    "--json-output",
                    str(report_path),
                    "--sarif-output",
                    str(sarif_path),
                    str(target),
                ],
                cwd=root,
                capture_output=True,
                text=True,
                timeout=90,
                check=False,
            )
            self.assertEqual(scanned.returncode, 0, scanned.stderr)
            originals = report_path.read_bytes(), sarif_path.read_bytes()
            native = json.loads(originals[1])
            self.assertEqual(len(native["runs"][0]["results"]), 6)
            checked = subprocess.run(
                [
                    sys.executable,
                    str(ROOT / ".github/scripts/check_semgrep_report.py"),
                    str(report_path),
                    str(sarif_path),
                    "--github-sarif-output",
                    str(upload_path),
                    "--source-root",
                    str(root),
                ],
                capture_output=True,
                text=True,
                timeout=10,
                check=False,
            )
            self.assertEqual(
                checked.returncode, 0, checked.stdout + checked.stderr
            )
            uploaded = json.loads(upload_path.read_text(encoding="utf-8"))
            positions = {
                (
                    result["ruleId"],
                    result["locations"][0]["physicalLocation"]["region"][
                        "startLine"
                    ],
                )
                for result in uploaded["runs"][0]["results"]
            }
            self.assertEqual(
                positions,
                {
                    ("probe-error", 2),
                    ("probe-warning", 3),
                    ("probe-warning", 4),
                    ("probe-info", 5),
                    ("probe-info", 6),
                },
            )
            self.assertEqual(len(uploaded["runs"][0]["results"]), 5)
            self.assertEqual(
                uploaded["runs"][0]["tool"], native["runs"][0]["tool"]
            )
            self.assertEqual(
                uploaded["runs"][0]["invocations"],
                native["runs"][0]["invocations"],
            )
            self.assertEqual(
                (report_path.read_bytes(), sarif_path.read_bytes()), originals
            )

    def test_other_rules_on_an_annotated_line_are_uploaded(self):
        """A reviewed annotation must not break the scan for other rules.

        Three rules match each probe_x() call. An annotation naming one or
        two of them must leave the strict scan error-free and the other
        rules' results in the upload: Semgrep 1.177.0 fails a --strict scan
        when a single-id nosemgrep comment sits on a line that another rule
        matches, which the trailing "reason:" item avoids.
        """
        with tempfile.TemporaryDirectory() as directory:
            root = Path(directory)
            rules = root / "rules.yml"
            rules.write_text(
                "rules:\n"
                + "".join(
                    f"  - id: probe-{name}\n"
                    "    languages: [python]\n    pattern: probe_x()\n"
                    f"    message: Synthetic {name} finding\n"
                    "    severity: WARNING\n"
                    for name in ("a", "b", "c")
                ),
                encoding="utf-8",
            )
            target = root / "example.py"
            report_path, sarif_path = (
                root / "report.json",
                root / "report.sarif",
            )
            upload_path = root / "github.sarif"

            def scan(code):
                target.write_text(code, encoding="utf-8")
                return subprocess.run(
                    [
                        "semgrep",
                        "scan",
                        "--config",
                        str(rules),
                        "--strict",
                        "--metrics=off",
                        "--disable-version-check",
                        "--json-output",
                        str(report_path),
                        "--sarif-output",
                        str(sarif_path),
                        str(target),
                    ],
                    cwd=root,
                    capture_output=True,
                    text=True,
                    timeout=90,
                    check=False,
                )

            # The premise: a comment with a single id fails the strict scan
            # as soon as another rule matches its line.
            scanned = scan("probe_x()  # nosemgrep: probe-a\n")
            self.assertNotEqual(scanned.returncode, 0, scanned.stderr)
            self.assertIn(
                "no corresponding rule trying",
                json.dumps(
                    json.loads(report_path.read_text(encoding="utf-8"))[
                        "errors"
                    ]
                ),
            )

            scanned = scan(
                "probe_x()  # nosemgrep: probe-a, reason: reviewed probe\n"
                "# nosemgrep: probe-a, probe-b, reason: reviewed probe\n"
                "probe_x()\n"
            )
            self.assertEqual(scanned.returncode, 0, scanned.stderr)
            report = json.loads(report_path.read_text(encoding="utf-8"))
            self.assertEqual(report["errors"], [])

            def positions(sarif, suppressed):
                return {
                    (
                        result["ruleId"],
                        result["locations"][0]["physicalLocation"]["region"][
                            "startLine"
                        ],
                    )
                    for result in sarif["runs"][0]["results"]
                    if bool(result.get("suppressions")) is suppressed
                }

            native = json.loads(sarif_path.read_text(encoding="utf-8"))
            self.assertEqual(
                positions(native, True),
                {("probe-a", 1), ("probe-a", 3), ("probe-b", 3)},
            )
            checked = subprocess.run(
                [
                    sys.executable,
                    str(ROOT / ".github/scripts/check_semgrep_report.py"),
                    str(report_path),
                    str(sarif_path),
                    "--github-sarif-output",
                    str(upload_path),
                    "--source-root",
                    str(root),
                ],
                capture_output=True,
                text=True,
                timeout=10,
                check=False,
            )
            self.assertEqual(
                checked.returncode, 0, checked.stdout + checked.stderr
            )
            uploaded = json.loads(upload_path.read_text(encoding="utf-8"))
            self.assertEqual(
                positions(uploaded, False),
                {("probe-b", 1), ("probe-c", 1), ("probe-c", 3)},
            )
            self.assertEqual(len(uploaded["runs"][0]["results"]), 3)

    def test_github_upload_rejects_unreviewed_source_suppressions(self):
        """Semgrep honours each of these; none may drop a GitHub alert."""
        with tempfile.TemporaryDirectory() as directory:
            root = Path(directory)
            rules = root / "rules.yml"
            rules.write_text(
                "rules:\n  - id: probe-error\n    languages: [python]\n"
                "    pattern: probe_error()\n    message: Synthetic finding\n"
                "    severity: ERROR\n",
                encoding="utf-8",
            )
            target = root / "example.py"
            for line in (
                "probe_error()  # nosemgrep",
                "probe_error()  # NOSEMGREP: probe-error, reason: reviewed probe",
                "probe_error()  # nosemgrep: probe-error",
                "probe_error()  # nosemgrep: probe-error -- reviewed probe",
                'probe_error() or "see nosemgrep docs"',
                "probe_error()  # NoSemantics",
            ):
                with self.subTest(line=line):
                    target.write_text(line + "\n", encoding="utf-8")
                    report_path = root / "report.json"
                    sarif_path = root / "report.sarif"
                    upload_path = root / "github.sarif"
                    scanned = subprocess.run(
                        [
                            "semgrep",
                            "scan",
                            "--config",
                            str(rules),
                            "--strict",
                            "--metrics=off",
                            "--disable-version-check",
                            "--json-output",
                            str(report_path),
                            "--sarif-output",
                            str(sarif_path),
                            str(target),
                        ],
                        cwd=root,
                        capture_output=True,
                        text=True,
                        timeout=90,
                        check=False,
                    )
                    self.assertEqual(scanned.returncode, 0, scanned.stderr)
                    native = json.loads(sarif_path.read_text(encoding="utf-8"))
                    # The premise: Semgrep itself suppressed the finding.
                    self.assertEqual(
                        [
                            r.get("suppressions")
                            for r in native["runs"][0]["results"]
                        ],
                        [[{"kind": "inSource"}]],
                    )
                    checked = subprocess.run(
                        [
                            sys.executable,
                            str(
                                ROOT / ".github/scripts/check_semgrep_report.py"
                            ),
                            str(report_path),
                            str(sarif_path),
                            "--github-sarif-output",
                            str(upload_path),
                            "--source-root",
                            str(root),
                        ],
                        capture_output=True,
                        text=True,
                        timeout=10,
                        check=False,
                    )
                    self.assertEqual(checked.returncode, 1, checked.stdout)
                    self.assertIn("no reviewed", checked.stdout)
                    self.assertFalse(upload_path.exists())

    def test_native_reports_preserve_all_severities_and_clean_control(self):
        with tempfile.TemporaryDirectory() as directory:
            root = Path(directory)
            rules = root / "rules.yml"
            rules.write_text(
                "rules:\n"
                + "".join(
                    f"  - id: probe-{severity.lower()}\n"
                    f"    languages: [python]\n    pattern: probe_{severity.lower()}()\n"
                    f"    message: Synthetic {severity} finding\n    severity: {severity}\n"
                    "    metadata:\n      category: security\n"
                    for severity in ("ERROR", "WARNING", "INFO")
                ),
                encoding="utf-8",
            )
            target = root / "example.py"
            for code, expected in (
                (
                    "probe_error()\nprobe_warning()\nprobe_info()\n",
                    {"ERROR", "WARNING", "INFO"},
                ),
                ("value = 1\n", set()),
            ):
                target.write_text(code, encoding="utf-8")
                result = subprocess.run(
                    [
                        "semgrep",
                        "scan",
                        "--config",
                        str(rules),
                        "--strict",
                        "--metrics=off",
                        "--disable-version-check",
                        "--json-output",
                        str(root / "report.json"),
                        "--sarif-output",
                        str(root / "report.sarif"),
                        str(target),
                    ],
                    cwd=root,
                    capture_output=True,
                    text=True,
                    timeout=90,
                    check=False,
                )
                self.assertEqual(result.returncode, 0, result.stderr)
                report = json.loads(
                    (root / "report.json").read_text(encoding="utf-8")
                )
                self.assertEqual(
                    {r["extra"]["severity"] for r in report["results"]},
                    expected,
                )
                checked = subprocess.run(
                    [
                        sys.executable,
                        str(ROOT / ".github/scripts/check_semgrep_report.py"),
                        str(root / "report.json"),
                        str(root / "report.sarif"),
                    ],
                    capture_output=True,
                    text=True,
                    timeout=10,
                    check=False,
                )
                self.assertEqual(
                    checked.returncode, 0, checked.stdout + checked.stderr
                )


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