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.

8.9 KB
Raw Blame
"""Tests for the check-nosemgrep-annotations pre-commit guard.

Semgrep honours a case-insensitive ``nosem`` marker anywhere after a space
(strings and prose included); a rule-less one hides every rule on its line,
and the GitHub upload drops source-suppressed findings. The guard accepts
only ``nosemgrep: <rule.id>[, <rule.id>]..., reason: <reason>`` at the start
of a comment.
"""

# allow: no-sut-import — the SUT is a pre-commit hook script under
# .pre-commit-hooks/, not a local_deep_research module; it is loaded via
# importlib below.

import importlib.util
import re
import subprocess
from pathlib import Path

import pytest
import yaml

_ROOT = Path(__file__).resolve().parents[2]
_HOOK = _ROOT / ".pre-commit-hooks" / "check-nosemgrep-annotations.py"


def _load():
    spec = importlib.util.spec_from_file_location(
        "check_nosemgrep_annotations", _HOOK
    )
    mod = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(mod)
    return mod


guard = _load()

RULE = "semgrep.rules.weak-random-generation"
OTHER = "python.lang.security.audit.other-rule"

VALID_LINES = [
    f"jitter = random.randint(0, 5)  # nosemgrep: {RULE}, reason: jitter only",
    f"    # nosemgrep: {RULE}, reason: jitter only",
    f"x = {{{{ v|tojson }}}}; // nosemgrep: {RULE}, reason: tojson escapes",
    f"<b {{{{ a }}}}> {{# nosemgrep: {RULE}, reason: fixed tokens only #}}",
    f"<b> <!-- nosemgrep: {RULE}, reason: fixed tokens only -->",
    f"x(); /* nosemgrep: {RULE}, reason: reviewed */",
    f"run(q)  # nosemgrep: {RULE}, {OTHER}, reason: both reviewed",
    f"    # nosemgrep: {OTHER}, {RULE}, reason: both reviewed",
    # Mentions Semgrep cannot read as a marker (no space or code before it).
    "see the xnosemgrep option",
    "value = 1",
]

INVALID_LINES = [
    "run(q)  # nosemgrep",
    "run(q)  # nosem",
    f"run(q)  # nosemgrep: {RULE}",
    f"run(q)  # nosemgrep: {RULE}, reason:",
    f"run(q)  # nosemgrep: {RULE}, reason: 42",
    f"run(q)  # NOSEMGREP: {RULE}, reason: reviewed",
    f"run(q)  # NoSemgrep: {RULE}, reason: reviewed",
    f"run(q)  # nosem: {RULE}, reason: reviewed",
    f"run(q)  # nosemgrep={RULE}, reason: reviewed",
    f"run(q)  # nosemgrep:{RULE}, reason: reviewed",
    f"run(q)  # nosemgrep: {RULE},reason: reviewed",
    f"run(q)  # nosemgrep: {RULE},  reason: reviewed",
    f"run(q)  # nosemgrep: {RULE} reason: reviewed",
    f"run(q)  # nosemgrep: {RULE}, reason: reviewed, other",
    f"run(q)  # nosemgrep: {RULE}, reason: reviewed  # nosemgrep",
    # The single-id form fails a strict scan when another rule matches.
    f"run(q)  # nosemgrep: {RULE} -- reviewed",
    f"run(q)  # nosemgrep: {RULE}, {OTHER} -- reviewed",
    "run(q)  # nosemgrep: weak-random-generation, reason: suffix ids",
    f"run(q)  # nosemgrep: {RULE}, short-id, reason: suffix ids",
    f"run(q)  # reason; nosemgrep: {RULE}, reason: reviewed",
    f"<b> nosemgrep: {RULE}, reason: reviewed",
    "# TODO: NOSEM later",
    "    nosemgrep at the start of a docstring line",
    "run(q)  # NoSemantics here",
    'raise ValueError("see nosemgrep docs")',
]


@pytest.mark.parametrize("line", VALID_LINES)
def test_valid_lines_pass(line):
    assert guard.check_line(line) is None


@pytest.mark.parametrize("line", INVALID_LINES)
def test_invalid_lines_flagged(line):
    assert guard.check_line(line) is not None


@pytest.mark.parametrize("line", VALID_LINES)
def test_accepted_annotations_name_the_rule_for_the_report_check(line):
    """The upload check reads the same annotation the hook accepted."""
    if "nosemgrep:" not in line:
        return
    report = guard._REPORT
    rules = report.reviewed_annotation_rules(
        line, previous_line=False
    ) + report.reviewed_annotation_rules(line, previous_line=True)
    assert RULE in rules


# Semgrep 1.177.0's parsing (src/reporting/Nosemgrep.ml): the ids after the
# marker are split at commas, each item is stripped of spaces and quotes and
# its first space-separated word is the id. Rule_ID.validate allows only
# [a-zA-Z0-9._-]; an invalid id suppresses nothing.
_SEMGREP_IDS = r"(?:[:=][\s]?(?P<ids>([^,\s](?:[,\s]+)?)+))?"
_SEMGREP_INLINE = re.compile(r" nosem(?:grep)?" + _SEMGREP_IDS, re.IGNORECASE)
_SEMGREP_PREVIOUS = re.compile(
    r"^[^a-zA-Z0-9]* *nosem(?:grep)?" + _SEMGREP_IDS, re.IGNORECASE
)
_SEMGREP_VALID_ID = re.compile(r"[a-zA-Z0-9._-]*")


