Skip to content

Accept Connection token lists in WebSocket client handshakes - #1428

Merged
pjfanning merged 1 commit into
apache:mainfrom
He-Pin:fix/websocket-connection-tokens
Nov 18, 2026
Merged

pjfanning merged 1 commit into
apache:mainfrom
He-Pin:fix/websocket-connection-tokens

Conversation

@He-Pin

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

Copy link
Copy Markdown
Member

Motivation

The client currently compares the entire Connection response field to upgrade. This rejects a valid WebSocket handshake such as Connection: keep-alive, Upgrade, although RFC 6455 section 4.1 requires a case-insensitive token match, not an exact field-value match.

Modification

  • Reuse Connection.hasUpgrade when validating the client's opening handshake.
  • Cover mixed case and an upgrade token at the beginning, middle, or end of a list.
  • Verify that missing upgrade tokens and partial token matches are still rejected.

Result

The client accepts valid single-field Connection token lists without weakening the remaining handshake checks. No public API or visibility changes are made.

This change is deliberately scoped to token matching within one field. Handling of repeated Connection response fields in the HTTP parser is unchanged.

Tests

  • The regression handshake failed on unchanged main with the existing wrong-value diagnostic.
  • WebSocket package tests passed: 184 tests, 12 existing pending.
  • Final WebSocketClientSpec rerun: 23/23 passed, including the additional middle-token case.
  • http-core/mimaReportBinaryIssues, cross-Scala headerCheckAll, and scoped scalafmt checks passed.
  • docs/paradox passed (existing duplicate-anchor warnings).
  • Independent adversarial review completed.
  • Full validatePullRequest and CI were not run.

References

RFC 6455 section 4.1, client response validation step 3.

@pjfanning pjfanning added this to the 2.1.0 milestone Nov 13, 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, this looks right. Reusing Connection.hasUpgrade gives the case-insensitive token match RFC 6455 §4.1 asks for, and matches what the server side already does in Handshake. The negative cases (xupgrade, upgraded) show it's a token match, not a substring match. A couple of points:

  1. Follow-up: repeated Connection lines. HttpMessageParser merges the tokens of repeated Connection header lines only into its internal ch value (used for keep-alive/close). The message's header list keeps just the first line. So Connection: keep-alive followed by a separate Connection: Upgrade line is still rejected by _.header[Connection]. The server-side check has the same limitation. You already call this out of scope in the description, which is fine, but it might be worth a follow-up issue, since split list fields are valid HTTP.
  2. Docs. The two new sentences in websocket-support.md describe standard handshake validation that users don't control. I'd leave them out.

He-Pin added a commit to He-Pin/incubator-pekko-http that referenced this pull request Nov 17, 2026
Motivation:
The guide need not repeat standard upgrade token validation rules.

Modification:
Remove the two statements describing client handshake token checks.

Result:
The guide stays focused on API usage; implementation and tests are unchanged.

Tests:
Not run - docs only.

References:
Refs apache#1428 and PJ's review.
He-Pin added a commit to He-Pin/incubator-pekko-http that referenced this pull request Nov 17, 2026
Motivation:
Whole-field matching rejects valid responses whose Connection field
contains Upgrade alongside other tokens.

Modification:
Use Connection.hasUpgrade and test token positions, mixed case and
nonmatching tokens. Keep the usage guide focused on the public API.

Result:
The client accepts valid token lists within one field while retaining
the other handshake checks. Repeated field handling is unchanged.

Tests:
The existing runtime patch passed 184 WebSocket package tests and 23
focused client tests. The follow-up is docs-only. No new runtime tests
or CI query for this squash.

References:
Refs apache#1428 and RFC 6455 section 4.1.
@He-Pin
He-Pin force-pushed the fix/websocket-connection-tokens branch from 1b765c5 to 672b809 Compare November 17, 2026 15:12
@He-Pin
He-Pin requested a review from pjfanning November 17, 2026 15:13
Motivation:
Whole-field matching rejects valid responses whose Connection field
contains Upgrade alongside other tokens.

Modification:
Use Connection.hasUpgrade and test token positions, mixed case and
nonmatching tokens. Keep the usage guide focused on the public API.

Result:
The client accepts valid token lists within one field while retaining
the other handshake checks. Repeated field handling is unchanged.

Tests:
The existing runtime patch passed 184 WebSocket package tests and 23
focused client tests. The follow-up is docs-only. No new runtime tests
or CI query for this squash.

References:
Refs apache#1428 and RFC 6455 section 4.1.
@pjfanning

Copy link
Copy Markdown
Member

I rebased this onto main after #1436 was merged and resolved the conflicts. In Handshake.validateResponse, the Connection token check now sits alongside #1436's extension check, and WebSocketClientSpec keeps both sets of new tests (35/35 pass locally). No other changes.

@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 ebffe54 into apache:main Nov 18, 2026
6 checks passed
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