diff --git a/backend/app/firewall/secrets.py b/backend/app/firewall/secrets.py index a80d561..e4c9b6e 100644 --- a/backend/app/firewall/secrets.py +++ b/backend/app/firewall/secrets.py @@ -310,9 +310,11 @@ def _is_discord_bot_token(candidate: str) -> bool: # entire value, including truncated end-of-line captures; otherwise # only the first word is removed and later words remain exposed. # A dangling escape is also secret data, but line endings must survive. - r"\s*[:=]\s*(?:\"((?:\\[^\r\n]|\\(?=\r?$)|[^\"\\\r\n])+)(?:\"|(?=\r?$))" + # Keep separators and values on the same line; \s also matches CR/LF. + # Otherwise a blank assignment can redact ordinary text below it. + r"[ \t]*[:=][ \t]*(?:\"((?:\\[^\r\n]|\\(?=\r?$)|[^\"\\\r\n])+)(?:\"|(?=\r?$))" r"|'((?:\\[^\r\n]|\\(?=\r?$)|[^'\\\r\n])+)(?:'|(?=\r?$))" - r"|([^\s\"',;]{8,}))", + r"|([^\s\"',;]{1,}))", re.MULTILINE, ), ), diff --git a/backend/tests/test_secrets.py b/backend/tests/test_secrets.py index e171bbc..3e22efd 100644 --- a/backend/tests/test_secrets.py +++ b/backend/tests/test_secrets.py @@ -1534,6 +1534,59 @@ def test_short_secrets_never_expose_prefix_suffix_or_length(): assert find_secrets("password=" + "A" * 20)[0].redacted == "AAAA…AAA (20 chars)" +def test_unquoted_short_secret_assignments_are_not_leaked(): + # Explicitly named credentials are sensitive even below eight characters. + for name, value in ( + ("password", "x"), + ("DB_PASSWORD", "12"), + ("api_key", "abc"), + ("TWILIO_AUTH_TOKEN", "1234567"), + ("client_secret", "0"), + ): + raw = f"{name}={value}" + findings = find_secrets(raw) + assert len(findings) == 1, raw + assert findings[0].kind == "secret_assignment" + assert findings[0].redacted == "***" + assert redact_text(raw) == "«REDACTED:secret_assignment»" + assert not has_secret(redact_text(raw)) + + # Adjacent non-secret identifier components must remain unchanged. + for ordinary in ("PASSWORD_HINT=x", "NOT_A_PASSWORD_FIELD=y", "API_KEY_NAME=abc"): + assert not has_secret(ordinary), ordinary + + +def test_empty_unquoted_secret_assignments_preserve_following_line(): + # Empty assignments must never consume ordinary text across LF or CRLF. + for newline in ("\n", "\r\n"): + for empty_line in ("password=", "DB_PASSWORD = ", "client_secret:\t"): + raw = empty_line + newline + "ordinary text" + assert find_secrets(raw) == [], repr(raw) + assert not has_secret(raw), repr(raw) + assert redact_text(raw) == raw + + # Neither the separator nor a value may be split onto another line. + assert not has_secret("password" + newline + "=12") + + +def test_short_unquoted_secret_assignments_preserve_following_line(): + marker = "«REDACTED:secret_assignment»" + for newline in ("\n", "\r\n"): + for assignment in ("password=x", "DB_PASSWORD = 12", "client_secret:\t0"): + raw = "before" + newline + assignment + newline + "ordinary text" + findings = find_secrets(raw) + assert len(findings) == 1, repr(raw) + assert findings[0].kind == "secret_assignment" + assert findings[0].start == len("before" + newline) + assert findings[0].end == len("before" + newline + assignment) + assert redact_text(raw) == "before" + newline + marker + newline + "ordinary text" + + # Consecutive assignments should each redact without consuming a newline. + raw = "password=12" + newline + "api_key=abc" + assert len(find_secrets(raw)) == 2 + assert redact_text(raw) == marker + newline + marker + + # --- negatives: should NOT be flagged ---------------------------------------- def test_ignores_plain_prose(): assert not has_secret("The deploy command changed from make deploy-v1 to make deploy-v2.")