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.

20.8 KB
Raw Blame
"""Regression coverage for the CI hardcoded SECRET_KEY detector.

The production shell script must recognize direct, prefixed, suffixed,
indexed and attribute assignments while excluding NON_/NOT_ decoys only
at their own occurrence. A decoy elsewhere on a line must not suppress a
real assignment. Bulk matches must finish reporting, and scanner errors
must fail closed.

The fixed input panel invokes the actual script in an isolated repository.
The Python-only repository scan extracts its live detector and suppression
patterns; it supplements, rather than replaces, the fixed input panel.
"""

# allow: no-sut-import — the subject is a CI shell script
# (.github/scripts/file-whitelist-check.sh), not importable Python. The tests
# run the actual script as a subprocess so a defect in its own pipeline
# mechanics (not just its patterns) is caught, and read the pattern out of
# the script rather than restating it so the two cannot drift.

import os
import re
import shlex
import shutil
import subprocess
from pathlib import Path
from typing import Final

import pytest

REPO_ROOT: Final = Path(__file__).resolve().parents[2]
GATE_SCRIPT: Final = REPO_ROOT / ".github/scripts/file-whitelist-check.sh"


def _gate_pattern() -> str:
    """Extract the SECRET_KEY detection pattern from the gate script.

    This used to be a two-stage pipeline (a broad, prefix-agnostic match
    piped through a second, inverted grep dropping NON_/NOT_ decoy lines).
    It is now a single ``grep -P`` (PCRE) pattern: prefix-agnostic like
    before, but with the decoy exclusion built in as two negative
    lookbehinds instead of a second grep, so it can no longer discard a
    genuine secret that merely shares a line with a decoy.
    """
    source = GATE_SCRIPT.read_text()
    # The shell string contains escaped quotes, so the extraction must consume
    # `\"` pairs rather than stopping at the first quote it meets.
    # Deliberately does NOT require the pattern to begin with SECRET_KEY:
    # a change that prefixes it with a boundary group must still be extracted,
    # so the assertions below fail on the narrowing itself rather than erroring
    # out here and hiding it.
    matches = re.findall(
        r'grep (?:-n )?-P "((?:[^"\\]|\\.)*SECRET_KEY(?:[^"\\]|\\.)*)"',
        source,
    )
    # The script greps twice with the same pattern: once to detect, once to
    # report the offending lines. If they ever diverge, the report would point
    # at different lines than the detection fired on.
    assert matches, "no SECRET_KEY grep -P found in the gate script"
    assert len(set(matches)) == 1, (
        f"detection and reporting patterns have diverged: {set(matches)}"
    )
    # Guard against a future edit accidentally reintroducing a prefix
    # allowlist (a boundary group with hardcoded alternatives like
    # `JWT_|FLASK_`) into the pattern. That kind of narrowing is exactly the
    # regression this file exists to catch, and it would otherwise still
    # pass the extraction above.
    pattern = matches[0]
    assert "JWT_" not in pattern and "FLASK_" not in pattern, (
        "detection pattern hardcodes specific prefixes again: "
        f"{pattern!r} -- it must match SECRET_KEY regardless of prefix, "
        "with decoy names excluded via the lookbehind instead"
    )
    # Guard against a future edit reintroducing the whole-line-discard bug:
    # a second, inverted `grep -v` chained after the detection grep. The
    # fix for that bug was to remove stage 2 entirely (fold the exclusion
    # into the single pattern above), not to patch stage 2's aim. Checked
    # at every occurrence of the pattern (both the detection and reporting
    # call sites), not just the first.
    rest_of_line_after_each_match = [
        source[m.end() :].split("\n", 1)[0]
        for m in re.finditer(re.escape(pattern), source)
    ]
    assert not any(
        "grep -v" in rest for rest in rest_of_line_after_each_match
    ), (
        "a `grep -v` immediately follows the SECRET_KEY detection pattern "
        "again -- this is the whole-line-discard shape: a decoy anywhere "
        "on a line would suppress a genuine secret sharing that line. The "
        "exclusion belongs in the single pattern (lookbehind), not a "
        "second, inverted grep over the whole line."
    )
    # The script embeds the pattern in a double-quoted shell string, so its
    # literal quotes arrive backslash-escaped. That is shell syntax, not regex
    # syntax -- `\"` is a valid escape to bash and an error to `re`.
    return pattern.replace('\\"', '"')


