Repository navigation
Stop pairing escaped quotes across an escaped line break - #252
Conversation
After #251, three traces were still refused by the same email_extended candidate: 80 characters with a 65-byte local part ending in f\"@pytest.fixture. The text is JSON-escaped Python source. The closing \" of one string and the opening \" of "@pytest.fixture" on the next source line were paired, because only a raw line break ended the line and the line break between them is written \n. With less indentation the pair fit the budget, and the code between the two strings was replaced as an email. A line break written as an escape now ends the line for quotes at its own escaping level and deeper. With s backslashes before n or r, that is every quote wider than the lowest set bit of s: \n separates \" and \\\" pairs, \\n separates \\\" pairs only. Where the quote width divides s, the backslashes are literal (C:\\new next to \") and nothing changes. The same check applies when an escaped closing quote is paired with the nearer unescaped quote that opens its string. Unchanged: an escaped quoted mailbox on the line after an escaped line break is still replaced, an oversized one-line escaped pair is still deferred, and \n between unescaped quotes is ordinary text. Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
There was a problem hiding this comment.
I reviewed this PR and didn't find any bugs. The change narrows what the share-path email redactor pairs as a quoted mailbox, and the escape-width logic depends on bit arithmetic, so a human look would still be worthwhile.
What was reviewed:
- The new stack pop in
_email_spanson an escapedn/r. It removes only quotes wider than the lowest set bit of the backslash count, soC:\\newnext to a\"quote (width 2) keeps its pairing. _spans_escaped_line_break. It returns false for an unescaped closing quote (width 1), and when it rejects a pair the code still falls back to_escaped_quoted_local_start, so a real mailbox on the later line can still be found.- The PRIVACY.md sentence matches the code. I didn't run the new tests or the randomized property test here.
Extended reasoning...
The change touches clawjournal/redaction/candidate_formats.py, PRIVACY.md and two test files: about 40 lines of logic and 120 lines of tests. That code is a deterministic redaction stage on the share path, so the sensitive surface is data exposure. The change makes the redactor produce fewer quoted-mailbox candidates, which means a mistake here could let an address through rather than over-redact. The pairing rules use bit tricks on escape width (lowest set bit of the backslash count) that are hard to check fully by reading alone, so I deferred instead of approving. No CODEOWNERS file covers these paths, and the timeline shows no outstanding reviewer objections.
Do not discard shell mailbox candidates just because backslash-n or backslash-r would be a JSON line break. Recognize complete arguments, assignment and option values, and mailboxes inside quoted shell arguments, including serialized commands. Retain original spans so oversized representations still fail closed. Keep the escaped-source preview fix by rejecting actual outer JSON line breaks and excluding JSON field and array quotes from shell argument evidence. Index quote context once and scan complete backslash runs to preserve bounded performance. Add redaction and preview regressions, including checks that AI review receives only redacted text. Validation on Python 3.13: 5894 passed, 1 skipped.
| if ((start == 0 or text[start - 1] in separators + "=" or json_start) | ||
| and (domain_end == len(text) or text[domain_end] in separators + "'" | ||
| or json_end) | ||
| and _SHELL_WORD.fullmatch(local)): |
There was a problem hiding this comment.
🔴 Shell-escaped mailboxes in JSON-serialized multi-line commands are shared unredacted when the address ends or starts a command line; the base branch redacts them. The boundary check at candidate_formats.py:264-267 accepts only raw separators. In serialized text, the command's line break before or after the mailbox is the two characters \n, so _shell_quoted_local returns None and the @ gets no candidate. Fix: also treat an escaped n/r/t as a separator on both sides when its backslash-run length k has k % (width // 2) != 0, while k % (width // 2) == 0 stays literal. [also at: clawjournal/redaction/candidate_formats.py:266 - A shell-escaped mailbox inside a serialized multi-line command now leaks unredacted, though the base redacts it. This happens when it is followed by a JSON-escaped \n, \r or \t.]
Why this was flagged
Trigger: a Bash tool input serialized once into the trace, for example {"command": "git config user.email \\\"john\\name\\\"@ audit.test\ngit commit -m x"}. That is a multi-line command whose mailbox is the last word on its line. In _shell_quoted_local the domain walk stops at the \ of the JSON \n. text[domain_end] is \, which is not in separators + "'". The function therefore returns None at line 269. The mixed pair at line 356-358 is also rejected now, because it spans the same \\n. _EMAIL_PATTERN in pii.py:623 cannot match "@ either, so the mailbox reaches preview, export and the AI-PII reviewer in plaintext. On base 4d9bc7d the escaped pair (or the short mixed pair) was accepted and redacted. The same rejection happens on the start side when the mailbox follows a serialized \n or \t (line 264). The new tests use only && echo done or the end of the JSON string, so they do not cover this.
Verification: Severity: normal. In _shell_quoted_local, text[domain_end] is \, which is not in separators + "'" (line 265), and json_end is False, so the function returns None at line 269. On the leading side, line 264 fails the same way. Base (4d9bc7d) produced a candidate and redacted the mailbox, so this is a regression.
Follow-up to #251.
Problem
After #251, three traces were still refused in the Share flow's Redact step, all by the same
email_extendedcandidate: 80 characters with a 65-byte local part ending inf\"@pytest.fixture.The text is JSON-escaped Python source. The closing
\"of one string and the opening\"of"@pytest.fixture"on the next source line were paired as a quoted mailbox. The quote index only cleared its stack on a raw\ror\n, and the line break between the two quotes is written as the escape\n. The decoded local part fit the 64-octet window, so the pair was accepted, and the raw 65-byte span then failed the budget inboundaries.py.With less indentation the same pair was under budget. Then nothing was refused, but the code between the two strings was replaced by
[REDACTED_EMAIL].Synthetic repro on main (4d9bc7d):
Change
A line break written as an escape now ends the line for quotes at its own escaping level and deeper, in
_email_spansinclawjournal/redaction/candidate_formats.py.norraftersbackslashes, the stack drops every quote wider than the lowest set bit ofs. So\nseparates\"pairs and\\\"pairs, and\\nseparates\\\"pairs only. This uses the width scheme that the index already has, and the stack widths increase toward the top, so it is a pop from the top.s, the backslashes are literal and nothing changes:C:\\newnext to a\"quote is a path.ensure_safe_replacementandensure_text_boundariesare untouched.Unchanged, with tests
\"john doe\"@audit.teston the line after an escaped line break is still replaced, at one, two and three JSON levels.C:\\newbefore a genuine escaped mailbox does not hide it, and a mailbox whose local part holds a literal backslash beforenorris still replaced.\nbetween unescaped quotes is two ordinary characters at that level, so"a\nb"@audit.testis replaced as before.Not covered
Line breaks written as
\u000aor\x0aare not treated as line breaks. JSON encoders write\nand\r, and I have not seen the other forms in traces.Tests
tests/redaction/test_redaction_boundaries.py: both reported shapes (the 65-byte block and the under-budget replacement) at one to three JSON levels, with the enclosing string starting far away and nearby;\rand\r\n; the unchanged cases above; a seeded property test that no escaped pair spans an escaped line break at its level; two more inputs in the pairing deadline test.tests/workbench/test_daemon.py: the two reported lines added totest_quote_before_decorator_or_file_mention_previews(/redactedand/redaction-report, AI review off and on).PRIVACY.mdgets one sentence. The frontend is not touched.🤖 Generated with Claude Code