Skip to content

Free, open source blog writer for Claude Code. Researches current sources, writes answer-first posts for Google and AI answers, has an independent reviewer score each one out of 100 and ships only after five delivery gates. 32 skills, 30 /blog commands, 5 agents. Active development at AI-Marketing-Hub/claude-blog; public releases ship here.

9.4 KB
Raw Blame
#!/usr/bin/env python3
"""Fail on unadjudicated detect-secrets findings without exposing values."""

from __future__ import annotations

import argparse
import json
import re
import subprocess
import sys
from collections import Counter
from pathlib import Path


ROOT = Path(__file__).resolve().parent.parent
SHA_FIELD_RE = re.compile(
    r'"(?:sha256|raw_snapshot_sha256|content_sha256|normalized_content_sha256|captured_excerpt_sha256|normalized_full_document_sha256)"\s*:\s*"[0-9a-f]{64}"',
    re.IGNORECASE,
)
GIT_REVISION_FIELD_RE = re.compile(r'"revision"\s*:\s*"[0-9a-f]{40}"', re.IGNORECASE)
# The marketplace pins Blog Studio's git-subdir source to a full commit. Only a
# lowercase 40-hex "sha" field in the marketplace manifest is adjudicated.
MARKETPLACE_PATH = ".claude-plugin/marketplace.json"
MARKETPLACE_SHA_FIELD_RE = re.compile(r'^\s*"sha"\s*:\s*"[0-9a-f]{40}"\s*,?\s*$')
ACTION_PIN_RE = re.compile(r"uses:\s*[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+@[0-9a-f]{40}\b")
ACTION_EXPECTATION_RE = re.compile(
    r'"actions/(?:checkout|setup-node|setup-python)"\s*:\s*"[0-9a-f]{40}"'
)
# These public Git commits pin the historical ownership evidence. Approval is
# limited to their exact values and the evidence paths plus this policy file.
# Other high-entropy values and all forbidden credential patterns still fail.
REVIEWED_LEGACY_COMMITS = frozenset({
    "7b6ca107adb40b7c59030568420c117e2ea61301",
    "2500d4c765034864cede2bf215d00ccd4d7d6fb8",
})
LEGACY_COMMIT_PATHS = frozenset({
    "scripts/check_secrets.py",
    "data/legacy-install-ownership.json",
    "tests/test_legacy_ownership_inventory.py",
    "tests/test_windows_installer_ownership.py",
    "tests/windows/windows_installer_ownership_smoke.ps1",
})
LEGACY_COMMIT_LINE_RE = re.compile(
    r"""\s*(?:(?:"(?:commit|revision)"|\$Baseline)\s*(?::|=)\s*|assert\s+)?["'](?P<commit>[0-9a-f]{40})["'](?:\s+in\s+source)?\s*,?\s*"""
)
# A Secret Keyword finding is a false positive only when the assigned VALUE is
# a placeholder. A marker elsewhere on the line (a comment, another field) does
# not clear a real secret that shares the line.
PLACEHOLDER_PREFIXES = (
    "YOUR_",
    "your-",
    "/path/to/",
    "AIzaSy...",
)
PLACEHOLDER_EXACT = frozenset({"..."})
AUTH_SCHEME_RE = re.compile(r"^(?:Bearer|Basic|Token)\s+", re.IGNORECASE)
# Every secret-looking assignment on the line (name, then value) is examined.
SECRET_ASSIGNMENT_RE = re.compile(
    r"""(?P<name>[A-Za-z0-9_.-]*(?:key|token|secret|pass|pwd|auth|credential)[A-Za-z0-9_.-]*)["']?\s*(?:=|:)\s*"""
    r"""(?P<value>"[^"]*"|'[^']*'|[^\s,;)}\]]+)""",
    re.IGNORECASE,
)
FORBIDDEN_VALUE_PATTERNS = {
    "Stripe key": re.compile(r"\b(?:sk|rk|pk)_(?:live|test)_[A-Za-z0-9]{16,}\b"),
    "Slack token": re.compile(r"\bxox[abprs]-[A-Za-z0-9-]{10,}\b"),
    "private key": re.compile(r"-----BEGIN [A-Z ]*PRIVATE KEY-----"),
    "OpenAI key": re.compile(r"\bsk-[A-Za-z0-9_-]{20,}\b"),
    "Anthropic key": re.compile(r"\bsk-ant-[A-Za-z0-9_-]{20,}\b"),
    "GitHub token": re.compile(r"\b(?:ghp|github_pat)_[A-Za-z0-9_]{20,}\b"),
    "Google API key": re.compile(r"\bAIza[0-9A-Za-z_-]{20,}\b"),
    "AWS access key": re.compile(r"\bAKIA[0-9A-Z]{16}\b"),
}


def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--json", action="store_true", help="Emit a JSON result envelope.")
    return parser.parse_args(argv)


