Skip to content

proxy: preserve buffered data after protocol upgrade - #4435

Merged
MustafaSaber merged 3 commits into
zalando:cwfreezefrom
perfloop-oss:perfloop-pr-open-cbnawn7qrx
Nov 24, 2026
Merged

MustafaSaber merged 3 commits into
zalando:cwfreezefrom
perfloop-oss:perfloop-pr-open-cbnawn7qrx

Conversation

@perfloop-agent

Copy link
Copy Markdown
Contributor

An experimental WebSocket upgrade can return 101 while losing the start of a server-first frame sent with the handshake. This can drop a small frame completely or corrupt a larger frame, with either HTTP or HTTPS backends.

Retain the backend response reader and drain its buffered bytes before copying the rest from the raw connection. Both direct and audited forwarding preserve byte order. When nothing was read ahead, the copy path is unchanged. Request-to-backend copying, non-101 response handling, and connection ownership remain unchanged.

TestUpgradeServerFirst in proxy/upgrade_test.go sends a handshake and frame in one TLS record, with auditing off and on. It covers payload and buffer boundaries, a split-record control, subsequent bidirectional traffic, exact audit output, and backend-initiated EOF.

The client-side Hijack reader is left unchanged: RFC 6455 section 4.1 requires clients to wait for the handshake response before sending more data, and net/http/httputil.ReverseProxy also copies from the hijacked connection.

The focused Go tests for server-first forwarding, existing upgrade-response handling, and audit logging passed, including the race-enabled run. Formatting, vet, and staticcheck checks passed for the changed proxy code.

The Redis-backed proxy tests TestBackendRatelimitRoundRobin and TestBackendRatelimitScenarios are left to target CI.

This requires ExperimentalUpgrade; it does not enable the option.

Fixes #4434.

Case and evidence


Made by Perfloop. Human sponsor: Tomás Senart.

Signed-off-by: Perfloop Agent <[email protected]>
Signed-off-by: Tomás Senart <[email protected]>
Forward the backend response reader after a successful upgrade so data read ahead with the handshake reaches the client and the audit log.

Signed-off-by: Perfloop Agent <[email protected]>
Signed-off-by: Tomás Senart <[email protected]>
Limit the response reader to its buffered prefix so bulk forwarding keeps the raw connection copy path. Leave forwarding unchanged when the handshake did not read ahead.

Signed-off-by: Perfloop Agent <[email protected]>
Signed-off-by: Tomás Senart <[email protected]>
@szuecs

szuecs commented Nov 16, 2026

Copy link
Copy Markdown
Member

@perfloop-agent can you create the PR to cwfreeze instead of master branch?
We are for 2-3 weeks only able to merge it to cwfreeze.

@perfloop-agent
perfloop-agent changed the base branch from master to cwfreeze November 16, 2026 09:43
@perfloop-agent

Copy link
Copy Markdown
Contributor Author

@szuecs Done: the PR now targets cwfreeze. It matched master, so the commit is unchanged.

The pr-labels check needs one of its required labels, which I cannot set. bugfix fits this change, if you agree.

@szuecs szuecs added the bugfix Bug fixes and patches label Nov 16, 2026
@MustafaSaber
MustafaSaber merged commit d70a998 into zalando:cwfreeze Nov 24, 2026
18 checks passed
szuecs pushed a commit that referenced this pull request Dec 1, 2026
An experimental WebSocket upgrade can return 101 while losing the start
of a server-first frame sent with the handshake. This can drop a small
frame completely or corrupt a larger frame, with either HTTP or HTTPS
backends.

Retain the backend response reader and drain its buffered bytes before
copying the rest from the raw connection. Both direct and audited
forwarding preserve byte order. When nothing was read ahead, the copy
path is unchanged. Request-to-backend copying, non-101 response
handling, and connection ownership remain unchanged.

`TestUpgradeServerFirst` in `proxy/upgrade_test.go` sends a handshake
and frame in one TLS record, with auditing off and on. It covers payload
and buffer boundaries, a split-record control, subsequent bidirectional
traffic, exact audit output, and backend-initiated EOF.

The client-side `Hijack` reader is left unchanged: [RFC 6455 section
4.1](https://www.rfc-editor.org/rfc/rfc6455#section-4.1) requires
clients to wait for the handshake response before sending more data, and
`net/http/httputil.ReverseProxy` also copies from the hijacked
connection.

The focused Go tests for server-first forwarding, existing
upgrade-response handling, and audit logging passed, including the
race-enabled run. Formatting, vet, and staticcheck checks passed for the
changed proxy code.

The Redis-backed proxy tests `TestBackendRatelimitRoundRobin` and
`TestBackendRatelimitScenarios` are left to target CI.

This requires `ExperimentalUpgrade`; it does not enable the option.

Fixes #4434.

[Case and evidence](<https://app.perfloop.ai/t/oss/case_bbsc737fya>)

---

Made by [Perfloop](https://app.perfloop.ai). Human sponsor: [Tomás
Senart](https://github.com/tsenart).

---------

Signed-off-by: Perfloop Agent <[email protected]>
Signed-off-by: Tomás Senart <[email protected]>
Signed-off-by: Sandor Szücs <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Bug fixes and patches

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Experimental upgrades lose server-first frames sent with the handshake

3 participants