def _suppression() -> re.Pattern[str]:
    """Extract the gate's suppression clause, compiled with the script's flags.

    Read out of the script for the same reason the detection pattern is: a
    restated copy is exactly the drift this file exists to prevent. The clause
    is anchored to the SECRET_KEY block -- the script has a dozen other
    ``grep -iE`` calls, so a bare search for one would pick up an unrelated
    check.
    """
    source = GATE_SCRIPT.read_text()
    block = re.search(
        r'grep -P "(?:[^"\\]|\\.)*SECRET_KEY(?:[^"\\]|\\.)*"[^\n]*\n'
        r"\s*if ! grep -(?P<flags>[A-Za-z]*)E "
        r'"(?P<pattern>(?:[^"\\]|\\.)*)"',
        source,
    )
    assert block, "no suppression clause found in the SECRET_KEY gate block"
    # The script greps case-insensitively; a case-sensitive copy here would
    # exempt fewer files than CI does and fail where CI passes.
    assert "i" in block.group("flags"), (
        "suppression clause is no longer case-insensitive; "
        f"flags are {block.group('flags')!r}"
    )
    return re.compile(block.group("pattern").replace('\\"', '"'), re.IGNORECASE)


def _run_gate(content: str, tmp_path: Path) -> subprocess.CompletedProcess[str]:
    """Run the actual gate script against a throwaway single-file repo.

    Builds an isolated git repository containing only a minimal
    ``.file-whitelist.txt`` (so the probe file passes the unrelated
    whitelist check without pulling in the real repo's ~100-pattern list)
    and the probe file itself, then invokes the real
    ``file-whitelist-check.sh`` in ``CHECK_ALL_FILES=true`` mode against it
    -- the same mode the release gate uses, reachable without a GitHub
    Actions ``GITHUB_EVENT_NAME``/``GITHUB_BASE_REF`` environment.

    This runs the gate's own bash pipeline end to end (its own detection
    grep, its own ``if``, its own ``set -euo pipefail``) rather than
    reimplementing it in Python or in a separate subprocess pipeline: a
    helper that models the gate's mechanics cannot catch a defect in the
    gate's own mechanics. SIGPIPE/pipefail and whole-line filtering
    failures are not visible to a helper that only replays the gate's
    *patterns*.
    """
    repo = tmp_path / "repo"
    repo.mkdir()
    subprocess.run(["git", "init", "-q"], cwd=repo, check=True)
    (repo / ".file-whitelist.txt").write_text(r"\.py$" + "\n")
    (repo / "probe.py").write_text(content)
    subprocess.run(["git", "add", "probe.py"], cwd=repo, check=True)
    env = dict(os.environ)
    env["CHECK_ALL_FILES"] = "true"
    return subprocess.run(
        ["bash", str(GATE_SCRIPT)],
        cwd=repo,
        env=env,
        capture_output=True,
        text=True,
    )


def _flags(content: str, tmp_path: Path) -> bool:
    """True if the real gate script reports a Flask secret key for ``content``."""
    result = _run_gate(content, tmp_path)
    return "❌ FLASK SECRET KEY" in result.stdout


