Skip to content

Auto-port 4.1: Reject non-token characters in HTTP/2 header names - #17022

Merged
chrisvest merged 2 commits into
4.1from
auto-port-pr-16762-to-4.1
Jun 30, 2026
Merged

chrisvest merged 2 commits into
4.1from
auto-port-pr-16762-to-4.1

Conversation

@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port of #16762 to 4.1
Cherry-picked commit: 6661ee4


Problem

DefaultHttp2Headers accepts header names that contain bytes outside the HTTP token grammar — non-ASCII (e.g. 0xF0), control characters (NUL/CR/LF/VT/FF/HTAB/...), SP, DEL, and the RFC 7230 separators ("(),/:;<=>?@[\\]{}\""). The current HTTP2_NAME_VALIDATOR only rejects upper-case ASCII and lets everything else through, so something like headers.add(new AsciiString(new byte[]{(byte)0xF0}), "v") succeeds and is then encoded onto the wire.

This was reported in #11975 with maintainer agreement on direction:

  • @idelpivnitskiy: confirmed the field-name grammar
  • @ejona86: "good and appropriate to restrict keys to the values permitted in HTTP/1, with little risk"

Root Cause

DefaultHttp2Headers.HTTP2_NAME_VALIDATOR_PROCESSOR returns !isUpperCase(value) and the non-AsciiString fallback only loops on isUpperCase(charAt(i)). Both paths therefore enforce the lower-case rule but skip the rest of RFC 9113 §8.2.1, which inherits the RFC 7230 token grammar.

Fix

In codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Headers.java:

  • After the empty/null and pseudo-header checks, run HttpHeaderValidationUtil.validateToken(name) and throw connectionError(PROTOCOL_ERROR, ...) when it returns a non--1 index.
  • Keep the existing upper-case ByteProcessor / fallback loop. The token check is additive: pseudo-headers still short-circuit, valid lower-case tokens still pass, upper-case ASCII still gets rejected (now at the upper-case check, not the token check).

Pseudo-headers (:method, :path, ...) deliberately bypass the token check via the existing hasPseudoHeaderFormat early return, since : is not a valid token character but is required for pseudo-headers.

Tests Added

