tests / unittest (push) Failing after 13s
Stage 1 flags the living documents a pull request makes stale (no model, nothing sent). Stage 2, on repositories that opt in with drafter: claude, has Claude Code draft the edits in a sandboxed copy of the tree and opens them as a pull request into the feature branch. A monthly pass re-reads the top-level documents against the whole repository. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
51 lines
2.0 KiB
Python
51 lines
2.0 KiB
Python
"""Which environment variables hold secrets, and keeping their values out of text."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from collections.abc import Mapping
|
|
|
|
SECRET_NAME_PARTS = ("TOKEN", "KEY", "SECRET", "CREDENTIAL", "AUTHORIZATION")
|
|
# PASS and AUTH are words inside ordinary names (COMPASS, AUTHOR), so they count only at
|
|
# an underscore boundary or in a fixed spelling. ASKPASS names a credential helper.
|
|
_BOUNDED_PARTS = re.compile(
|
|
r"(?:^|_)PASS|PASSWORD|PASSWD|ASKPASS|(?:^|_)AUTH(?:_|$)|OAUTH"
|
|
)
|
|
# Names that match a part but never hold a secret: git's per-process config (the
|
|
# runner passes safe.directory this way) and the ssh agent's socket path.
|
|
NOT_SECRET_PREFIXES = ("GIT_CONFIG_",)
|
|
NOT_SECRET_NAMES = frozenset({"SSH_AUTH_SOCK"})
|
|
MIN_SECRET_LENGTH = 8
|
|
REDACTED = "***"
|
|
|
|
|
|
def is_secret_name(name: str) -> bool:
|
|
"""True when a variable name holds a secret part, ignoring case: any of
|
|
SECRET_NAME_PARTS anywhere, or PASS or AUTH at a token boundary. GIT_CONFIG_*
|
|
and SSH_AUTH_SOCK are never secret."""
|
|
upper = name.upper()
|
|
if upper in NOT_SECRET_NAMES or upper.startswith(NOT_SECRET_PREFIXES):
|
|
return False
|
|
return any(part in upper for part in SECRET_NAME_PARTS) or bool(
|
|
_BOUNDED_PARTS.search(upper))
|
|
|
|
|
|
def secret_values(env: Mapping[str, str]) -> list[str]:
|
|
"""Values of secret-named variables at least MIN_SECRET_LENGTH long, longest first
|
|
so a value containing another is replaced whole."""
|
|
values = {v for k, v in env.items()
|
|
if is_secret_name(k) and isinstance(v, str) and len(v) >= MIN_SECRET_LENGTH}
|
|
return sorted(values, key=len, reverse=True)
|
|
|
|
|
|
def redact(text: str, env: Mapping[str, str]) -> str:
|
|
"""text with every secret value from env replaced by ***."""
|
|
for value in secret_values(env):
|
|
text = text.replace(value, REDACTED)
|
|
return text
|
|
|
|
|
|
def scrub_env(env: Mapping[str, str]) -> dict[str, str]:
|
|
"""A copy of env without any secret-named variable."""
|
|
return {k: v for k, v in env.items() if not is_secret_name(k)}
|