reverseproxy: Use correct cases for websocket related headers (#6621)
Some checks failed
Tests / test (./cmd/caddy/caddy, ~1.22.3, ubuntu-latest, 0, 1.22, linux) (push) Failing after 1m38s
Tests / test (./cmd/caddy/caddy, ~1.23.0, ubuntu-latest, 0, 1.23, linux) (push) Failing after 1m23s
Tests / test (s390x on IBM Z) (push) Has been skipped
Tests / goreleaser-check (push) Successful in 2m55s
Cross-Build / build (~1.22.3, 1.22, aix) (push) Successful in 1m33s
Cross-Build / build (~1.22.3, 1.22, darwin) (push) Successful in 1m33s
Cross-Build / build (~1.22.3, 1.22, dragonfly) (push) Successful in 1m33s
Cross-Build / build (~1.22.3, 1.22, freebsd) (push) Successful in 1m31s
Cross-Build / build (~1.22.3, 1.22, illumos) (push) Successful in 1m32s
Cross-Build / build (~1.22.3, 1.22, linux) (push) Successful in 1m37s
Cross-Build / build (~1.22.3, 1.22, netbsd) (push) Successful in 1m32s
Cross-Build / build (~1.22.3, 1.22, openbsd) (push) Successful in 1m38s
Cross-Build / build (~1.22.3, 1.22, solaris) (push) Successful in 1m33s
Cross-Build / build (~1.22.3, 1.22, windows) (push) Successful in 1m33s
Cross-Build / build (~1.23.0, 1.23, aix) (push) Successful in 1m26s
Cross-Build / build (~1.23.0, 1.23, darwin) (push) Successful in 1m24s
Cross-Build / build (~1.23.0, 1.23, dragonfly) (push) Successful in 1m21s
Cross-Build / build (~1.23.0, 1.23, freebsd) (push) Successful in 1m24s
Cross-Build / build (~1.23.0, 1.23, illumos) (push) Successful in 1m23s
Cross-Build / build (~1.23.0, 1.23, linux) (push) Successful in 1m26s
Cross-Build / build (~1.23.0, 1.23, netbsd) (push) Successful in 1m25s
Cross-Build / build (~1.23.0, 1.23, openbsd) (push) Successful in 1m26s
Cross-Build / build (~1.23.0, 1.23, solaris) (push) Successful in 1m26s
Cross-Build / build (~1.23.0, 1.23, windows) (push) Successful in 1m26s
Lint / lint (ubuntu-latest, linux) (push) Successful in 2m6s
Lint / govulncheck (push) Successful in 1m16s
Tests / test (./cmd/caddy/caddy, ~1.22.3, macos-14, 0, 1.22, mac) (push) Has been cancelled
Tests / test (./cmd/caddy/caddy, ~1.23.0, macos-14, 0, 1.23, mac) (push) Has been cancelled
Tests / test (./cmd/caddy/caddy.exe, ~1.22.3, windows-latest, True, 1.22, windows) (push) Has been cancelled
Tests / test (./cmd/caddy/caddy.exe, ~1.23.0, windows-latest, True, 1.23, windows) (push) Has been cancelled
Lint / lint (macos-14, mac) (push) Has been cancelled
Lint / lint (windows-latest, windows) (push) Has been cancelled

Co-authored-by: Francis Lavoie <lavofr@gmail.com>
This commit is contained in:
WeidiDeng 2024-10-11 17:02:23 +08:00 committed by GitHub
parent ef4e0224a8
commit 48ce47f1d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 0 deletions

View File

@ -569,6 +569,30 @@ func (h *Handler) proxyLoopIteration(r *http.Request, origReq *http.Request, w h
return false, proxyErr
}
// Mapping of the canonical form of the headers, to the RFC 6455 form,
// i.e. `WebSocket` with uppercase 'S'.
var websocketHeaderMapping = map[string]string{
"Sec-Websocket-Accept": "Sec-WebSocket-Accept",
"Sec-Websocket-Extensions": "Sec-WebSocket-Extensions",
"Sec-Websocket-Key": "Sec-WebSocket-Key",
"Sec-Websocket-Protocol": "Sec-WebSocket-Protocol",
"Sec-Websocket-Version": "Sec-WebSocket-Version",
}
// normalizeWebsocketHeaders ensures we use the standard casing as per
// RFC 6455, i.e. `WebSocket` with uppercase 'S'. Most servers don't
// care about this difference (read headers case insensitively), but
// some do, so this maximizes compatibility with upstreams.
// See https://github.com/caddyserver/caddy/pull/6621
func normalizeWebsocketHeaders(header http.Header) {
for k, rk := range websocketHeaderMapping {
if v, ok := header[k]; ok {
delete(header, k)
header[rk] = v
}
}
}
// prepareRequest clones req so that it can be safely modified without
// changing the original request or introducing data races. It then
// modifies it so that it is ready to be proxied, except for directing
@ -655,6 +679,7 @@ func (h Handler) prepareRequest(req *http.Request, repl *caddy.Replacer) (*http.
if reqUpType != "" {
req.Header.Set("Connection", "Upgrade")
req.Header.Set("Upgrade", reqUpType)
normalizeWebsocketHeaders(req.Header)
}
// Set up the PROXY protocol info

View File

@ -66,6 +66,7 @@ func (h *Handler) handleUpgradeResponse(logger *zap.Logger, wg *sync.WaitGroup,
// write header first, response headers should not be counted in size
// like the rest of handler chain.
copyHeader(rw.Header(), res.Header)
normalizeWebsocketHeaders(rw.Header())
rw.WriteHeader(res.StatusCode)
logger.Debug("upgrading connection")