Change point Test
Reject non-ASCII via the AsciiString byte path rejectNonAsciiHeaderNameAsciiString (uses the U+1F631 / 0xF0 0x9F 0x98 0xB1 reproducer from #11975)
Reject non-ASCII via the CharSequence charAt path rejectNonAsciiHeaderNameCharSequence, rejectHighBitHeaderNameCharSequence (covers char ≤ 0xFF and char > 0xFF)
Reject every C0 control byte and every RFC 7230 separator rejectNonTokenCharactersInHeaderName (parameterized — 28 cases: NUL, SOH, BEL, BS, HTAB, LF, VT, FF, CR, US, DEL, SP, and , ; : / = ? @ ( ) [ ] { } < > \ ")
Regression: upper-case rejection unchanged uppercaseHeaderNameStillRejected
Regression: full RFC 7230 lower-case token still accepted acceptValidLowercaseTokenHeaderName (covers x-custom-header, 2name, and a name using every special token char !#$%&'*+-.^_\
Regression: pseudo-headers still accepted acceptPseudoHeaderName
Existing wire-level test now exercises the validator InboundHttp2ToHttpAdapterTest.clientRequestSingleHeaderNonAsciiShouldThrow updated — the rejection now fires at headers.add(...) instead of at the HPACK encoder

mvn -pl codec-http2 test -Drevapi.skip=true runs 1512 tests with 0 failures locally; codec-http regression run is also clean (8891 tests, 0 failures).

Impact

  • API: new DefaultHttp2Headers().add(name, value) now throws Http2Exception(PROTOCOL_ERROR, ...) for any name that is not a valid lower-case RFC 7230 token. Code that was relying on the previous lax behaviour (passing non-ASCII or separator bytes) needs to either (a) clean up the name before adding, or (b) construct the headers with new DefaultHttp2Headers(false) to opt out of validation, which already exists.
  • Wire effect: Inbound HPACK-decoded headers with malformed names now produce an Http2Exception at validation time and are surfaced via the existing decoder failure path. The previous behaviour silently accepted them and could allow malformed bytes to reach the application as parsed Http2Headers.
  • Hot path: Standard requests are unaffected — DefaultHttp2Headers validation only runs when adding non-pseudo headers, and the new validateToken call is a single forward scan that returns -1 on the first invalid byte for clean tokens.

Fixes #11975

## Problem

`DefaultHttp2Headers` accepts header names that contain bytes outside
the HTTP token grammar — non-ASCII (e.g. `0xF0`), control characters
(NUL/CR/LF/VT/FF/HTAB/...), SP, DEL, and the RFC 7230 separators
(`"(),/:;<=>?@[\\]{}\""`). The current `HTTP2_NAME_VALIDATOR` only
rejects upper-case ASCII and lets everything else through, so something
like `headers.add(new AsciiString(new byte[]{(byte)0xF0}), "v")`
succeeds and is then encoded onto the wire.

This was reported in #11975 with maintainer agreement on direction:
- @idelpivnitskiy: confirmed the field-name grammar
- @ejona86: "good and appropriate to restrict keys to the values
permitted in HTTP/1, with little risk"

## Root Cause

`DefaultHttp2Headers.HTTP2_NAME_VALIDATOR_PROCESSOR` returns
`!isUpperCase(value)` and the non-`AsciiString` fallback only loops on
`isUpperCase(charAt(i))`. Both paths therefore enforce the lower-case
rule but skip the rest of RFC 9113 §8.2.1, which inherits the RFC 7230
token grammar.

## Fix

In
`codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Headers.java`:

- After the empty/null and pseudo-header checks, run
`HttpHeaderValidationUtil.validateToken(name)` and throw
`connectionError(PROTOCOL_ERROR, ...)` when it returns a non-`-1` index.
- Keep the existing upper-case `ByteProcessor` / fallback loop. The
token check is additive: pseudo-headers still short-circuit, valid
lower-case tokens still pass, upper-case ASCII still gets rejected (now
at the upper-case check, not the token check).

Pseudo-headers (`:method`, `:path`, ...) deliberately bypass the token
check via the existing `hasPseudoHeaderFormat` early return, since `:`
is not a valid token character but is required for pseudo-headers.

## Tests Added

| Change point | Test |
|--------------|------|
| Reject non-ASCII via the `AsciiString` byte path |
`rejectNonAsciiHeaderNameAsciiString` (uses the U+1F631 / `0xF0 0x9F
0x98 0xB1` reproducer from #11975) |
| Reject non-ASCII via the `CharSequence` `charAt` path |
`rejectNonAsciiHeaderNameCharSequence`,
`rejectHighBitHeaderNameCharSequence` (covers char ≤ 0xFF and char >
0xFF) |
| Reject every C0 control byte and every RFC 7230 separator |
`rejectNonTokenCharactersInHeaderName` (parameterized — 28 cases: NUL,
SOH, BEL, BS, HTAB, LF, VT, FF, CR, US, DEL, SP, and `, ; : / = ? @ ( )
[ ] { } < > \ "`) |
| Regression: upper-case rejection unchanged |
`uppercaseHeaderNameStillRejected` |
| Regression: full RFC 7230 lower-case token still accepted |
`acceptValidLowercaseTokenHeaderName` (covers `x-custom-header`,
`2name`, and a name using every special token char `!#$%&'*+-.^_\`|~`) |
| Regression: pseudo-headers still accepted | `acceptPseudoHeaderName` |
| Existing wire-level test now exercises the validator |
`InboundHttp2ToHttpAdapterTest.clientRequestSingleHeaderNonAsciiShouldThrow`
updated — the rejection now fires at `headers.add(...)` instead of at
the HPACK encoder |

`mvn -pl codec-http2 test -Drevapi.skip=true` runs 1512 tests with 0
failures locally; `codec-http` regression run is also clean (8891 tests,
0 failures).

## Impact

- **API**: `new DefaultHttp2Headers().add(name, value)` now throws
`Http2Exception(PROTOCOL_ERROR, ...)` for any name that is not a valid
lower-case RFC 7230 token. Code that was relying on the previous lax
behaviour (passing non-ASCII or separator bytes) needs to either (a)
clean up the name before adding, or (b) construct the headers with `new
DefaultHttp2Headers(false)` to opt out of validation, which already
exists.
- **Wire effect**: Inbound HPACK-decoded headers with malformed names
now produce an `Http2Exception` at validation time and are surfaced via
the existing decoder failure path. The previous behaviour silently
accepted them and could allow malformed bytes to reach the application
as parsed `Http2Headers`.
- **Hot path**: Standard requests are unaffected — `DefaultHttp2Headers`
validation only runs when adding non-pseudo headers, and the new
`validateToken` call is a single forward scan that returns `-1` on the
first invalid byte for clean tokens.

Fixes #11975

---------

Co-authored-by: Norman Maurer <[email protected]>
(cherry picked from commit 6661ee4)
@chrisvest
chrisvest enabled auto-merge (squash) June 30, 2026 20:43
@chrisvest
chrisvest merged commit d7dcf6c into 4.1 Jun 30, 2026
18 checks passed
@chrisvest
chrisvest deleted the auto-port-pr-16762-to-4.1 branch June 30, 2026 23:05
lltopk pushed a commit to lltopk/netty that referenced this pull request Oct 24, 2026
…tty#17022)

Auto-port of netty#16762 to 4.1
Cherry-picked commit: 6661ee4

---
## Problem

`DefaultHttp2Headers` accepts header names that contain bytes outside
the HTTP token grammar — non-ASCII (e.g. `0xF0`), control characters
(NUL/CR/LF/VT/FF/HTAB/...), SP, DEL, and the RFC 7230 separators
(`"(),/:;<=>?@[\\]{}\""`). The current `HTTP2_NAME_VALIDATOR` only
rejects upper-case ASCII and lets everything else through, so something
like `headers.add(new AsciiString(new byte[]{(byte)0xF0}), "v")`
succeeds and is then encoded onto the wire.

This was reported in netty#11975 with maintainer agreement on direction:
- @idelpivnitskiy: confirmed the field-name grammar
- @ejona86: "good and appropriate to restrict keys to the values
permitted in HTTP/1, with little risk"

## Root Cause

`DefaultHttp2Headers.HTTP2_NAME_VALIDATOR_PROCESSOR` returns
`!isUpperCase(value)` and the non-`AsciiString` fallback only loops on
`isUpperCase(charAt(i))`. Both paths therefore enforce the lower-case
rule but skip the rest of RFC 9113 §8.2.1, which inherits the RFC 7230
token grammar.

## Fix

In
`codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Headers.java`:

- After the empty/null and pseudo-header checks, run
`HttpHeaderValidationUtil.validateToken(name)` and throw
`connectionError(PROTOCOL_ERROR, ...)` when it returns a non-`-1` index.
- Keep the existing upper-case `ByteProcessor` / fallback loop. The
token check is additive: pseudo-headers still short-circuit, valid
lower-case tokens still pass, upper-case ASCII still gets rejected (now
at the upper-case check, not the token check).

Pseudo-headers (`:method`, `:path`, ...) deliberately bypass the token
check via the existing `hasPseudoHeaderFormat` early return, since `:`
is not a valid token character but is required for pseudo-headers.

## Tests Added

| Change point | Test |
|--------------|------|
| Reject non-ASCII via the `AsciiString` byte path |
`rejectNonAsciiHeaderNameAsciiString` (uses the U+1F631 / `0xF0 0x9F
0x98 0xB1` reproducer from netty#11975) |
| Reject non-ASCII via the `CharSequence` `charAt` path |
`rejectNonAsciiHeaderNameCharSequence`,
`rejectHighBitHeaderNameCharSequence` (covers char ≤ 0xFF and char >
0xFF) |
| Reject every C0 control byte and every RFC 7230 separator |
`rejectNonTokenCharactersInHeaderName` (parameterized — 28 cases: NUL,
SOH, BEL, BS, HTAB, LF, VT, FF, CR, US, DEL, SP, and `, ; : / = ? @ ( )
[ ] { } < > \ "`) |
| Regression: upper-case rejection unchanged |
`uppercaseHeaderNameStillRejected` |
| Regression: full RFC 7230 lower-case token still accepted |
`acceptValidLowercaseTokenHeaderName` (covers `x-custom-header`,
`2name`, and a name using every special token char `!#$%&'*+-.^_\`|~`) |
| Regression: pseudo-headers still accepted | `acceptPseudoHeaderName` |
| Existing wire-level test now exercises the validator |
`InboundHttp2ToHttpAdapterTest.clientRequestSingleHeaderNonAsciiShouldThrow`
updated — the rejection now fires at `headers.add(...)` instead of at
the HPACK encoder |

`mvn -pl codec-http2 test -Drevapi.skip=true` runs 1512 tests with 0
failures locally; `codec-http` regression run is also clean (8891 tests,
0 failures).

## Impact

- **API**: `new DefaultHttp2Headers().add(name, value)` now throws
`Http2Exception(PROTOCOL_ERROR, ...)` for any name that is not a valid
lower-case RFC 7230 token. Code that was relying on the previous lax
behaviour (passing non-ASCII or separator bytes) needs to either (a)
clean up the name before adding, or (b) construct the headers with `new
DefaultHttp2Headers(false)` to opt out of validation, which already
exists.
- **Wire effect**: Inbound HPACK-decoded headers with malformed names
now produce an `Http2Exception` at validation time and are surfaced via
the existing decoder failure path. The previous behaviour silently
accepted them and could allow malformed bytes to reach the application
as parsed `Http2Headers`.
- **Hot path**: Standard requests are unaffected — `DefaultHttp2Headers`
validation only runs when adding non-pseudo headers, and the new
`validateToken` call is a single forward scan that returns `-1` on the
first invalid byte for clean tokens.

Fixes netty#11975

---------

Co-authored-by: Guimu <[email protected]>
Co-authored-by: Norman Maurer <[email protected]>
Co-authored-by: Chris Vest <[email protected]>
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.

3 participants