Skip to content

bug: a tab-separated User-Agent parses wrongly, splitting one agent across two series #1017

Closed

Description

@huang195

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.

Measured on feat/cost-a-client:

in="claude-cli/2.1\t(external)" -> Name="claude-code" Version="2.1�(external)"
in="claude-cli/2.1 (external)"  -> Name="claude-code" Version="2.1"

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.

Activity

  1. huang195 commented on Sep 16, 2026

    @huang195
    MemberAuthor

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions