-
-
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
7
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.
+203
−19
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8ccfcbc
reverseproxy: re-apply WebSocket header normalization after header ops
sapirbaruch 5e44b98
test(reverseproxy): add tests for normalizeWebsocketHeaders
sapirbaruch 04c494c
test: fix TestNormalizeWebsocketHeadersSurvivesCopyHeader assertion
sapirbaruch 9a90785
reverseproxy: test websocket header rebuild normalisation
steadytao 63d8d4a
Merge branch 'master' into fix-websocket-header-normalization
steadytao 1a4b26b
chore: explainer comment for normalizeWebsocketHeaders
steadytao 9987783
chore: clarify websocket header rebuild normalisation
steadytao 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,170 @@ | ||
| package reverseproxy | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/caddyserver/caddy/v2" | ||
| "github.com/caddyserver/caddy/v2/modules/caddyhttp/headers" | ||
| ) | ||
|
|
||
| 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) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestRebuildRequestHeadersPreservesWebsocketCasing 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 rebuild path must restore the RFC 6455 casing before | ||
| // the request is forwarded. | ||
| func TestRebuildRequestHeadersPreservesWebsocketCasing(t *testing.T) { | ||
| for _, tc := range []struct { | ||
| name string | ||
| handler Handler | ||
| }{ | ||
| { | ||
| name: "user header_ops only", | ||
| handler: Handler{ | ||
| Headers: &headers.Handler{ | ||
| Request: &headers.HeaderOps{ | ||
| Add: http.Header{"X-Custom": {"v"}}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "transport-injected Host op only", | ||
| handler: Handler{ | ||
| transportHeaderOps: &headers.HeaderOps{ | ||
| Set: http.Header{"Host": {"upstream.example.com"}}, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "transport and user ops together", | ||
| handler: Handler{ | ||
| transportHeaderOps: &headers.HeaderOps{ | ||
| Set: http.Header{"Host": {"upstream.example.com"}}, | ||
| }, | ||
| Headers: &headers.Handler{ | ||
| Request: &headers.HeaderOps{ | ||
| Add: http.Header{"X-Custom": {"v"}}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| reqHeader := http.Header{} | ||
| reqHeader["Sec-WebSocket-Key"] = []string{"dGhlIHNhbXBsZSBub25jZQ=="} | ||
| reqHeader["Sec-WebSocket-Version"] = []string{"13"} | ||
| reqHeader.Set("Connection", "Upgrade") | ||
| reqHeader.Set("Upgrade", "websocket") | ||
|
|
||
| req := httptest.NewRequest("GET", "http://example.com/", nil) | ||
| ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, caddy.NewReplacer()) | ||
| req = req.WithContext(ctx) | ||
|
|
||
| tc.handler.rebuildRequestHeaders(req, reqHeader, "upstream.example.com") | ||
|
|
||
| for _, key := range []string{"Sec-WebSocket-Key", "Sec-WebSocket-Version"} { | ||
| if _, ok := req.Header[key]; !ok { | ||
| t.Errorf("%q missing after rebuild; header = %v", key, req.Header) | ||
| } | ||
| canonical := http.CanonicalHeaderKey(key) | ||
| if canonical == key { | ||
| continue | ||
| } | ||
| if _, ok := req.Header[canonical]; ok { | ||
| t.Errorf("%q leaked after rebuild; header = %v", canonical, req.Header) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestRebuildRequestHeadersIsNoOpWithoutOps(t *testing.T) { | ||
| h := Handler{} | ||
| req := httptest.NewRequest("GET", "/", nil) | ||
| req.Header.Set("Original", "stays") | ||
| otherHeader := http.Header{"Different": {"should-not-appear"}} | ||
|
|
||
| h.rebuildRequestHeaders(req, otherHeader, "ignored") | ||
|
|
||
| if got := req.Header.Get("Original"); got != "stays" { | ||
| t.Errorf("header rebuilt despite no ops; Original = %q, want %q", got, "stays") | ||
| } | ||
| if got := req.Header.Get("Different"); got != "" { | ||
| t.Errorf("reqHeader leaked despite no ops; Different = %q, want empty", got) | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.