@pytest.mark.parametrize(
    "line",
    [
        'SECRET_KEY = "super-secret-flask-value-1"',
        # Prefixed names are real Flask/third-party config keys, not
        # near-misses. The class matters more than any single example: the
        # gate must catch ANY <PREFIX>_SECRET_KEY, not a hardcoded allowlist
        # of prefixes -- that allowlist is exactly the regression this suite
        # exists to prevent.
        'JWT_SECRET_KEY = "super-secret-jwt-value-123"',
        'FLASK_SECRET_KEY = "super-secret-flask-value1"',
        # LDR_-prefixed spellings of the same shape. The application reads
        # neither from the environment (the session-signing key lives in
        # `<data_dir>/.secret_key`), but they are the names an operator would
        # most plausibly try, so they are the most likely place a real leak
        # would show up -- and the pair that a `JWT_`/`FLASK_`-only
        # allowlist is blind to.
        'LDR_SECRET_KEY = "super-secret-ldr-value-123"',
        'LDR_BOOTSTRAP_SECRET_KEY = "super-secret-boot-value-1"',
        'APP_SECRET_KEY = "super-secret-app-value-123"',
        'DJANGO_SECRET_KEY = "super-secret-django-value1"',
        'STRIPE_SECRET_KEY = "super-secret-stripe-value1"',
        'SESSION_SECRET_KEY = "super-secret-sess-value-1"',
        'MY_SECRET_KEY = "super-secret-my-value-1234"',
        'CSRF_SECRET_KEY = "super-secret-csrf-value-1"',
        'OAUTH_SECRET_KEY = "super-secret-oauth-value1"',
        # Mapping form, which the trailing `[^A-Za-z0-9_]*[:=]` admits.
        '"SECRET_KEY": "super-secret-flask-value-1",',
        "SECRET_KEY: str = 'super-secret-flask-value-1'",
        # Attribute and subscript assignment forms. Deliberately not
        # `config["SECRET_KEY"] = ...`: the gate's suppression clause (see
        # `_suppression`) exempts any file containing the literal substring
        # `config[` -- meant for a `config["SECRET_KEY"]` *read* -- and that
        # substring match fires just as well on this *write*, suppressing
        # it too. That is a real, separate gap in the suppression heuristic,
        # not the pipeline bug this file targets, so it is sidestepped here
        # rather than fixed here.
        'self.SECRET_KEY = "super-secret-flask-value-1"',
        'settings["SECRET_KEY"] = "super-secret-flask-value-1"',
        # Suffixed names. The original `SECRET_KEY.*=` caught these; a pattern
        # that jumps straight from SECRET_KEY to a non-identifier character
        # does not, which is the same silent narrowing in the other direction.
        'SECRET_KEY2 = "super-secret-flask-value-1"',
        'SECRET_KEY_V2 = "super-secret-flask-value-1"',
        'SECRET_KEYS = "super-secret-flask-value-1"',
    ],
)
def test_hardcoded_secret_is_flagged(line: str, tmp_path: Path) -> None:
    assert _flags(line, tmp_path), f"gate missed a hardcoded secret: {line}"


@pytest.mark.parametrize(
    "target",
    [
        'SECRET_KEYS["active"]',
        "SECRET_KEYS[0]",
        "SECRET_KEY.value",
        'SECRET_KEYS[scope["primary"]]',
    ],
)
def test_indexed_and_attribute_assignments_are_flagged(target, tmp_path):
    """Decoy exclusions must retain the original assignment-target coverage."""
    result = _run_gate(f'{target} = "synthetic-private-marker-928"\n', tmp_path)
    assert result.returncode == 1, result.stdout + result.stderr
    assert "❌ FLASK SECRET KEY" in result.stdout
    assert "SECURITY REMINDER" in result.stdout


@pytest.mark.parametrize("line_count", [150, 3000])
def test_bulk_secret_dump_is_flagged(line_count: int, tmp_path: Path) -> None:
    """A file of many hardcoded secrets must still be flagged.

    Regression test for a fail-open in the gate's own pipeline mechanics,
    not in its pattern: an earlier stage 2 was ``grep -qv -E "<exclude>"``.
    ``-q`` exits the instant it sees the first surviving line, which --
    once stage 1 had enough matching lines queued up behind it -- killed
    stage 1 with SIGPIPE (exit 141) on its next write. Under this script's
    ``set -euo pipefail``, that 141 became the pipeline's exit status, and
    ``set -e`` is suppressed inside an ``if`` condition, so the ``if`` just
    saw a nonzero status and silently treated it as "no match". A single
    hardcoded secret (one line, well under the pipe-buffer threshold) was
    still caught; a bulk credential dump -- the highest-severity case a
    secret-scanning gate exists for -- passed silently. Kept as a
    regression test through the removal of stage 2: the detection
    pipeline changed shape,
    but must still not silently fail open on a large match. This only
    reproduces by driving the real script (see ``_run_gate``): a Python
    reimplementation of the pipeline has no ``-q``, no SIGPIPE, and no
    ``set -e``/``pipefail`` interaction to trigger it.
    """
    content = "\n".join(
        f'SECRET_KEY_{i} = "super-secret-flask-value-{i:04d}"'
        for i in range(line_count)
    )
    result = _run_gate(content, tmp_path)
    assert "❌ FLASK SECRET KEY" in result.stdout
    assert result.returncode == 1, result.stdout + result.stderr
    # Reaching the final message proves reporting did not abort on SIGPIPE.
    assert "SECURITY REMINDER" in result.stdout


