Skip to content

reject non-ascii digits in content-length and chunk size - #750

Open
rootvector2 wants to merge 1 commit into
apache:masterfrom
rootvector2:content-length-ascii-digits
Open

rootvector2 wants to merge 1 commit into
apache:masterfrom
rootvector2:content-length-ascii-digits

Conversation

@rootvector2

Copy link
Copy Markdown

Long#parseLong accepts any Unicode digit (and fullwidth A-F in radix 16), so with a UTF-8 CharsetDecoder MessageSupport#getContentLength, ChunkedInputStream#getChunkSize and ChunkDecoder#readChunkHead read Content-Length: ١٠ as 10, a framing value RFC 9112 forbids and intermediaries reject, so reject non-ASCII values next to the isSignedOrEmpty check from #729 (found by reviewing the #729 parse sites, reproduced over a socket, regression tests added).

Long#parseLong accepts any Unicode decimal digit, and with radix 16
also fullwidth Latin letters, so a connection that decodes message
heads with a charset such as UTF-8 parses a Content-Length of
U+0661 U+0660 (Arabic-Indic "10") as 10 and a fullwidth 'A' chunk
size as 10. RFC 9112 restricts these to 1*DIGIT and 1*HEXDIG. Reject
non-ASCII values before parsing in MessageSupport#getContentLength
and both chunk decoders.
@ok2c

ok2c commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

@rootvector2 What good does that do exactly? TextUtils#isAllASCII call is not cheap. Why do we even need it? If Long#parseLong can parse the chunk length into a long value, this is good enough for me

@rootvector2

Copy link
Copy Markdown
Author

Long#parseLong goes through Character#digit, so it takes any Unicode decimal digit and fullwidth A-F, not just 0-9/A-F. That's only reachable when CharCodingConfig sets a non-Latin-1 charset like UTF-8. Then Content-Length: ١٠ parses as 10, while a front-end that follows RFC 9112 1*DIGIT rejects or ignores it, so the two disagree on where the body ends. I hit this going over the #729 parse sites and reproduced it on a DefaultBHttpServerConnection with a UTF-8 decoder, where the bytes of the next pipelined request got read as body. With the default config high bytes map to U+0080-U+00FF and none of those parse as digits, so default setups aren't affected.

On cost, it's one pass over the same short string Long#parseLong already walks, once per message or chunk header. If guarding the UTF-8 case isn't worth it to you, I'm fine closing this.

@ok2c

ok2c commented Nov 19, 2026

Copy link
Copy Markdown
Member

If guarding the UTF-8 case isn't worth it to you, I'm fine closing this.

@rootvector2 One needs to explicitly override the default configuration and force the use of UTF-8 for protocol elements in a direct violation of the specification. I see little sense to allowing to do so and then force ASCII check for message elements. This makes no sense.

By default the library uses ASCII for message elements and the check you re proposing is just a useless overhead.

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.

2 participants