Skip to content

Commit 7fe6cd6

Browse files
scrubber: a flattened PEM body separated from its armour by a space is still a body
6db996b (council pass 2, gpt-6-astra's bypass) taught the truncated-block rule to consume a base64 run glued straight onto the BEGIN armour. The coordinator's lane-D verification probe on 2026-10-22 found the next shape over: a one-line paste whose collapsed newline survives as a space or tab (the BEGIN armour, one space, then MIIE..., what ssh-keygen/openssl output looks like flattened) still had only its header redacted, because the inline form required the run to start at the very next character. The inline form now also admits horizontal whitespace before the run, but only when the run is at least 20 base64 characters, which no word of prose is; an armour line followed by 'and nothing follows' stays prose. Both branches keep possessive quantifiers, so matching stays linear: 5 MB body 0.04 s, 20k armour lines each followed by short words 0.77 s, 100k spaces then prose 0.01 s. RED: three parametrised gap cases (space, two spaces, tab) fail without the src change and the other 111 pass; with it 114 pass. The first commit attempt was refused by detect-private-key and detect-secrets because a comment and a docstring spelled the armour line literally; reworded, no allowlist pragmas. Not lane D's defect (main and the lane behaved identically).
1 parent 7229e8c commit 7fe6cd6

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎packages/agent-session-tools/src/agent_session_tools/scrubber.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,14 @@
119119
# (council pass 2: that run used to survive with only its header redacted).
120120
# The inline form stops at whitespace, so prose after a bare armour line
121121
# ("...PRIVATE KEY----- and nothing follows") is never treated as a body.
122-
_PEM_INLINE_BODY = r"(?=[A-Za-z0-9+/])[A-Za-z0-9+/=]*+"
122+
# A flattened paste may also carry the collapsed newline as a space or tab
123+
# (the BEGIN armour, one space, then "MIIE..."); that form is admitted only
124+
# when the run is at least 20 base64 characters, which no word of prose is,
125+
# so "----- and nothing follows" stays prose while a real body does not.
126+
_PEM_INLINE_BODY = (
127+
r"(?:(?=[A-Za-z0-9+/])[A-Za-z0-9+/=]*+"
128+
r"|[ \t]++(?=[A-Za-z0-9+/=]{20})[A-Za-z0-9+/=]*+)"
129+
)
123130
_PEM_HEAD = re.compile(
124131
rf"-----BEGIN{_PEM_LABEL}-----{_ARMOUR_HEADERS}"
125132
rf"(?:(?=\r?\n|\\[rn]){_PEM_BODY}|{_PEM_INLINE_BODY})?"

‎packages/agent-session-tools/tests/test_scrubber.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,48 @@ def test_flattened_unterminated_pem_leaves_no_body() -> None:
423423
assert result.scrubbed
424424

425425

426+
@pytest.mark.parametrize("gap", [" ", " ", "\t"])
427+
def test_flattened_unterminated_pem_with_a_space_before_the_body_leaves_no_body(
428+
gap: str,
429+
) -> None:
430+
"""Same flattened paste, but with whitespace between the armour and the body.
431+
432+
Found by the coordinator while verifying lane D (2026-10-22): the inline
433+
rule from ``6db996b3`` required the base64 run to start at the very next
434+
character, so the BEGIN armour followed by one space and ``MIIE...`` -- the shape a
435+
one-line paste of ``ssh-keygen``/``openssl`` output takes, with the
436+
newline collapsed to a space -- had only its header redacted. The body is
437+
key material whatever separates it from the armour.
438+
"""
439+
body = "".join(_pem_body("flat-gap")[:3])
440+
text = "paste: " + _pem_marker("BEGIN", "RSA") + gap + body + " [cut]"
441+
result = Scrubber().scrub(text)
442+
assert body not in result.text
443+
assert body[:24] not in result.text
444+
assert "PRIVATE KEY" not in result.text
445+
assert result.text.startswith("paste: [PRIVATE_KEY-")
446+
assert result.text.endswith(" [cut]")
447+
assert result.scrubbed
448+
449+
450+
def test_prose_after_a_bare_armour_line_with_a_space_is_still_prose() -> None:
451+
"""The whitespace allowance must not turn ordinary words into a "body".
452+
453+
``----- and nothing follows`` is prose: short words, not a base64 run of
454+
body length. Only a run long enough to be key material is consumed.
455+
"""
456+
text = (
457+
"The file starts with "
458+
+ _pem_marker("BEGIN", "RSA")
459+
+ " and nothing follows it."
460+
)
461+
result = Scrubber().scrub(text)
462+
assert (
463+
result.text == "The file starts with [PRIVATE_KEY-001] and nothing follows it."
464+
)
465+
assert "PRIVATE KEY" not in result.text
466+
467+
426468
def test_pem_tail_without_begin_line_redacts_body_and_footer() -> None:
427469
"""Output cut off at the front: the body before END is key material too."""
428470
body = _pem_body("tail")[3:]

0 commit comments

Comments
 (0)