@pytest.mark.parametrize("error_status", [2, 137])
def test_secret_scanner_errors_fail_the_gate(
    tmp_path, monkeypatch, error_status
):
    """A broken PCRE scan must not be treated as a clean file."""
    real_grep = shutil.which("grep")
    assert real_grep is not None
    bin_dir = tmp_path / "bin"
    bin_dir.mkdir()
    shim = bin_dir / "grep"
    shim.write_text(
        "#!/usr/bin/env bash\n"
        'for arg in "$@"; do\n'
        '  if [[ "$arg" == "-P" ]]; then\n'
        '    echo "simulated secret scanner failure" >&2\n'
        f"    exit {error_status}\n"
        "  fi\n"
        "done\n"
        f'exec {shlex.quote(real_grep)} "$@"\n',
        encoding="utf-8",
    )
    shim.chmod(0o755)
    monkeypatch.setenv("PATH", str(bin_dir) + os.pathsep + os.environ["PATH"])

    result = _run_gate(
        'LDR_SECRET_KEY = "super-secret-ldr-value-123"\n', tmp_path
    )

    assert result.returncode == error_status, result.stdout + result.stderr
    assert "SECRET_KEY scan failed for probe.py" in result.stderr
    assert "All security checks passed!" not in result.stdout


def test_no_secret_match_keeps_the_gate_green(tmp_path):
    result = _run_gate("value = 1\n", tmp_path)
    assert result.returncode == 0, result.stdout + result.stderr
    assert "All security checks passed!" in result.stdout


def test_real_secret_with_trailing_decoy_comment_is_flagged(
    tmp_path: Path,
) -> None:
    """A genuine secret must still be flagged when a decoy trails it on the
    same line.

    Regression test for whole-line filtering: the old stage 2,
    ``grep -v -E "(^|[^A-Za-z0-9_])NO[NT]_SECRET_KEY"``, dropped the entire
    matching LINE whenever a decoy identifier appeared anywhere on it -- so
    a trailing ``# NOT_SECRET_KEY ...`` comment silently suppressed a real,
    hardcoded secret earlier on the same line. The fix folds the exclusion
    into the detection pattern itself (a lookbehind right before
    ``SECRET_KEY``), so it can only ever suppress the decoy occurrence, not
    an unrelated real match sharing its line.

    Deliberately avoids the words "example"/"placeholder" in the comment:
    those (along with "os.environ"/"getenv"/"config[") trip the gate's
    separate, file-scoped suppression clause (see `_suppression`), which
    would mask this file's violation for an unrelated reason and defeat the
    point of this test. That suppression-heuristic gap is real but is not
    the pipeline bug this test targets -- sidestepped here the same way the
    `config["SECRET_KEY"] = ...` case is sidestepped above.
    """
    content = 'LDR_SECRET_KEY = "super-secret-ldr-value-123"  # NOT_SECRET_KEY marker\n'
    assert _flags(content, tmp_path), (
        "gate missed a real secret with a same-line trailing decoy comment"
    )


def test_unlike_decoy_comment_is_flagged(tmp_path: Path) -> None:
    """A second, differently-worded same-line decoy case for the same bug.

    ``# unlike NOT_SECRET_KEY`` is the minimal case: nothing but the decoy
    word itself trails the real assignment on the line.
    """
    content = (
        'SECRET_KEY = "super-secret-flask-value-1"  # unlike NOT_SECRET_KEY\n'
    )
    assert _flags(content, tmp_path), (
        "gate missed a real secret with a same-line 'unlike NOT_SECRET_KEY' comment"
    )


def test_real_secret_and_decoy_on_separate_lines_is_flagged(
    tmp_path: Path,
) -> None:
    """A decoy on one line must not suppress a real secret on another line.

    Sanity check in the other direction from the same-line tests above: even
    under the old whole-line-discard stage 2, a decoy on a *different* line
    only ever dropped that other line, never the real secret's own line -- so
    this case was never broken. It is kept here as a fixed point: the
    lookbehind fix must not regress it while fixing the same-line case.
    """
    content = (
        'SECRET_KEY = "super-secret-flask-value-1"\n'
        'NON_SECRET_KEY: Final = "search.fetch.mode"\n'
    )
    assert _flags(content, tmp_path), (
        "gate missed a real secret sharing a file with a decoy on another line"
    )


