-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
reverseproxy: re-apply WebSocket header normalization after header ops #7786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sapirbaruch
wants to merge
3
commits into
caddyserver:master
Choose a base branch
from
sapirbaruch:fix-websocket-header-normalization
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package reverseproxy | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestNormalizeWebsocketHeaders(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input http.Header | ||
| want http.Header | ||
| }{ | ||
| { | ||
| name: "canonicalized headers are renamed to RFC 6455 form", | ||
| input: http.Header{ | ||
| // Go's http.CanonicalHeaderKey lowercases the 'S' in WebSocket: | ||
| // "Sec-WebSocket-Key" -> "Sec-Websocket-Key" | ||
| "Sec-Websocket-Key": {"dGhlIHNhbXBsZSBub25jZQ=="}, | ||
| "Sec-Websocket-Version": {"13"}, | ||
| "Sec-Websocket-Protocol": {"chat"}, | ||
| "Sec-Websocket-Extensions": {"permessage-deflate"}, | ||
| }, | ||
| want: http.Header{ | ||
| "Sec-WebSocket-Key": {"dGhlIHNhbXBsZSBub25jZQ=="}, | ||
| "Sec-WebSocket-Version": {"13"}, | ||
| "Sec-WebSocket-Protocol": {"chat"}, | ||
| "Sec-WebSocket-Extensions": {"permessage-deflate"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "already-correct headers are left unchanged", | ||
| input: http.Header{ | ||
| "Sec-WebSocket-Key": {"abc123"}, | ||
| "Sec-WebSocket-Version": {"13"}, | ||
| }, | ||
| want: http.Header{ | ||
| "Sec-WebSocket-Key": {"abc123"}, | ||
| "Sec-WebSocket-Version": {"13"}, | ||
| }, | ||
| }, | ||
| { | ||
| name: "non-WebSocket headers are untouched", | ||
| input: http.Header{"Content-Type": {"text/plain"}, "X-Foo": {"bar"}}, | ||
| want: http.Header{"Content-Type": {"text/plain"}, "X-Foo": {"bar"}}, | ||
| }, | ||
| { | ||
| name: "empty header map is a no-op", | ||
| input: http.Header{}, | ||
| want: http.Header{}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| normalizeWebsocketHeaders(tt.input) | ||
| for k, wantV := range tt.want { | ||
| gotV, ok := tt.input[k] | ||
| if !ok { | ||
| t.Errorf("missing header %q", k) | ||
| continue | ||
| } | ||
| if len(gotV) != len(wantV) || gotV[0] != wantV[0] { | ||
| t.Errorf("header %q: got %v, want %v", k, gotV, wantV) | ||
| } | ||
| } | ||
| // Ensure no extra keys remain (old canonical forms must be deleted). | ||
| for k := range tt.input { | ||
| if _, ok := tt.want[k]; !ok { | ||
| t.Errorf("unexpected header key left in map: %q", k) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestNormalizeWebsocketHeadersSurvivesCopyHeader is a regression test for | ||
| // https://github.com/caddyserver/caddy/issues/7784. | ||
| // | ||
| // proxyLoopIteration rebuilds r.Header with copyHeader when transport or header | ||
| // ops are configured. copyHeader uses http.Header.Add internally, which calls | ||
| // http.CanonicalHeaderKey and lowercases the 'S' in "WebSocket" to produce | ||
| // "Sec-Websocket-*". The fix calls normalizeWebsocketHeaders after the rebuild | ||
| // so the RFC 6455 casing is restored before the request is forwarded. | ||
| func TestNormalizeWebsocketHeadersSurvivesCopyHeader(t *testing.T) { | ||
| // Simulate the state of r.Header after copyHeader re-canonicalizes it. | ||
| rebuilt := make(http.Header) | ||
| // http.Header.Add canonicalizes to "Sec-Websocket-Key" (lowercase 's'). | ||
| rebuilt.Add("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ==") | ||
| rebuilt.Add("Sec-WebSocket-Version", "13") | ||
|
|
||
| // At this point the map contains the lowercase form. | ||
| if _, ok := rebuilt["Sec-Websocket-Key"]; !ok { | ||
| t.Fatal("test setup: expected canonical (lowercase) key to be present after Add") | ||
| } | ||
|
|
||
| // The fix: call normalizeWebsocketHeaders after the rebuild. | ||
| normalizeWebsocketHeaders(rebuilt) | ||
|
|
||
| // RFC 6455 form must be present (direct map lookup — .Get() re-canonicalizes | ||
| // to "Sec-Websocket-Key" and would miss the corrected key). | ||
| if _, ok := rebuilt["Sec-WebSocket-Key"]; !ok { | ||
| t.Error("Sec-WebSocket-Key missing after normalize; WebSocket upgrade will fail") | ||
| } | ||
| // Lowercase form must be gone. | ||
| if _, ok := rebuilt["Sec-Websocket-Key"]; ok { | ||
| t.Error("canonical (lowercase) Sec-Websocket-Key still present after normalize") | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A comment about why this is needed should be added. Otherwise it's not intuitive why websocket headers need to be normalized for non websockets requests.