mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2025-04-05 20:52:50 +08:00
Fix s3 auth failed with X-Forwarded-Host and X-Forwarded-Port (#6650)
This commit is contained in:
parent
4ad669b2aa
commit
db759a1ad1
@ -25,6 +25,7 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"hash"
|
"hash"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"regexp"
|
"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 X-Forwarded-Port is set, use that too to form the host.
|
||||||
if forwardedHost != "" {
|
if forwardedHost != "" {
|
||||||
extractedHost := forwardedHost
|
extractedHost := forwardedHost
|
||||||
if forwardedPort != "" && forwardedPort != "80" && forwardedPort != "443" {
|
host, port, err := net.SplitHostPort(extractedHost)
|
||||||
extractedHost = forwardedHost + ":" + forwardedPort
|
if err == nil {
|
||||||
|
extractedHost = host
|
||||||
|
if forwardedPort == "" {
|
||||||
|
forwardedPort = port
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !isDefaultPort(r.URL.Scheme, forwardedPort) {
|
||||||
|
extractedHost = net.JoinHostPort(forwardedHost, forwardedPort)
|
||||||
}
|
}
|
||||||
return extractedHost
|
return extractedHost
|
||||||
} else {
|
} else {
|
||||||
// Go http server removes "host" from Request.Header
|
// 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
|
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
|
// getSignedHeaders generate a string i.e alphabetically sorted, semicolon-separated list of lowercase request header names
|
||||||
func getSignedHeaders(signedHeaders http.Header) string {
|
func getSignedHeaders(signedHeaders http.Header) string {
|
||||||
var headers []string
|
var headers []string
|
||||||
|
Loading…
Reference in New Issue
Block a user