Fix s3 auth failed with X-Forwarded-Host and X-Forwarded-Port (#6650)

This commit is contained in:
zuzuviewer 2025-03-21 11:47:07 +08:00 committed by GitHub
parent 4ad669b2aa
commit db759a1ad1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -25,6 +25,7 @@ import (
"encoding/hex"
"hash"
"io"
"net"
"net/http"
"net/url"
"regexp"
@ -720,16 +721,44 @@ func extractHostHeader(r *http.Request) string {
// If X-Forwarded-Port is set, use that too to form the host.
if forwardedHost != "" {
extractedHost := forwardedHost
if forwardedPort != "" && forwardedPort != "80" && forwardedPort != "443" {
extractedHost = forwardedHost + ":" + forwardedPort
host, port, err := net.SplitHostPort(extractedHost)
if err == nil {
extractedHost = host
if forwardedPort == "" {
forwardedPort = port
}
}
if !isDefaultPort(r.URL.Scheme, forwardedPort) {
extractedHost = net.JoinHostPort(forwardedHost, forwardedPort)
}
return extractedHost
} else {
// Go http server removes "host" from Request.Header
host := r.Host
if host == "" {
host = r.URL.Host
}
h, port, err := net.SplitHostPort(host)
if err != nil {
return r.Host
}
if isDefaultPort(r.URL.Scheme, port) {
return h
}
return r.Host
}
}
func isDefaultPort(scheme, port string) bool {
if port == "" {
return true
}
lowerCaseScheme := strings.ToLower(scheme)
return (lowerCaseScheme == "http" && port == "80") ||
(lowerCaseScheme == "https" && port == "443")
}
// getSignedHeaders generate a string i.e alphabetically sorted, semicolon-separated list of lowercase request header names
func getSignedHeaders(signedHeaders http.Header) string {
var headers []string