Skip to content

fix(guard): do not split a character when finding a URL's scheme - #333

Merged
fylorn merged 2 commits into
mainfrom
fix/url-scheme-char-boundary
Oct 12, 2026
Merged

fylorn merged 2 commits into
mainfrom
fix/url-scheme-char-boundary

Conversation

@fylorn

@fylorn fylorn commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Problem

A streamed response could be cut off mid-way: the client saw stream disconnected before completion and the request log showed gw.internal ("interrupted by an error inside the gateway").

The tool-call wall (tw-guard/src/tools/net.rs, destinations) scans each tool call's arguments for http(s):// destinations every time another argument delta arrives. It found where the scheme starts like this:

let scheme_start = args[..sep]
    .rfind(|c: char| !c.is_ascii_alphabetic())
    .map_or(0, |i| i + 1);
let scheme = &args[scheme_start..sep]; // panics

rfind returns the byte offset of the start of the last non-letter character. When that character is multibyte, i + 1 lands inside it and the slice panics: byte index N is not a char boundary.

Minimal reproducer: a tool call whose arguments contain a multibyte character immediately before a URL, e.g.

{"command":["bash","-lc","echo 文档见:https://example.com/a"]}

(: is U+FF1A, three bytes). This is ordinary text in CJK conversations. The wall runs while the response streams whenever inspect_tools is not off (observe is the default), so it does not depend on redaction settings. The panic happens inside the response body stream after the headers were sent, so Ending is dropped while panicking and reports gw.internal.

Fix

Count the ASCII letters back from :// byte by byte. ASCII letters are single bytes, so the start is always on a character boundary.

tw-secret/src/mask.rs redact_urls_within had the same slicing mistake (rfind(...).map(|p| p + 1)), reached from mask_line when a config line contains a credential URL right after a multibyte character (# 上游换成了:https://user:pass@host). It gets the same fix.

Tests

  • tools::net::tests::a_multibyte_character_right_before_a_url_does_not_split_it
  • tools::wall::tests::a_url_right_after_chinese_text_in_streamed_arguments_does_not_break_the_stream (Responses function_call_arguments.delta stream)
  • mask::body_tests::a_credential_url_right_after_chinese_text_is_masked_without_splitting_a_character

All three panic without the fix. cargo test -p tw-guard -p tw-gateway -p tw-secret, cargo clippy --workspace --all-targets -- -D warnings, and cargo fmt --all -- --check pass.

The panic was found by randomized tests over Responses-shaped request bodies and SSE streams. The tests drove outbound redaction (observe and enforce, custom rules matching as written or by regex, case-insensitive, with Unicode case variants), the SSE/byte restorers, body storage masking, security-log detail, and the tool-call wall. After this fix, about 30k cases ran with no further panics.

🤖 Generated with Claude Code

fylorn and others added 2 commits October 12, 2026 15:18
The tool-call wall looks for http(s) destinations in every streamed
tool-call argument. It found the start of the scheme as "position of the
last non-letter before `://`, plus one". When that character is multibyte
(a full-width colon, a CJK character, an accented letter), the start
landed inside it and the slice panicked. The wall runs while the response
streams in every mode except off (observe is the default), so the client
saw the stream cut off and the request was logged as an internal error.

Count the ASCII letters back from `://` instead; the start is then always
on a character boundary. The same slicing mistake in tw-secret's
redact_urls_within (reached by mask_line on config lines) gets the same fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fylorn
fylorn merged commit 7e14e85 into main Oct 12, 2026
6 checks passed
@fylorn
fylorn deleted the fix/url-scheme-char-boundary branch October 12, 2026 07:53
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 12, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant