Skip to content

Move websocket header normalization later - #7921

Merged
mholt merged 4 commits into
caddyserver:masterfrom
nfreya:renormalize-copy-headers
Sep 23, 2026
Merged

mholt merged 4 commits into
caddyserver:masterfrom
nfreya:renormalize-copy-headers

Conversation

@nfreya

@nfreya nfreya commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

A regression since #6621, these headers can be rewritten by cloneRequest and by req.Headers.Add from e.g. user and transport ops. Moving this closer to where it's round-tripped feels like the nicest solution.

I've put the harness I used to reproduce the issue at
https://codeberg.org/nfreya/caddy-websocket-headers-repro

I encountered this issue while trying to reverse proxy the iKVM from an old SuperMicro BMC which was using some ancient noVNC fork, and I had a suspicion that the 400 response from the upstream was due to CanonicalMIME-related normalization combined with the sort of naive strstr you'd expect in the C code of a firmware written under ... 'constraints.' From there I found the comment referencing #6621 and the open #7784. Since that more recent one has AI usage, I didn't look further into it on principle, and from skimming the corresponding #7786 now, it looks uglier than my approach of just moving the normalize down. Any unintended consequences of this movement would be limited to consumers of the headers that branch on case difference for a request between the old and new normalizeWebsocketHeaders call site, and that seems like a rare enough edge case with obviously unsupported semantics to be worth breaking for simplicity's sake. If some unit tests of this behaviour are necessary for acceptance we can do that, but the regression itself was caused in a way that seems difficult to guard against by anything other than relatively fragile integration testing, so I've left it at this.

git log -S showed me #7911 affecting something tangential since those allocations come from the same mismatch between CanonicalMIME and the original websocket RFC -- this PR doesn't make that situation strictly worse, as leaving these headers in CanonicalMIME form before the request is cloned however many times reverse_proxy ends up cloning it makes it less likely for that allocation to occur, but I also think the normalizeWebsocketHeaders method is unlikely do any allocations since it's just migrating a (pointer-to-const key, slice value) from one hashmap slot to another (unverified). The extra upgradeType call is in the path of every request, but memoizing that seems annoying just to eliminate what is just a scan of the map when there's so much other code in this path.

Assistance Disclosure

No AI was used.

@francislavoie

Copy link
Copy Markdown
Member

Seems to me like #7786 should cover this already

@steadytao

Copy link
Copy Markdown
Member

I think this placement is cleaner than 7786: it normalises once immediately before reverseProxy, after the request-header changes and only for upgrade requests. Please bring over a regression test which exercises this actual proxy-loop path and fails when the call is removed; with that coverage I would prefer this PR.

A regression since caddyserver#6621, these headers can be rewritten by cloneHeaders
and by .Add from user and transport ops. Moving this closer to where
it's round-tripped feels like the nicest solution.

I've put the harness I used to reproduce the issue at
  https://codeberg.org/nfreya/caddy-websocket-headers-repro
@nfreya
nfreya force-pushed the renormalize-copy-headers branch 4 times, most recently from 24f7ca9 to 34eb794 Compare August 23, 2026 13:32
@nfreya

nfreya commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Took a bit to get back to this, sorry. There's now an integration test for the WebSocket header normalization which fails if the first commit of the branch is removed. This catches the #7784 header_up case explicitly, which is not exactly the config I had with the https upstream, but they're equivalent as they both modify Headers after the unpatched normalizeWebsocketHeaders site, and header_up makes for an easier test to set up since it doesn't need TLS serving.

@steadytao steadytao 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.

No need to be sorry. Thank you very much for your work. Just a issue in goroutine handling but otherwise very lovely!

@nfreya
nfreya force-pushed the renormalize-copy-headers branch from 34eb794 to 2cfa67e Compare August 24, 2026 19:24

@steadytao steadytao 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.

Looking really good!

@nfreya
nfreya force-pushed the renormalize-copy-headers branch from 2cfa67e to b2756d9 Compare August 25, 2026 14:40

@steadytao steadytao 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.

Thank you very much, freya. LGTM.

@steadytao

Copy link
Copy Markdown
Member

Pending #7964 -- I hate CI :D

@steadytao steadytao added the bug 🐞 Something isn't working label Aug 28, 2026
@steadytao steadytao added this to the v2.11.5 milestone Aug 28, 2026
@steadytao steadytao self-assigned this Aug 28, 2026
@mholt
mholt merged commit 7ee4441 into caddyserver:master Sep 23, 2026
27 checks passed
@nfreya
nfreya deleted the renormalize-copy-headers branch September 23, 2026 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug 🐞 Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants