Skip to content

Stop pairing escaped quotes across an escaped line break - #252

Merged
richardopenrefinery merged 2 commits into
mainfrom
fix/escaped-line-break-quote-pair
Nov 11, 2026
Merged

richardopenrefinery merged 2 commits into
mainfrom
fix/escaped-line-break-quote-pair

Conversation

@richardopenrefinery

Copy link
Copy Markdown
Contributor

Follow-up to #251.

Problem

After #251, three traces were still refused in the Share flow's Redact step, all 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 as a quoted mailbox. The quote index only cleared its stack on a raw \r or \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 in boundaries.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):

from clawjournal.redaction.boundaries import ensure_text_boundaries
from clawjournal.redaction.candidate_formats import iter_format_candidates
head = '{"output": "' + "ordinary text " * 8 + r'print(\"one\")\n'
tail = r'source = f\"@pytest.fixture\\ndef client(): pass\")"}'
ensure_text_boundaries(head + " " * 48 + tail)        # RedactionBoundaryError(email_extended)
list(iter_format_candidates(head + " " * 20 + tail))  # one email_extended candidate, 37-byte local part

Change

A line break written as an escape now ends the line for quotes at its own escaping level and deeper, in _email_spans in clawjournal/redaction/candidate_formats.py.

  • In the quote index. On n or r after s backslashes, the stack drops every quote wider than the lowest set bit of s. So \n separates \" pairs and \\\" pairs, and \\n separates \\\" 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.
  • Literal backslashes are not line breaks. Where the quote width divides s, the backslashes are literal and nothing changes: C:\\new next to a \" quote is a path.
  • The mixed pair gets the same check. When an escaped closing quote is paired with the nearer unescaped quote that opens its string (the pre-Fix blocked Share previews from quotes paired across unrelated text #251 pairing, kept for short spans), an escaped line break at the closing quote's level between them also disqualifies the pair. Without this, the under-budget replacement would remain whenever the enclosing JSON string starts within 64 bytes. The escaped pair is still tried afterwards, so a genuine mailbox on the later line is found.

ensure_safe_replacement and ensure_text_boundaries are untouched.

Unchanged, with tests

  • \"john doe\"@audit.test on the line after an escaped line break is still replaced, at one, two and three JSON levels.
  • An oversized one-line escaped pair is still deferred, also when it follows an escaped line break.
  • A Windows-style path such as C:\\new before a genuine escaped mailbox does not hide it, and a mailbox whose local part holds a literal backslash before n or r is still replaced.
  • \n between unescaped quotes is two ordinary characters at that level, so "a\nb"@audit.test is replaced as before.

Not covered

Line breaks written as \u000a or \x0a are not treated as line breaks. JSON encoders write \n and \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; \r and \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 to test_quote_before_decorator_or_file_mention_previews (/redacted and /redaction-report, AI review off and on).
  • On main, 36 of these fail (33 unit, 3 daemon); the unchanged-behaviour tests pass on both.
  • Full suite on Python 3.13: 5822 passed, 1 skipped.
  • Differential fuzz against main (not checked in), 200,000 strings: the only candidates that changed were pairs across an escaped line break, plus one later candidate whose walk-back was no longer cut short by such a pair.
  • Read-only check on a local index: the three traces that main still refused for this shape have no oversized candidate left.

PRIVACY.md gets one sentence. The frontend is not touched.

🤖 Generated with Claude Code

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>

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_spans on an escaped n/r. It removes only quotes wider than the lowest set bit of the backslash count, so C:\\new next 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.
@richardopenrefinery
richardopenrefinery merged commit 68abb48 into main Nov 11, 2026
7 checks passed
@richardopenrefinery
richardopenrefinery deleted the fix/escaped-line-break-quote-pair branch November 11, 2026 03:10

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 optional suggestion (a nit or a note on pre-existing code) was found and not posted.

Comment on lines +264 to +267
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)):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant