From 866d4acaa782e183ff989809cfb3aca29401b9fc Mon Sep 17 00:00:00 2001 From: Daniel Petty Date: Tue, 22 Sep 2026 16:59:46 +0000 Subject: [PATCH] fix(security): mask credentials, not sentences that mention them The keyword rule matched any word of ten characters or more following `secret`, `token`, `password`, `bearer` or `api_key`. In prose that word is usually English, so the commit subject "unify secret resolution and manage Vault lease lifecycle" was rewritten to "unify and manage Vault lease lifecycle". That is not confined to a terminal. The changelog is masked on its way out and the release description is written from the changelog, so the corruption reached the published notes for v0.2.21 -- "Integrated client-side and capacity-aware dispatch" -- where it reads as though a credential had been caught rather than a phrase mangled. "secret storage" survived only because "storage" is seven characters, which is the kind of accident that makes the rule look like it works. A credential is distinguishable from a word: it carries a digit, a capital, or a separator, or it is longer than any English word. The value is now inspected instead of assumed. `Bearer ghp_A1b2...`, `token abc123def456ghi789` and a twenty-six character opaque string are still masked; "secret resolution", "token counting" and "password management policy" are left alone. The specific rules -- AWS keys, GitHub tokens, private key blocks -- are untouched, and a test covers one of them to confirm narrowing the general rule did not weaken them. Found while compiling the v0.2.22 changelog, where the same subject came through mangled again. --- src/devops_cli/security/sanitizer.py | 33 ++++++++++- .../test_consolidation_security_sanitizer.py | 55 +++++++++++++++++++ 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/src/devops_cli/security/sanitizer.py b/src/devops_cli/security/sanitizer.py index 00ce98070a..613d088919 100644 --- a/src/devops_cli/security/sanitizer.py +++ b/src/devops_cli/security/sanitizer.py @@ -3,10 +3,39 @@ from __future__ import annotations import re +from collections.abc import Callable from typing import Any from urllib.parse import urlsplit, urlunsplit -_SECRET_PATTERNS: tuple[tuple[re.Pattern[str], str], ...] = ( +# The keyword patterns below match a word following "secret", "token" or "password". In +# prose that word is usually English: "unify secret resolution" was rewritten to "unify +# ", and that corruption reached published release descriptions before anyone +# noticed, because the changelog is masked on its way out. A credential is distinguishable +# from a word -- it carries a digit, a capital, or a separator, or it is longer than any +# English word -- so the value is inspected rather than assumed. +_CREDENTIAL_MIN_OPAQUE_LENGTH = 24 + + +def _looks_like_a_credential(value: str) -> bool: + """Report whether a value following a credential keyword is plausibly a secret.""" + if len(value) >= _CREDENTIAL_MIN_OPAQUE_LENGTH: + return True + return any(char.isdigit() or char.isupper() or char in "_-." for char in value) + + +def _mask_credential_like_value(match: re.Match[str]) -> str: + """Replace a keyword-prefixed value only when it looks like a credential.""" + value = match.group(1) + if not _looks_like_a_credential(value): + return match.group(0) + return match.group(0).replace(value, "") + + +# A replacement is a literal for a pattern whose whole match is the secret, or a +# callable where the match also covers surrounding text that must survive. +_Replacement = str | Callable[[re.Match[str]], str] + +_SECRET_PATTERNS: tuple[tuple[re.Pattern[str], _Replacement], ...] = ( ( re.compile( r"(?", + _mask_credential_like_value, ), ( re.compile( diff --git a/tests/test_consolidation_security_sanitizer.py b/tests/test_consolidation_security_sanitizer.py index 99594ac4a0..342f47580c 100644 --- a/tests/test_consolidation_security_sanitizer.py +++ b/tests/test_consolidation_security_sanitizer.py @@ -393,3 +393,58 @@ def test_mask_secrets_word_boundaries_and_safe_paths() -> None: mask_secrets("Authorization: Bearer sk-proj-1234567890abcdef12345678") == "Authorization: Bearer " ) + + +# ============================================================================= +# Prose that merely mentions credentials +# ============================================================================= + + +def test_a_sentence_about_secrets_is_not_a_secret() -> None: + """`\\bsecret\\s+(\\S{10,})` matches any English word of ten characters or more. + + "unify secret resolution and manage Vault lease lifecycle" became "unify + and manage ...", and because the changelog is masked on its way out, + that corruption reached the published release description for v0.2.21 before anyone + noticed. "secret storage" survived only because "storage" is seven characters. + """ + from devops_cli.security.sanitizer import mask_secrets + + subject = "unify secret resolution and manage Vault lease lifecycle" + assert mask_secrets(subject) == subject + + +def test_other_credential_keywords_are_safe_in_prose() -> None: + """The same pattern covers token, password and api_key, so all four need the guard.""" + from devops_cli.security.sanitizer import mask_secrets + + for subject in ( + "token counting inside loops", + "password management policy", + "api_key rotation scheduling", + "bearer instrumentation coverage", + ): + assert mask_secrets(subject) == subject + + +def test_a_space_separated_credential_is_still_masked() -> None: + """`Bearer ` and `token ` are the real space-separated forms.""" + from devops_cli.security.sanitizer import mask_secrets + + assert "abc123def456ghi789" not in mask_secrets("token abc123def456ghi789") + + +def test_a_long_opaque_lowercase_value_is_still_masked() -> None: + """No English word runs to twenty-four characters, so length alone is enough signal.""" + from devops_cli.security.sanitizer import mask_secrets + + secret = "abcdefghijklmnopqrstuvwxyz" + assert secret not in mask_secrets(f"secret {secret}") + + +def test_assigned_credentials_are_unaffected_by_the_guard() -> None: + """The keyword rule is one of many; narrowing it must not weaken the specific ones.""" + from devops_cli.security.sanitizer import mask_secrets + + masked = mask_secrets("export AWS_SECRET_ACCESS_KEY=AKIAIOSFODNN7EXAMPLE") + assert "AKIAIOSFODNN7EXAMPLE" not in masked