Repository navigation
fix(guard): do not split a character when finding a URL's scheme - #333
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A streamed response could be cut off mid-way: the client saw
stream disconnected before completionand the request log showedgw.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 forhttp(s)://destinations every time another argument delta arrives. It found where the scheme starts like this:rfindreturns the byte offset of the start of the last non-letter character. When that character is multibyte,i + 1lands 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 wheneverinspect_toolsis 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, soEndingis dropped while panicking and reportsgw.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.rsredact_urls_withinhad the same slicing mistake (rfind(...).map(|p| p + 1)), reached frommask_linewhen 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_ittools::wall::tests::a_url_right_after_chinese_text_in_streamed_arguments_does_not_break_the_stream(Responsesfunction_call_arguments.deltastream)mask::body_tests::a_credential_url_right_after_chinese_text_is_masked_without_splitting_a_characterAll three panic without the fix.
cargo test -p tw-guard -p tw-gateway -p tw-secret,cargo clippy --workspace --all-targets -- -D warnings, andcargo fmt --all -- --checkpass.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