You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug: a tab-separated User-Agent parses wrongly, splitting one agent across two series #1017
A User-Agent that separates the product token from its comment with a tab parses
wrongly, and the caller lands in a series key of its own.
RFC 9110 spells the separator RWS = 1*( SP / HTAB ), so a tab there is compliant. ParseUserAgent sanitises before it splits, and HTAB is a C0 control, so by the time the
split runs the tab has become U+FFFD and there is no whitespace left to split on.
The consequence is cost attribution, not cosmetics: Label() builds name + "/" + version,
so the same agent sending tab-separated and space-separated requests occupies two byAgent series and its spend divides between them.
Why it was not fixed in place. The sanitise-then-cap order in ParseUserAgent is
load-bearing — a substitution can triple the string, so capping first would let 128 control
bytes become 384 in a value whose whole purpose is to be bounded. The fix is to take the
token boundary from the RAW header and sanitise the extracted fields instead, which reorders
that sequence and needs its own reasoning about what is matched before it is sanitised
(knownClients is an exact lowercase lookup, so a control byte can only cause a miss, never
a false match — but that argument belongs in the change that relies on it).
Two alternatives considered and rejected:
splitting on U+FFFD as well as SP — makes a product token containing any control byte
truncate at it, so "claude-cli\x1b[31m/2.1" would start matching claude-code;
exempting HTAB from the sanitiser — would break the "the rule is identical in both
copies" invariant that sanitizeUA and the ledger's sanitizeLabel hold each other to.
ParseUserAgent and isControlRune both name the residual as of #1011; this issue is the
fix.
Fixed in #1011 (f69f24d) by the reviewer's own one-liner: strings.ReplaceAll(ua, "\t", " ") ahead of the sanitiser.
That sidesteps the reorder this issue said the fix needed — the reorder was only required for the take-the-boundary-from-the-raw-header approach. Normalising legal whitespace to canonical whitespace happens OUTSIDE sanitizeUA, so the rule inside it stays identical to the ledger's copy, and it is byte-length-neutral so the cap arithmetic is untouched. Both alternatives this issue rejected stay rejected.
Covered by two table cases plus TestParseUserAgent_ATabIsNormalisedNotSubstituted, which asserts Raw carries no U+FFFD and that the label matches the space-separated spelling's.
Activity
huang195 commentedon Sep 16, 2026
Fixed in #1011 (f69f24d) by the reviewer's own one-liner:
strings.ReplaceAll(ua, "\t", " ")ahead of the sanitiser.That sidesteps the reorder this issue said the fix needed — the reorder was only required for the take-the-boundary-from-the-raw-header approach. Normalising legal whitespace to canonical whitespace happens OUTSIDE
sanitizeUA, so the rule inside it stays identical to the ledger's copy, and it is byte-length-neutral so the cap arithmetic is untouched. Both alternatives this issue rejected stay rejected.Covered by two table cases plus
TestParseUserAgent_ATabIsNormalisedNotSubstituted, which asserts Raw carries no U+FFFD and that the label matches the space-separated spelling's.