def _semgrep_ids(line, *, previous_line):
    rex = _SEMGREP_PREVIOUS if previous_line else _SEMGREP_INLINE
    ids = []
    for match in rex.finditer(line):
        if match.group("ids") is None:
            continue  # a bare marker
        for item in match.group("ids").split(","):
            ids.append(item.strip(" ").split(" ")[0].strip('"'))
    return ids


def _annotated_lines():
    files = subprocess.run(
        ["git", "ls-files", "-z", "src"],
        cwd=_ROOT,
        capture_output=True,
        check=True,
    ).stdout.split(b"\0")
    for name in filter(None, files):
        path = _ROOT / name.decode()
        if path.is_symlink() or not path.is_file():
            continue
        data = path.read_bytes()
        if b"nosemgrep:" in data:
            yield from (
                line
                for line in data.decode("utf-8").split("\n")
                if "nosemgrep:" in line
            )


@pytest.mark.parametrize("previous_line", [False, True])
def test_semgrep_reads_the_reviewed_rules_plus_an_inert_item(previous_line):
    """Semgrep sees exactly the reviewed rules and one invalid extra id.

    With a single id, Semgrep 1.177.0 fails a --strict scan whenever another
    rule matches the annotated line ("found 'nosem' comment with id ...,
    but no corresponding rule trying ..."); it skips that check when a
    comment holds two or more ids. The trailing "reason:" item is the extra
    id, and it cannot match any rule because it is not a valid rule id.
    """
    report = guard._REPORT
    lines = [line for line in VALID_LINES if "nosemgrep:" in line]
    lines += list(_annotated_lines())
    checked = 0
    for line in lines:
        rules = report.reviewed_annotation_rules(
            line, previous_line=previous_line
        )
        if not rules:
            continue
        checked += 1
        ids = _semgrep_ids(line, previous_line=previous_line)
        assert ids == [*rules, "reason:"], line
        assert len(ids) >= 2
        assert all(_SEMGREP_VALID_ID.fullmatch(rule) for rule in rules)
        assert not _SEMGREP_VALID_ID.fullmatch(ids[-1])
    assert checked > 0


def test_python_string_literal_annotation_flagged():
    src = f'x = "# nosemgrep: {RULE}, reason: reviewed"\n'
    errors = guard.check_content(src, python=True)
    assert len(errors) == 1
    assert "string" in errors[0][1]


def test_python_docstring_annotation_flagged():
    src = f'def f():\n    """Doc.\n\n    # nosemgrep: {RULE}, reason: reviewed\n    """\n'
    errors = guard.check_content(src, python=True)
    assert [row for row, _ in errors] == [4]


def test_python_comment_annotation_passes():
    src = (
        f"# nosemgrep: {RULE}, reason: jitter only\n"
        "delay = random.randint(1, 30)\n"
        f"x = random.random()  # nosemgrep: {RULE}, reason: jitter only\n"
    )
    assert guard.check_content(src, python=True) == []


def test_lines_split_like_semgrep():
    # U+2028 is not a line break for Semgrep; the marker is on line 1.
    src = "x = 1
y  # nosemgrep\n"
    assert [row for row, _ in guard.check_content(src, python=False)] == [1]


def test_main_reports_failures(tmp_path, capsys):
    bad = tmp_path / "bad.html"
    bad.write_text("<b {{ a }}> {# nosemgrep #}\n", encoding="utf-8")
    good = tmp_path / "good.py"
    good.write_text(f"# nosemgrep: {RULE}, reason: jitter only\nx = 1\n")
    assert guard.main([str(good)]) == 0
    assert guard.main([str(good), str(bad)]) == 1
    assert "bad.html" in capsys.readouterr().out


def test_repository_annotations_pass():
    files = subprocess.run(
        ["git", "ls-files", "-z", "src"],
        cwd=_ROOT,
        capture_output=True,
        check=True,
    ).stdout.split(b"\0")
    annotated = 0
    for name in filter(None, files):
        path = _ROOT / name.decode()
        if path.is_symlink() or not path.is_file():
            continue
        assert guard.check_file(str(path)) == [], path
        if b"nosemgrep:" in path.read_bytes():
            annotated += 1
    # The sweep is not vacuous: the reviewed annotations were checked.
    assert annotated > 0


def test_hook_is_registered_for_the_scanned_tree():
    config = yaml.safe_load(
        (_ROOT / ".pre-commit-config.yaml").read_text(encoding="utf-8")
    )
    hooks = {
        hook["id"]: hook for repo in config["repos"] for hook in repo["hooks"]
    }
    hook = hooks["check-nosemgrep-annotations"]
    assert hook["entry"] == ".pre-commit-hooks/check-nosemgrep-annotations.py"
    # Semgrep scans every file under src/, templates and static included.
    assert hook["files"] == "^src/"
    assert "types" not in hook or hook["types"] == ["text"]