2 Commits
Author SHA1 Message Date
Johannes Weiss eeb20e7f95 HTTPDecoder: restore old limit of 80 kB headers (#3621)
### Motivation:

NIO's historic limit was that the maximum header block was limited to 80
kB which is a sensible limit and originates from Nginx's http_parser.c.

The history of the changes:
- commit e67fc995: Introduces the 80 kB limit
- commit 69180342: Accidentally removes the limit entirely
- commit b24872d3: Introduced the 2 MB / 256 headers limit

I would argue that 2MB & 256 headers is a bad limit:
- 2MB is way too big, that allows pretty powerful slowloris-style
attacks forcing NIO to hold on to 2 MB per connection
- 256 headers is too little for software that may be a proxy
  - in HTTP, the number of headers is _non-semantic_
- A: `a: 1\r\n a: 2\r\n a: 3\r\n a: 4\r\n a: 5\r\n a: 6\r\n[...]\r\na:
257\r\n`
    - B: `a: 1, 2, 3, 4, 5, ..., 257\r\n`
- A & B are _equivalent_. Yet, A would be rejected and B accepted.
That's not right.
 
### Modifications:

Restore the old limits.

### Result:

- More compatible (because we allow more than 256 headers)
- More secure (because the default goes back to 80kB)
2026-06-30 09:22:19 +00:00
Fabian Fett b24872d3aa Merge commit from fork
Add configurable HTTP/1 decoder parsing limits

### Motivation:

The HTTP/1 decoder previously enforced a single hardcoded limit (80 KB per header field) and placed no cap on the total header-block size or the number of header fields per message. That made resource use unpredictable on requests with very many or very large headers, and there was no way for users to tune the limits for their workload.

### Modifications:

- Introduce a new public `NIOHTTPDecoderLimitConfiguration` exposing three parsing limits:
  - maxHeaderFieldSize — maximum size of a single header field (name + value). Default: 80 KB.
  - maxHeaderListSize — maximum total bytes of all header field names and values combined per message. Default: 2 MB.
  - maxHeaderFieldCount — maximum number of header fields per message, including trailers. Default: 256.
- Plumb the configuration through `HTTPDecoder` / `HTTPRequestDecoder` / `HTTPResponseDecoder` initializers. The existing initializers continue to work unchanged via convenience overloads that fall back to
  default limits.
- Expose the configuration on `NIOUpgradableHTTPServerPipelineConfiguration` and `NIOUpgradableHTTPClientPipelineConfiguration` so users of the typed pipeline setup can override the defaults.
- Track per-message totals in `BetterHTTPParser` and reject with `HTTPParserError.headerOverflow` when a limit is exceeded; counters reset at each new message so HTTP/1 pipelined connections behave
  correctly.
- Add tests covering the success and error paths for each limit, the default behaviors, and that trailer fields count toward the field-count cap.

### Result:

Users can configure the HTTP/1 decoder's per-field-size, total-list-size, and field-count limits. Existing call sites continue to compile and run unchanged, now with bounded defaults across all three dimensions instead of just one.
2026-05-20 10:00:56 +01:00