Skip to content

Validate custom HTTP response reason phrases - #1452

Merged
pjfanning merged 2 commits into
apache:mainfrom
He-Pin:fix/status-reason-validation
Nov 18, 2026
Merged

pjfanning merged 2 commits into
apache:mainfrom
He-Pin:fix/status-reason-validation

Conversation

@He-Pin

@He-Pin He-Pin commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Motivation

Custom status reasons currently accept characters outside the HTTP/1 reason-phrase grammar and can pass them to the status-line renderer. Keep constructed status objects representable as a valid reason phrase, rather than relying on each caller to validate its input.

This is model/output validation informed by Netty's related change. It does not claim a demonstrated downstream exploit.

Modification

  • Validate reasons in all six concrete status-code constructors, reached through the public Scala and Java factories.
  • Preserve empty reasons, HTAB, printable ASCII and the complete obs-text byte range. Reject other control characters, DEL, null and characters above 0xff, which the existing byte renderer cannot represent without truncation.
  • Validate once at construction, without adding a scan to each render. Error messages do not echo the invalid reason.
  • Add focused public Scala/Java factory, byte-rendering and CRLF/LF parser tests, and document the accepted range.

Result

Constructed status objects cannot carry an invalid reason phrase into HTTP/1 rendering. Parsing standard or registered status codes continues to use the registered reason. When parsing a response with an unregistered status code, control characters (other than HTAB) and DEL are dropped from the received reason instead of failing the response, following RFC 9112 section 4's advice that clients ignore the reason phrase. Applications that construct a status with an invalid reason now receive IllegalArgumentException.

Tests

The results below are historical validation. The latest compiler-only follow-up removes six calls to inaccessible private generated copy methods from tests to address Scala 3/next E173 errors; it makes no production change. Current test coverage uses the public Scala and Java factories and does not claim generated-copy coverage. The parser behavior was later changed to drop invalid characters from a received reason (commit b3c164c); project CI passed on the merged revision.

  • Baseline regression run: 3 failing tests, demonstrating the missing validation.
  • Updated focused reason tests: 9 passed; existing custom-status tests: 4 passed.
  • Full legal reason-byte range checked at model rendering, including exact bytes. Parser tests cover legal ASCII/HTAB/empty reasons, and check that invalid non-line-ending control characters are dropped from a received reason.
  • http-core / mimaReportBinaryIssues passed.
  • Scoped Compile/Test Scala formatting and header checks, Compile Java formatting, and git diff --check passed.
  • Independent adversarial source/diff review completed.
  • Global javafmtAll and docs / paradox were blocked by the unrelated http2-tests h2spec dependency download; scoped core checks passed. No full-suite or CI run was performed for this change.

References

Refs netty/netty#18331

Reason-phrase grammar: https://www.rfc-editor.org/rfc/rfc9112.html#section-4

@pjfanning pjfanning added this to the 2.1.0 milestone Nov 15, 2026

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Validating in the constructors keeps the rule in one place, and the error message doesn't echo the invalid reason, which is good. A few points:

  1. CI fails on Scala 3 and next. ResponseRendererSpec calls StatusCodes.Continue.copy(199)(...), StatusCodes.OK.copy(299)(...) etc. These case classes have private[StatusCodes] constructors, and on Scala 3 that makes the generated copy private too (E173 ... private[StatusCodes] method copy can only be accessed from object StatusCodes). So on Scala 3 copy isn't a public way to construct these at all, and the copy cases need to go (or be limited to 2.13).
  2. Failing to parse responses because of the reason phrase. With this change, an HTTP/1 response with an unregistered status code and a control character in its reason phrase now fails at message start in the client parser. RFC 9112 §4 says a client SHOULD ignore the reason-phrase content, so failing the whole response over it is the opposite of what's recommended. Rejecting invalid reasons when the application builds a status is reasonable, but on the parse path I'd sanitise instead: fall back to an empty or default reason, or drop the offending characters, so a sloppy server's status line doesn't make the response unusable.
  3. Docs. The paragraph added to http-model.md is fine for the custom-status API, but if point 2 changes, the sentence about rejecting invalid reasons when parsing needs updating.

He-Pin added a commit to He-Pin/incubator-pekko-http that referenced this pull request Nov 17, 2026
Motivation:
Keep constructed status reasons within the HTTP/1 reason-phrase grammar.
The test suite must also compile where generated case-class copy methods
inherit constructor visibility, including Scala 3 and Scala next.

Modification:
Validate concrete status constructors, preserve legal bytes, cover public
factories, and document unknown-status parsing behavior. Remove test calls
to inaccessible generated copy methods while retaining public factory
coverage. Keep parser and validation behavior unchanged by this CI fix.

Result:
The original construction-time validation remains intact. Tests no longer
rely on case-class copy methods that are not public across Scala versions.
The separate review question about parser behavior remains unresolved.

