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.

3.4 KB
Raw Blame
"""Regression tests for the Brave Search API-key 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"
BRAVE_PREFIX = "B" + "S"
BRAVE_KEY = BRAVE_PREFIX + ("A1b2" * 8)


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


def _detected_secret(text: str) -> str | None:
    rule = _brave_rule()
    match = re.search(rule["regex"], text)
    if match is None:
        return None
    return match.group(rule["secretGroup"])


def test_brave_rule_reports_only_a_standalone_key():
    rule = _brave_rule()

    assert rule["secretGroup"] == 1
    for text in (BRAVE_KEY, f"key={BRAVE_KEY}", f"({BRAVE_KEY})"):
        assert _detected_secret(text) == BRAVE_KEY


def test_brave_rule_ignores_subscription_identifier_false_positive():
    assert (
        _detected_secret("NEWS_SUBSCRIPTION_MAX_QUESTIONS_PER_ITERATION")
        is None
    )


@pytest.mark.parametrize("joining_character", ["A", "0", "_", "-"])
def test_brave_rule_rejects_keys_embedded_in_larger_tokens(joining_character):
    assert _detected_secret(joining_character + BRAVE_KEY) is None
    assert _detected_secret(BRAVE_KEY + joining_character) is None


def test_brave_rule_detects_a_key_in_shell_default_syntax():
    shell_default = f'BRAVE_API_KEY="${{BRAVE_API_KEY:-{BRAVE_KEY}}}"'
    assert _detected_secret(shell_default) == BRAVE_KEY


def test_brave_rule_detects_consecutive_standalone_keys():
    pattern = re.compile(_brave_rule()["regex"])
    matches = list(pattern.finditer(f"{BRAVE_KEY}\n{BRAVE_KEY}"))
    assert [match.group(1) for match in matches] == [BRAVE_KEY, BRAVE_KEY]


def test_brave_rule_with_gitleaks_cli(tmp_path):
    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")

    fixture = tmp_path / "brave-canaries.txt"
    fixture.write_text(
        "\n".join(
            [
                BRAVE_KEY,
                f"key={BRAVE_KEY}",
                "NEWS_SUBSCRIPTION_MAX_QUESTIONS_PER_ITERATION",
                "_" + BRAVE_KEY,
                BRAVE_KEY + "-suffix",
                f'BRAVE_API_KEY="${{BRAVE_API_KEY:-{BRAVE_KEY}}}"',
                BRAVE_KEY,
                BRAVE_KEY,
            ]
        ),
        encoding="utf-8",
    )
    report = tmp_path / "gitleaks-report.json"
    result = subprocess.run(
        [
            gitleaks,
            "dir",
            "--no-banner",
            "--redact",
            "--config",
            str(CONFIG_PATH),
            "--report-format",
            "json",
            "--report-path",
            str(report),
            str(tmp_path),
        ],
        check=False,
        capture_output=True,
        text=True,
    )
    assert result.returncode == 1, result.stderr
    findings = [
        finding
        for finding in json.loads(report.read_text(encoding="utf-8"))
        if finding["RuleID"] == "brave-api-key"
    ]
    assert {finding["StartLine"] for finding in findings} == {1, 2, 6, 7, 8}