A tiny, local-first CLI that finds the files your AI coding agent actually needs — and packs them into a focused context.
| 1 | """Secret detection and redaction. Never print real secret values.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import re |
| 6 | from dataclasses import dataclass |
| 7 | from pathlib import Path |
| 8 | |
| 9 | # Sensitive filenames / basename patterns (case-insensitive). |
| 10 | SENSITIVE_BASENAME_PATTERNS: tuple[str, ...] = ( |
| 11 | r"^\.env$", |
| 12 | r"^\.env\..+$", |
| 13 | r"^.*credentials\.json$", |
| 14 | r"^.*secrets?\.json$", |
| 15 | r"^service-account\.json$", |
| 16 | r"^id_rsa$", |
| 17 | r"^id_rsa\.pub$", |
| 18 | r"^id_ed25519$", |
| 19 | r"^id_ed25519\.pub$", |
| 20 | r"^id_ecdsa$", |
| 21 | r"^.*\.pem$", |
| 22 | r"^.*\.key$", |
| 23 | r"^.*\.crt$", |
| 24 | r"^.*\.p12$", |
| 25 | r"^.*\.pfx$", |
| 26 | r"^auth\.json$", |
| 27 | r"^token\.json$", |
| 28 | r"^\.npmrc$", |
| 29 | r"^\.pypirc$", |
| 30 | r"^docker-compose\.override\.ya?ml$", |
| 31 | ) |
| 32 | |
| 33 | SENSITIVE_BASENAME_RE = re.compile( |
| 34 | "|".join(f"(?:{p})" for p in SENSITIVE_BASENAME_PATTERNS), |
| 35 | re.IGNORECASE, |
| 36 | ) |
| 37 | |
| 38 | # Content patterns that look like assigned secrets. |
| 39 | # Capture groups: (prefix_with_key, value) |
| 40 | _SECRET_ASSIGN_RE = re.compile( |
| 41 | r"(?P<prefix>(?:^|[\s;,{])" |
| 42 | r"(?:export\s+)?" |
| 43 | r"(?:const\s+|let\s+|var\s+)?" |
| 44 | r"(?:API[_-]?KEY|SECRET[_-]?KEY|SECRET|ACCESS[_-]?TOKEN|AUTH[_-]?TOKEN|" |
| 45 | r"TOKEN|PASSWORD|PASSWD|PRIVATE[_-]?KEY|CLIENT[_-]?SECRET|" |
| 46 | r"AWS[_-]?SECRET[_-]?ACCESS[_-]?KEY|DATABASE[_-]?URL|DB[_-]?PASSWORD|" |
| 47 | r"CONNECTION[_-]?STRING|BEARER)" |
| 48 | r"\s*[=:]\s*)" |
| 49 | r"(?P<quote>[\"']?)" |
| 50 | r"(?P<value>[^\s\"'#;,]+)" |
| 51 | r"(?P=quote)", |
| 52 | re.IGNORECASE | re.MULTILINE, |
| 53 | ) |
| 54 | |
| 55 | _PEM_BLOCK_RE = re.compile( |
| 56 | r"-----BEGIN [A-Z0-9 ]+PRIVATE KEY-----.*?-----END [A-Z0-9 ]+PRIVATE KEY-----", |
| 57 | re.DOTALL, |
| 58 | ) |
| 59 | |
| 60 | _BEARER_RE = re.compile( |
| 61 | r"(Bearer\s+)([A-Za-z0-9\-._~+/]+=*)", |
| 62 | re.IGNORECASE, |
| 63 | ) |
| 64 | |
| 65 | REDACTION_PLACEHOLDER = "[REDACTED]" |
| 66 | |
| 67 | |
| 68 | @dataclass(frozen=True) |
| 69 | class SecurityFinding: |
| 70 | """A detected sensitive path (never includes secret contents).""" |
| 71 | |
| 72 | path: str |
| 73 | reason: str |
| 74 | |
| 75 | |
| 76 | def is_sensitive_path(path: Path | str) -> bool: |
| 77 | """Return True if the basename looks like a secrets file.""" |
| 78 | name = Path(path).name |
| 79 | return SENSITIVE_BASENAME_RE.match(name) is not None |
| 80 | |
| 81 | |
| 82 | def detect_sensitive_paths(paths: list[Path], *, root: Path | None = None) -> list[SecurityFinding]: |
| 83 | """Return findings for paths that look sensitive by name.""" |
| 84 | findings: list[SecurityFinding] = [] |
| 85 | for path in paths: |
| 86 | if is_sensitive_path(path): |
| 87 | rel = _rel(path, root) |
| 88 | findings.append(SecurityFinding(path=rel, reason="sensitive filename")) |
| 89 | findings.sort(key=lambda f: f.path) |
| 90 | return findings |
| 91 | |
| 92 | |
| 93 | def contains_likely_secrets(text: str) -> bool: |
| 94 | """True if content appears to contain secret-like assignments.""" |
| 95 | if not text: |
| 96 | return False |
| 97 | if _PEM_BLOCK_RE.search(text): |
| 98 | return True |
| 99 | if _SECRET_ASSIGN_RE.search(text): |
| 100 | return True |
| 101 | if _BEARER_RE.search(text): |
| 102 | return True |
| 103 | return False |
| 104 | |
| 105 | |
| 106 | def redact_secrets(text: str) -> str: |
| 107 | """Replace likely secret values with ``[REDACTED]``. |
| 108 | |
| 109 | Returns the redacted text. Never returns original secret values in |
| 110 | diagnostics — callers should treat the result as safe to embed. |
| 111 | """ |
| 112 | if not text: |
| 113 | return text |
| 114 | |
| 115 | redacted = _PEM_BLOCK_RE.sub( |
| 116 | f"-----BEGIN PRIVATE KEY-----\n{REDACTION_PLACEHOLDER}\n-----END PRIVATE KEY-----", |
| 117 | text, |
| 118 | ) |
| 119 | |
| 120 | def _replace_assign(match: re.Match[str]) -> str: |
| 121 | quote = match.group("quote") or "" |
| 122 | return f"{match.group('prefix')}{quote}{REDACTION_PLACEHOLDER}{quote}" |
| 123 | |
| 124 | redacted = _SECRET_ASSIGN_RE.sub(_replace_assign, redacted) |
| 125 | redacted = _BEARER_RE.sub(rf"\1{REDACTION_PLACEHOLDER}", redacted) |
| 126 | return redacted |
| 127 | |
| 128 | |
| 129 | def should_exclude_from_context(path: Path | str) -> bool: |
| 130 | """Sensitive files are never included in packed context.""" |
| 131 | return is_sensitive_path(path) |
| 132 | |
| 133 | |
| 134 | def _rel(path: Path, root: Path | None) -> str: |
| 135 | if root is None: |
| 136 | return path.as_posix() |
| 137 | try: |
| 138 | return path.resolve().relative_to(root.resolve()).as_posix() |
| 139 | except ValueError: |
| 140 | return path.as_posix() |