def run_detect_secrets() -> dict[str, object]:
    command = [
        "detect-secrets",
        "scan",
        "--all-files",
        "--exclude-files",
        r"^outputs/",
        "--exclude-files",
        r"^\.git/",
        "--exclude-files",
        r"^\.pytest_cache/",
        # Local virtualenvs and dependency trees are not source. A contributor's
        # .venv would otherwise trip the entropy scan on vendored activate scripts.
        "--exclude-files",
        r"^(\.venv|venv|node_modules)/",
    ]
    try:
        result = subprocess.run(
            command,
            cwd=ROOT,
            text=True,
            capture_output=True,
            check=False,
        )
    except FileNotFoundError as exc:
        raise SystemExit("ERROR: detect-secrets is not installed") from exc
    if result.returncode:
        raise SystemExit("ERROR: detect-secrets scan failed without a usable report")
    try:
        payload = json.loads(result.stdout)
    except ValueError as exc:
        raise SystemExit("ERROR: detect-secrets returned invalid JSON") from exc
    if not isinstance(payload, dict) or not isinstance(payload.get("results"), dict):
        raise SystemExit("ERROR: detect-secrets report has an unexpected shape")
    return payload


def line_for(path: Path, number: int) -> str:
    if number < 1:
        return ""
    try:
        lines = path.read_text(encoding="utf-8", errors="replace").splitlines()
    except OSError:
        return ""
    return lines[number - 1] if number <= len(lines) else ""


def is_adjudicated(path: str, finding: dict[str, object], line: str) -> bool:
    finding_type = str(finding.get("type", ""))
    if finding_type == "Hex High Entropy String":
        commit_line = LEGACY_COMMIT_LINE_RE.fullmatch(line)
        reviewed_commit = (
            path in LEGACY_COMMIT_PATHS
            and commit_line is not None
            and commit_line["commit"] in REVIEWED_LEGACY_COMMITS
        )
        return bool(
            SHA_FIELD_RE.search(line)
            or GIT_REVISION_FIELD_RE.search(line)
            or ACTION_PIN_RE.search(line)
            or ACTION_EXPECTATION_RE.search(line)
            or reviewed_commit
            or (path == MARKETPLACE_PATH and MARKETPLACE_SHA_FIELD_RE.fullmatch(line) is not None)
        )
    if finding_type == "Secret Keyword":
        return is_placeholder_value(line)
    return False


def is_placeholder_value(line: str) -> bool:
    """True when every secret-looking value assigned on this line is a placeholder.

    A placeholder marker elsewhere on the line (a comment, an unrelated field)
    never clears a real value, and a line with no recognizable assignment is
    not adjudicated.
    """
    matches = list(SECRET_ASSIGNMENT_RE.finditer(line))
    if not matches:
        return False
    for match in matches:
        value = AUTH_SCHEME_RE.sub("", match["value"].strip("\"'").strip())
        if value in PLACEHOLDER_EXACT:
            continue
        if not any(value.startswith(prefix) for prefix in PLACEHOLDER_PREFIXES):
            return False
    return True


def scan_forbidden_values() -> list[dict[str, object]]:
    findings: list[dict[str, object]] = []
    for path in sorted(ROOT.rglob("*")):
        if not path.is_file() or path.is_symlink():
            continue
        rel = path.relative_to(ROOT).as_posix()
        if any(part in {".git", ".pytest_cache", ".venv", "venv", "node_modules", "outputs"} for part in path.relative_to(ROOT).parts):
            continue
        if path.stat().st_size > 25 * 1024 * 1024:
            continue
        try:
            text = path.read_text(encoding="utf-8")
        except (OSError, UnicodeDecodeError):
            continue
        for label, pattern in FORBIDDEN_VALUE_PATTERNS.items():
            match = pattern.search(text)
            if match:
                findings.append(
                    {
                        "file": rel,
                        "line": text.count("\n", 0, match.start()) + 1,
                        "type": label,
                    }
                )
    return findings


def main(argv: list[str] | None = None) -> int:
    args = parse_args(argv)
    report = run_detect_secrets()
    approved = Counter()
    unexpected: list[dict[str, object]] = []
    for rel, findings in sorted(report["results"].items()):
        if not isinstance(rel, str) or not isinstance(findings, list):
            unexpected.append({"file": str(rel), "line": None, "type": "invalid report row"})
            continue
        for finding in findings:
            if not isinstance(finding, dict):
                unexpected.append({"file": rel, "line": None, "type": "invalid finding"})
                continue
            number = finding.get("line_number")
            line_number = number if isinstance(number, int) else 0
            line = line_for(ROOT / rel, line_number)
            if is_adjudicated(rel, finding, line):
                approved[str(finding.get("type", "unknown"))] += 1
            else:
                unexpected.append(
                    {
                        "file": rel,
                        "line": line_number or None,
                        "type": str(finding.get("type", "unknown")),
                    }
                )

    unexpected.extend(scan_forbidden_values())
    payload = {
        "status": "pass" if not unexpected else "fail",
        "approved_findings": dict(sorted(approved.items())),
        "approved_total": sum(approved.values()),
        "unexpected": unexpected,
    }
    if args.json:
        print(json.dumps(payload, indent=2, sort_keys=True))
    elif unexpected:
        print("Secret scan failed. Findings list file, line, and detector only.", file=sys.stderr)
        for finding in unexpected:
            print(
                f"{finding['file']}:{finding.get('line') or '?'}: {finding['type']}",
                file=sys.stderr,
            )
    else:
        print(f"Secret scan passed with {payload['approved_total']} adjudicated false positives.")
    return 1 if unexpected else 0


if __name__ == "__main__":
    raise SystemExit(main())