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.0 KB
Raw Blame
"""Regression tests for the low-entropy credential-literal Gitleaks rule."""

# allow: no-sut-import — guards the repository-level Gitleaks configuration

import json
import os
import re
import shutil
import subprocess
import tomllib
from pathlib import Path

import pytest


CONFIG_PATH = Path(__file__).parents[2] / ".gitleaks.toml"


def _credential_rule() -> dict:
    config = tomllib.loads(CONFIG_PATH.read_text(encoding="utf-8"))
    return next(
        rule for rule in config["rules"] if rule["id"] == "credential-literal"
    )


def _is_finding(line: str) -> bool:
    """Check every literal, applying only the shared placeholder allowlist."""
    rule = _credential_rule()
    allowlist = rule["allowlists"][0]
    return any(
        not any(
            re.search(pattern, match.group(rule["secretGroup"]))
            for pattern in allowlist["regexes"]
        )
        for match in re.finditer(rule["regex"], line)
    )


def test_credential_literal_rule_catches_bare_and_quoted_keys():
    assert _is_finding('password = "summer2026"')
    assert _is_finding('  secret: "winter2027"')
    assert _is_finding('{"password": "autumn2028"}')
    assert _is_finding("{'pwd': 'spring2029'}")
    assert _is_finding('DB_PASSWORD="summer2026"')
    assert _is_finding('db_password = "winter2027"')
    assert _is_finding('client_secret = "autumn2028"')
    assert _is_finding('api_key = "spring2029"')
    assert _is_finding('AUTH_TOKEN = "summer2030"')
    assert _is_finding('{"access-token": "winter2031"}')
    assert _is_finding('token = "autumn2032"')


def test_credential_literal_rule_ignores_code_and_placeholders():
    assert not _is_finding("password = get_user_password(username)")
    assert not _is_finding('password = "not-a-secret"')
    assert not _is_finding('"password": "your-password"')
    assert not _is_finding('secret = "replace-me"')
    assert not _is_finding('secret = "changeme"')
    assert _is_finding('password = "benchmark123"')
    assert _is_finding('password = "xxxxxxxx"')
    assert not _is_finding('key = "summer2026"')
    assert not _is_finding('password = "short"')


@pytest.fixture
def gitleaks_cli():
    gitleaks = os.environ.get("GITLEAKS_PATH") or shutil.which("gitleaks")
    if gitleaks is None:
        if os.environ.get("REQUIRE_GITLEAKS") == "1":
            pytest.fail("REQUIRE_GITLEAKS=1 but gitleaks is not installed")
        pytest.skip("gitleaks is not installed")
    return gitleaks


def _scan_credentials(gitleaks, scan_root, report):
    result = subprocess.run(
        [
            gitleaks,
            "dir",
            "--no-banner",
            "--redact",
            "--config",
            str(CONFIG_PATH),
            "--report-format",
            "json",
            "--report-path",
            str(report),
            ".",
        ],
        cwd=scan_root,
        check=False,
        capture_output=True,
        text=True,
        timeout=30,
    )
    assert result.returncode in (0, 1), result.stderr
    return [
        finding
        for finding in json.loads(report.read_text(encoding="utf-8"))
        if finding["RuleID"] == "credential-literal"
    ]


def test_credential_literal_rule_with_gitleaks_cli(tmp_path, gitleaks_cli):
    fixture = tmp_path / "credential-canaries.txt"
    fixture.write_text(
        "\n".join(
            [
                'password = "summer2026"',
                'DB_PASSWORD = "winter2027"',
                '"client_secret": "autumn2028"',
                'AUTH_TOKEN = "spring2029"',
                'password = "not-a-secret"',
                "password = get_user_password(username)",
            ]
        ),
        encoding="utf-8",
    )
    findings = _scan_credentials(
        gitleaks_cli, tmp_path, tmp_path / "gitleaks-report.json"
    )
    assert {finding["StartLine"] for finding in findings} == {1, 2, 3, 4}


def test_placeholder_does_not_hide_another_literal_on_the_same_line():
    assert _is_finding(
        '# {"password": "example-secret"}, {"password": "summer2026"}'
    )
    assert _is_finding(
        '# {"password": "summer2026"}, {"password": "example-secret"}'
    )


def test_log_sanitizer_examples_use_shared_placeholders():
    source = (
        CONFIG_PATH.parent / "src/local_deep_research/security/log_sanitizer.py"
    ).read_text(encoding="utf-8")
    offenders = [
        i
        for i, line in enumerate(source.splitlines(), start=1)
        if _is_finding(line)
    ]
    assert offenders == [], (
        "Use shared credential placeholders in log_sanitizer.py examples; "
        f"unexpected credential literals on lines {offenders}"
    )


def test_log_sanitizer_exception_requires_both_path_and_value(
    tmp_path, gitleaks_cli
):
    scan_root = tmp_path / "scan"
    allowed_path = "src/local_deep_research/security/log_sanitizer.py"
    other_paths = [
        "src/local_deep_research/security/other.py",
        "unrelated_src/local_deep_research/security/log_sanitizer.py",
    ]
    source = scan_root / allowed_path
    source.parent.mkdir(parents=True)
    source.write_text(
        "\n".join(
            [
                '# {"api_key": "alice123"}',
                '# {"password": "summer2026"}',
                '# {"password": "example-secret"}, {"password": "winter2027"}',
                '# {"password": "alice123"}, {"password": "autumn2028"}',
                '# {"password": "alice123-more"}',
                '# {"password": "more-alice123"}',
            ]
        ),
        encoding="utf-8",
    )
    for rel_path in other_paths:
        other = scan_root / rel_path
        other.parent.mkdir(parents=True, exist_ok=True)
        other.write_text('# {"password": "alice123"}\n', encoding="utf-8")

    findings = _scan_credentials(
        gitleaks_cli, scan_root, tmp_path / "gitleaks-report.json"
    )
    actual = {
        (Path(finding["File"]).as_posix(), finding["StartLine"])
        for finding in findings
    }
    expected = {(allowed_path, line) for line in range(2, 7)}
    expected.update((path, 1) for path in other_paths)
    assert actual == expected