refactor(hide-secrets): drop the unreachable configparser error fallback

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
yucheng 2026-09-09 07:39:04 +00:00
parent a74f12d846
commit 1ccc5ab70b
2 changed files with 19 additions and 6 deletions

View file

@ -503,12 +503,7 @@ def _parseable_lines(text: str) -> Iterator[str]:
def _quoted_assignments(text: str) -> tuple[str, ...]:
parser: Final = configparser.ConfigParser(interpolation=None)
parser.optionxform = str # pyright: ignore[reportAttributeAccessIssue] # configparser types optionxform as a method
body: Final = "\n".join(_parseable_lines(text))
try:
parser.read_string(f"[{_CONFIG_SECTION}]\n{body}")
except (configparser.Error, UnicodeDecodeError):
return ()
parser.read_string(f"[{_CONFIG_SECTION}]\n" + "\n".join(_parseable_lines(text)))
return tuple(
f'{key} = "{value}"'
for section in parser

View file

@ -390,6 +390,24 @@ def test_scan_message_reads_a_config_with_a_broken_section_header(content):
assert "Zx4Kp9Lm2Qr7Ns3Vt" not in guardrail.redact_text(content)
@pytest.mark.parametrize(
"content",
[
"=orphan\npassword = Zx4Kp9Lm2Qr7Ns3Vt\n",
" indented before any key\npassword = Zx4Kp9Lm2Qr7Ns3Vt\n",
"greeting = %(name)s\npassword = Zx4Kp9Lm2Qr7Ns3Vt\n",
"token = a\x00b\npassword = Zx4Kp9Lm2Qr7Ns3Vt\n",
],
ids=["empty-key", "leading-continuation", "interpolation", "nul-byte"],
)
def test_scan_message_reads_lines_that_a_stock_ini_parser_rejects(content):
"""The parser has no error fallback, so every line shape must be filtered or
accepted before it reaches configparser, or one odd line would 500 the request."""
guardrail = _guardrail()
assert "Zx4Kp9Lm2Qr7Ns3Vt" not in guardrail.redact_text(content)
def test_scan_message_reads_a_config_that_repeats_a_section():
"""A pasted ini can name the same section twice, and refusing to parse it would drop
every assignment in the message, not just the repeated one."""