@pytest.mark.parametrize(
    "line",
    [
        # Too short to be a real key.
        'SECRET_KEY = "short"',
        # No assignment.
        'log.info("SECRET_KEY rotated after %s", when)',
        # Names that merely CONTAIN "SECRET_KEY" are not all Flask secret
        # keys -- unlike JWT_/FLASK_/LDR_/etc, "NON_"/"NOT_" are negation
        # prefixes, not real credential namespaces. The lookbehind drops
        # exactly these two decoy names while still catching every genuine
        # prefix (see test_hardcoded_secret_is_flagged above).
        'NON_SECRET_KEY: Final = "search.fetch.mode"',
        'NOT_SECRET_KEY: Final = "search.fetch.mode"',
        # A decoy alone, as a comment -- must not flag on its own either.
        "# NOT_SECRET_KEY is not a real setting, see docs",
        # No SECRET_KEY-like token on the line at all.
        "x = 1",
    ],
)
def test_benign_line_is_not_flagged(line: str, tmp_path: Path) -> None:
    assert not _flags(line, tmp_path), f"gate over-matched: {line}"


def test_decoy_alone_is_not_flagged(tmp_path: Path) -> None:
    """A file containing only a decoy identifier must never be flagged.

    Explicit standalone case (distinct from the parametrized benign-line
    cases above) because it is the direct counterpart to
    ``test_real_secret_with_trailing_decoy_comment_is_flagged``: same decoy
    text, but with no real secret anywhere in the file to (incorrectly)
    suppress.
    """
    content = 'NOT_SECRET_KEY: Final = "search.fetch.mode"\n'
    assert not _flags(content, tmp_path), "gate flagged a decoy-only file"


def test_environment_read_is_suppressed() -> None:
    """A file that also reads the environment is exempt, as the script says."""
    content = 'SECRET_KEY = os.environ["SECRET_KEY"]\n'
    assert _suppression().search(content), (
        "suppression clause no longer matches an os.environ read"
    )


def test_repository_is_clean_under_the_gate() -> None:
    """No Python file under src/ or tests/ trips the gate today.

    This is what turns the tests above into a real guard: widening the pattern
    is only safe if the repository still passes, and this fails loudly if a
    genuine secret is ever committed under those trees.

    Scope caveat: this walks ``src/`` and ``tests/`` ``*.py`` only, whereas the
    CI gate (``file-whitelist-check.sh`` over ``git ls-files``) scans every
    tracked file of every type. So this can stay green while CI flags a secret
    in a ``.md``/``.yml``/``.js`` or a root/``scripts/`` file -- the safe
    direction (this passing never means CI is more lenient), but it is not a
    complete pre-CI mirror. Broaden the walk if that gap ever matters.
    """
    # Walk the Python source trees directly; the full-script cases above
    # independently exercise tracked-file discovery through git.
    pattern = re.compile(_gate_pattern())
    suppression = _suppression()
    offenders = []
    scanned = 0
    for root in ("src", "tests"):
        for path in sorted((REPO_ROOT / root).rglob("*.py")):
            if "__pycache__" in path.parts:
                continue
            scanned += 1
            text = path.read_text(errors="replace")
            # Per LINE, because that is what grep does. Searching the whole
            # file lets `[^A-Za-z0-9_]*` span newlines and match a SECRET_KEY
            # mention on one line against an unrelated assignment on another.
            # The decoy exclusion is now built into `pattern` itself (the
            # lookbehinds), so a single `pattern.search(line)` is the whole
            # per-line check -- no second, separately-applied exclusion
            # pattern to combine it with.
            if not any(pattern.search(line) for line in text.splitlines()):
                continue
            # Suppression is file-scoped in the script, so it is file-scoped
            # here too.
            if not suppression.search(text):
                offenders.append(str(path.relative_to(REPO_ROOT)))

    # Guard against a walk that silently finds nothing and passes vacuously.
    # A floor, not a census: the tree holds ~2300 .py files today, so 100 is
    # low enough that ordinary pruning will not false-fail and high enough
    # that a broken root or glob will.
    assert scanned > 100, f"only scanned {scanned} files; the walk is broken"
    assert offenders == [], f"files trip the SECRET_KEY gate: {offenders}"