Skip to content

fix(redact): find a key right after a JSON escape - #87

Merged
fylorn merged 1 commit into
mainfrom
fix/tokens-after-escapes
Sep 21, 2026
Merged

fylorn merged 1 commit into
mainfrom
fix/tokens-after-escapes

Conversation

@fylorn

@fylorn fylorn commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

Built-in redaction rules missed a key that sits at the start of a line.

The scan runs on the request body as it is sent, which is JSON: a newline in the text is the two characters \n, a tab is \t, and some clients write non-ASCII as \uXXXX (Python's json.dumps does by default, so 密钥:sk-… arrives as 密钥:sk-…). Tokens were split on characters, so the letter of the escape stuck to what followed: a key after a newline became nsk-ant-… and no prefix matched. The same held for JWTs and, when they are on, internal addresses. A key alone on a line is the ordinary shape of a credentials file pasted into a prompt or read by a tool, so this was a real gap in observe and enforce alike.

for_each_token now treats a whole escape sequence (\ plus the next character, and four hex digits after \u) as a separator. Replacement ranges still start after the escape, so the body stays valid JSON.

Found while demoing the security page: a request with a key after a full-width colon was not recorded. The sample tester in the control API runs the same scan, so it agrees with the gateway again.

Tests

  • A key after \n, \t and \uXXXX, a JWT after \n, and a key after an escaped backslash are all found.
  • A body with a key at the start of a line is redacted, stays JSON, and restores byte for byte.
  • cargo test -p tw-redact (60 passed), clippy -D warnings, fmt.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@fylorn
fylorn merged commit f4d0f2e into main Sep 21, 2026
1 check passed
@fylorn
fylorn deleted the fix/tokens-after-escapes branch September 21, 2026 18:00
@fylorn fylorn mentioned this pull request Sep 21, 2026
fylorn added a commit that referenced this pull request Sep 21, 2026
Built-in redaction rules find a key right after a JSON escape (#87), and
a built-in tool-call rule's action on enforce is a setting (#88).
Additive; CONTROL_API_VERSION unchanged.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
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