Tests:
- git diff --check: passed.
- Static check: no generated copy calls remain in ResponseRendererSpec;
  existing public factory coverage and imports are retained.
- Scala compilation, tests, MiMa and formatting were not run for this
  revision: a working full JDK is unavailable. Prior-head reported passes
  are not evidence that this revised commit passes.

References:
Refs apache#1452 and PJ Fanning review 5938963618.
Refs netty/netty#18331 and RFC 9112 section 4.
@He-Pin
He-Pin force-pushed the fix/status-reason-validation branch from 8920091 to fed72dd Compare November 17, 2026 15:46
He-Pin and others added 2 commits November 17, 2026 19:37
Motivation:
Keep constructed status reasons within the HTTP/1 reason-phrase grammar.
The test suite must also compile where generated case-class copy methods
inherit constructor visibility, including Scala 3 and Scala next.

Modification:
Validate concrete status constructors, preserve legal bytes, cover public
factories, and document unknown-status parsing behavior. Remove test calls
to inaccessible generated copy methods while retaining public factory
coverage. Keep parser and validation behavior unchanged by this CI fix.

Result:
The original construction-time validation remains intact. Tests no longer
rely on case-class copy methods that are not public across Scala versions.
The separate review question about parser behavior remains unresolved.

Tests:
- git diff --check: passed.
- Static check: no generated copy calls remain in ResponseRendererSpec;
  existing public factory coverage and imports are retained.
- Scala compilation, tests, MiMa and formatting were not run for this
  revision: a working full JDK is unavailable. Prior-head reported passes
  are not evidence that this revised commit passes.

References:
Refs apache#1452 and PJ Fanning review 5938963618.
Refs netty/netty#18331 and RFC 9112 section 4.
Motivation:
With reason validation in the status constructors, an HTTP/1 response with
an unregistered status code and a control character in its reason phrase
failed at message start. RFC 9112 section 4 says a client SHOULD ignore
the reason-phrase content, so the reason should not make the response
unusable.

Modification:
Remove control characters other than HTAB, and DEL, from the received
reason before building the custom status. Update the parser test and the
http-model docs accordingly. Application-constructed statuses are still
validated.

Result:
Responses from servers with sloppy reason phrases still parse; the status
code and the remaining reason text are kept.

Tests:
- sbt "http-core/testOnly ...ResponseParserCRLFSpec ...ResponseParserLFSpec ...ResponseRendererSpec": 111 passed

References:
Refs apache#1452 review
@pjfanning
pjfanning force-pushed the fix/status-reason-validation branch from fed72dd to b3c164c Compare November 17, 2026 18:39
@pjfanning

Copy link
Copy Markdown
Member

I rebased this onto main and pushed a separate commit (b3c164c). When parsing a response with an unregistered status code, control characters (other than HTAB) and DEL are now dropped from the received reason instead of failing the response, following RFC 9112 §4's advice that clients ignore the reason phrase. Statuses built by the application are still validated. Updated the parser test and the http-model docs. Feel free to revert anything you don't agree with.

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@pjfanning
pjfanning merged commit b2f6e58 into apache:main Nov 18, 2026
6 checks passed
pjfanning added a commit that referenced this pull request Nov 20, 2026
Motivation:
A status code reason is rendered as-is into the HTTP/1 status line, so a
custom reason containing CR or LF could inject extra lines into a response.

Modification:
Validate the reason in every StatusCode constructor against the RFC 9112
section 4 reason-phrase grammar (HTAB, SP, VCHAR, obs-text) and throw
IllegalArgumentException otherwise. When parsing a response with an
unregistered status code, drop control characters and DEL from the
received reason instead of failing the response.

Result:
Application-constructed status codes can't carry characters that break
HTTP/1 framing, and responses with sloppy reason phrases still parse.

Tests:
- ResponseParserSpec, ResponseRendererSpec (backport of #1452)

References:
Backport of #1452 (b2f6e58)

Co-authored-by: He-Pin(kerr) <[email protected]>
pjfanning added a commit that referenced this pull request Nov 20, 2026
Motivation:
A status code reason is rendered as-is into the HTTP/1 status line, so a
custom reason containing CR or LF could inject extra lines into a response.

Modification:
Validate the reason in every StatusCode constructor against the RFC 9112
section 4 reason-phrase grammar (HTAB, SP, VCHAR, obs-text) and throw
IllegalArgumentException otherwise. When parsing a response with an
unregistered status code, drop control characters and DEL from the
received reason instead of failing the response.

Result:
Application-constructed status codes can't carry characters that break
HTTP/1 framing, and responses with sloppy reason phrases still parse.

Tests:
- ResponseParserSpec, ResponseRendererSpec (backport of #1452)

References:
Backport of #1452 (b2f6e58)

Co-authored-by: He-Pin(kerr) <[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.

2 participants