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.

6.5 KB
Raw Blame
#!/usr/bin/env python3
"""Pre-commit guard: Semgrep ``nosemgrep`` annotations must be reviewed and
rule-specific.

Semgrep 1.177.0 honours a case-insensitive ``nosem``/``nosemgrep`` marker
after a space anywhere on a finding's line (string literals, prose and words
such as "NoSemantics" included) or at the start of the line above it. Without
a rule id, a marker hides EVERY rule on that line. The GitHub upload omits
source-suppressed findings, so such a marker silently removes alerts from
code scanning and from the release gate.

This hook fails the commit unless every marker Semgrep would honour in a
scanned file (``src/``) is the only marker on its line and is written exactly
as::

    # nosemgrep: <rule.id>[, <rule.id>]..., reason: <reason>

(``//``, ``{#``, ``<!--`` or ``/*`` instead of ``#`` outside Python). Each
rule id must be namespaced (contain a dot), because Semgrep also accepts any
suffix of a rule id. The reason must not contain a comma: Semgrep reads each
comma-separated item after the colon as another rule id. The ``reason:``
item is not a valid rule id, so it suppresses nothing, but it keeps the
comment at two or more items; Semgrep 1.177.0 fails a ``--strict`` scan when
a single-id comment sits on a line that another rule also matches.

Python files are tokenized, so a marker inside a string literal or docstring
is rejected even when it is well-formed. Other files only need the comment
opener directly before the marker; a well-formed annotation embedded in a
string there is not detected, but it still names its rules and a reason.

The grammar is shared with ``.github/scripts/check_semgrep_report.py``,
which also rejects, at upload time, any source-suppressed finding whose
line lacks a reviewed annotation naming its rule.
"""

from __future__ import annotations

import importlib.util
import io
import sys
import tokenize
from pathlib import Path

_REPORT_SCRIPT = (
    Path(__file__).resolve().parents[1]
    / ".github"
    / "scripts"
    / "check_semgrep_report.py"
)
_SPEC = importlib.util.spec_from_file_location(
    "check_semgrep_report", _REPORT_SCRIPT
)
_REPORT = importlib.util.module_from_spec(_SPEC)
_SPEC.loader.exec_module(_REPORT)

# A marker must start a comment: the opener, one space, then the marker.
_COMMENT_OPENERS = ("# ", "// ", "{# ", "{#- ", "<!-- ", "/* ")

_FORM = "write it as `nosemgrep: <rule.id>[, <rule.id>]..., reason: <reason>`"
_MULTIPLE_MSG = (
    "more than one nosem marker on one line; Semgrep reads each one, so keep "
    "a single reviewed annotation per line"
)
_CASE_MSG = (
    "Semgrep matches `nosem`/`nosemgrep` in any case, so this text acts as a "
    f"suppression; {_FORM} (lowercase) or reword it"
)
_FORM_MSG = (
    "a bare, rule-less or reasonless nosemgrep, or one without the "
    "`reason:` item, hides rules without review or fails a strict scan; "
    f"{_FORM}, with no comma in the reason"
)
_NAMESPACE_MSG = (
    "use fully qualified rule ids: Semgrep also accepts any suffix of a "
    "rule id, so a short id can hide other rules"
)
_COMMENT_MSG = (
    "the marker must start a comment (`# `, `// `, `{# `, `<!-- ` or `/* ` "
    "directly before it); Semgrep also honours text in strings and code"
)
_STRING_MSG = (
    "nosem text inside a Python string or docstring still suppresses "
    "Semgrep findings; move the annotation into a `#` comment or reword it"
)


def _python_comment_spans(content: str) -> list[tuple[int, int, int]] | None:
    """(row, start_col, end_col) of each comment, or None if untokenizable."""
    try:
        tokens = tokenize.generate_tokens(io.StringIO(content).readline)
        return [
            (tok.start[0], tok.start[1], tok.end[1])
            for tok in tokens
            if tok.type == tokenize.COMMENT
        ]
    except (tokenize.TokenError, IndentationError, SyntaxError):
        return None


def check_line(line: str) -> str | None:
    """The violation on one line, or None if it has no marker or is valid."""
    offsets = sorted(
        set(_REPORT.nosem_markers(line, previous_line=False))
        | set(_REPORT.nosem_markers(line, previous_line=True))
    )
    if not offsets:
        return None
    if len(offsets) > 1:
        return _MULTIPLE_MSG
    offset = offsets[0]
    rest = line[offset:].rstrip("\r")
    if not rest.startswith("nosemgrep"):
        return _CASE_MSG
    match = _REPORT.REVIEWED_ANNOTATION.fullmatch(rest)
    if match is None:
        return _FORM_MSG
    if any("." not in rule for rule in match.group("rules").split(", ")):
        return _NAMESPACE_MSG
    if not line[:offset].endswith(_COMMENT_OPENERS):
        return _COMMENT_MSG
    return None


def check_content(content: str, *, python: bool) -> list[tuple[int, str]]:
    errors: list[tuple[int, str]] = []
    # Semgrep splits lines at "\n" only.
    lines = content.split("\n")
    comments = _python_comment_spans(content) if python else None
    for row, line in enumerate(lines, start=1):
        msg = check_line(line)
        if msg is None and python and comments is not None:
            offsets = _REPORT.nosem_markers(
                line, previous_line=False
            ) + _REPORT.nosem_markers(line, previous_line=True)
            if offsets and not any(
                r == row and start <= offsets[0] < end
                for r, start, end in comments
            ):
                msg = _STRING_MSG
        if msg is not None:
            errors.append((row, msg))
    return errors


def check_file(filename: str) -> list[tuple[int, str]]:
    try:
        content = (
            Path(filename)
            .read_bytes()
            .decode("utf-8", errors="surrogateescape")
        )
    except OSError:
        return []
    # A UTF-8 BOM is part of Semgrep's first line but not of a comment token.
    python = filename.endswith(".py")
    if python and content.startswith("\ufeff"):
        content = content[1:]
    return check_content(content, python=python)


def main(argv: list[str]) -> int:
    failed = False
    for filename in argv:
        errors = check_file(filename)
        if errors:
            failed = True
            print(f"\n{filename}:")
            for line_num, msg in sorted(errors):
                print(f"  Line {line_num}: {msg}")
    if failed:
        print(
            "\nUnreviewed Semgrep suppression(s). Each nosemgrep annotation must "
            "name fully\nqualified rules and give a reason, e.g.:\n"
            "       # nosemgrep: semgrep.rules.weak-random-generation, "
            "reason: jitter only\n"
            "See .semgrep/rules/README.md."
        )
        return 1
    return 0


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))