mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
s3api: sign the virtual-hosted bucket in signature V2 (#12835)
* s3api: sign the virtual-hosted bucket in signature V2 Signature V2 signed only the request path, so a path-style signed or presigned request replayed with a virtual-host Host header reached another bucket. Prefix the bucket taken from the Host to the canonical resource, as AWS does, and refuse a request routed to a different bucket. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: test signature V2 against virtual-host retargeting Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: match port-qualified virtual-host domains like the router With -s3.domainName=s3.test:8333 the router matches the full host:port, but bucketFromVirtualHost compared the port-stripped host and never found the bucket, so the virtual-host checks did not apply. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7d52383ccf
commit
d68cde4cfc
4 files changed
+108
-7
No files matched your search
@@ -171,7 +171,11 @@ func (iam *IdentityAccessManagement) doesSignV2Match(r *http.Request) (*Identity
|
||||
return nil, s3err.ErrAccessDenied
|
||||
}
|
||||
|
||||
expectedAuth := signatureV2(cred, r.Method, r.URL.EscapedPath(), r.URL.Query().Encode(), r.Header)
|
||||
resource, errCode := iam.encodedResourceV2(r)
|
||||
if errCode != s3err.ErrNone {
|
||||
return nil, errCode
|
||||
}
|
||||
expectedAuth := signatureV2(cred, r.Method, resource, r.URL.Query().Encode(), r.Header)
|
||||
|
||||
// Extract signatures from both auth headers
|
||||
v2Signature := ""
|
||||
@@ -250,7 +254,11 @@ func (iam *IdentityAccessManagement) doesPresignV2SignatureMatch(r *http.Request
|
||||
return nil, s3err.ErrAccessDenied
|
||||
}
|
||||
|
||||
expectedSignature := preSignatureV2(cred, r.Method, r.URL.EscapedPath(), r.URL.Query().Encode(), r.Header, expires)
|
||||
resource, errCode := iam.encodedResourceV2(r)
|
||||
if errCode != s3err.ErrNone {
|
||||
return nil, errCode
|
||||
}
|
||||
expectedSignature := preSignatureV2(cred, r.Method, resource, r.URL.Query().Encode(), r.Header, expires)
|
||||
if !compareSignatureV2(signature, expectedSignature) {
|
||||
return nil, s3err.ErrSignatureDoesNotMatch
|
||||
}
|
||||
@@ -260,6 +268,19 @@ func (iam *IdentityAccessManagement) doesPresignV2SignatureMatch(r *http.Request
|
||||
}
|
||||
|
||||
// validateV2AuthHeader validates AWS Signature Version '2' authentication header.
|
||||
// encodedResourceV2 returns the request path with the virtual-hosted bucket
|
||||
// prefixed, as signature V2 signs it.
|
||||
func (iam *IdentityAccessManagement) encodedResourceV2(r *http.Request) (string, s3err.ErrorCode) {
|
||||
bucket, ok := bucketFromVirtualHost(r.Host, iam.domain)
|
||||
if !ok {
|
||||
return r.URL.EscapedPath(), s3err.ErrNone
|
||||
}
|
||||
if routedBucket, _ := s3_constants.GetBucketAndObject(r); routedBucket != "" && routedBucket != bucket {
|
||||
return "", s3err.ErrAccessDenied
|
||||
}
|
||||
return "/" + bucket + r.URL.EscapedPath(), s3err.ErrNone
|
||||
}
|
||||
|
||||
func validateV2AuthHeader(v2Auth string) (accessKey string, errCode s3err.ErrorCode) {
|
||||
if v2Auth == "" {
|
||||
return "", s3err.ErrAuthHeaderEmpty
|
||||
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
||||
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
|
||||
)
|
||||
@@ -518,3 +520,66 @@ func TestSignatureV2RejectsRepeatedSubresource(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignatureV2SignsVirtualHostBucket(t *testing.T) {
|
||||
iam := setupTestIAMForV2Auth()
|
||||
cred := &Credential{
|
||||
AccessKey: "AKIAIOSFODNN7EXAMPLE",
|
||||
SecretKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
|
||||
}
|
||||
expires := strconv.FormatInt(time.Now().Add(time.Hour).Unix(), 10)
|
||||
date := time.Now().UTC().Format(http.TimeFormat)
|
||||
|
||||
presigned := func(host, path, resource, routedBucket string) *http.Request {
|
||||
req, err := http.NewRequest(http.MethodGet, "http://"+host+path, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("new request: %v", err)
|
||||
}
|
||||
query := req.URL.Query()
|
||||
query.Set("AWSAccessKeyId", cred.AccessKey)
|
||||
query.Set("Expires", expires)
|
||||
query.Set("Signature", calculateSignatureV2("GET\n\n\n"+expires+"\n"+resource, cred.SecretKey))
|
||||
req.URL.RawQuery = query.Encode()
|
||||
return mux.SetURLVars(req, map[string]string{"bucket": routedBucket})
|
||||
}
|
||||
signed := func(host, path, resource, routedBucket string) *http.Request {
|
||||
req, err := http.NewRequest(http.MethodGet, "http://"+host+path, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("new request: %v", err)
|
||||
}
|
||||
req.Header.Set("Date", date)
|
||||
req.Header.Set("Authorization", signV2Algorithm+" "+cred.AccessKey+":"+calculateSignatureV2("GET\n\n\n"+date+"\n"+resource, cred.SecretKey))
|
||||
return mux.SetURLVars(req, map[string]string{"bucket": routedBucket})
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name, host, path, resource, routedBucket string
|
||||
want s3err.ErrorCode
|
||||
domain string
|
||||
}{
|
||||
{"path style", "s3.test:8333", "/alpha/key", "/alpha/key", "alpha", s3err.ErrNone, ""},
|
||||
{"virtual host object", "alpha.s3.test:8333", "/key", "/alpha/key", "alpha", s3err.ErrNone, ""},
|
||||
{"virtual host bucket root", "alpha.s3.test", "/", "/alpha/", "alpha", s3err.ErrNone, ""},
|
||||
{"path style retargeted by host", "beta.s3.test:8333", "/alpha/key", "/alpha/key", "beta", s3err.ErrSignatureDoesNotMatch, ""},
|
||||
{"virtual host retargeted by host", "beta.s3.test:8333", "/key", "/alpha/key", "beta", s3err.ErrSignatureDoesNotMatch, ""},
|
||||
{"routed to another bucket", "alpha.s3.test:8333", "/key", "/alpha/key", "beta", s3err.ErrAccessDenied, ""},
|
||||
{"port domain virtual host", "alpha.s3.test:8333", "/key", "/alpha/key", "alpha", s3err.ErrNone, "s3.test:8333"},
|
||||
{"port domain retargeted by host", "beta.s3.test:8333", "/alpha/key", "/alpha/key", "beta", s3err.ErrSignatureDoesNotMatch, "s3.test:8333"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
iam.domain = "s3.test"
|
||||
if tt.domain != "" {
|
||||
iam.domain = tt.domain
|
||||
}
|
||||
t.Run("presigned "+tt.name, func(t *testing.T) {
|
||||
if _, errCode := iam.doesPresignV2SignatureMatch(presigned(tt.host, tt.path, tt.resource, tt.routedBucket)); errCode != tt.want {
|
||||
t.Fatalf("got %v, want %v", errCode, tt.want)
|
||||
}
|
||||
})
|
||||
t.Run("signed "+tt.name, func(t *testing.T) {
|
||||
if _, errCode := iam.doesSignV2Match(signed(tt.host, tt.path, tt.resource, tt.routedBucket)); errCode != tt.want {
|
||||
t.Fatalf("got %v, want %v", errCode, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1133,19 +1133,29 @@ func bucketFromVirtualHost(host, domainConfig string) (string, bool) {
|
||||
if domainConfig == "" {
|
||||
return "", false
|
||||
}
|
||||
h := host
|
||||
host = strings.ToLower(host)
|
||||
hostWithoutPort := host
|
||||
if hh, _, err := net.SplitHostPort(host); err == nil {
|
||||
h = hh
|
||||
hostWithoutPort = hh
|
||||
}
|
||||
// The router matches a domain that names a port against the full host:port.
|
||||
hostFor := func(domain string) string {
|
||||
if strings.Contains(domain, ":") {
|
||||
return host
|
||||
}
|
||||
return hostWithoutPort
|
||||
}
|
||||
h = strings.ToLower(h)
|
||||
pathStyleDomains, virtualHostDomains := classifyDomainNames(strings.Split(domainConfig, ","))
|
||||
for _, domain := range pathStyleDomains {
|
||||
if h == strings.ToLower(strings.TrimSpace(domain)) {
|
||||
domain = strings.ToLower(strings.TrimSpace(domain))
|
||||
if hostFor(domain) == domain {
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
for _, domain := range virtualHostDomains {
|
||||
suffix := "." + strings.ToLower(strings.TrimSpace(domain))
|
||||
domain = strings.ToLower(strings.TrimSpace(domain))
|
||||
h := hostFor(domain)
|
||||
suffix := "." + domain
|
||||
if strings.HasSuffix(h, suffix) {
|
||||
bucket := h[:len(h)-len(suffix)]
|
||||
if bucket != "" {
|
||||
|
||||
@@ -2017,6 +2017,11 @@ func TestBucketFromVirtualHost(t *testing.T) {
|
||||
{"alpha-bkt.s3.test", "s3.test,develop.s3.test", "alpha-bkt", true},
|
||||
{"bucket.develop.s3.test", "s3.test,develop.s3.test", "bucket.develop", true},
|
||||
{"develop.s3.test", "s3.test,develop.s3.test", "", false},
|
||||
{"alpha-bkt.s3.test:8333", "s3.test:8333", "alpha-bkt", true},
|
||||
{"alpha-bkt.s3.test", "s3.test:8333", "", false},
|
||||
{"alpha-bkt.s3.test:9000", "s3.test:8333", "", false},
|
||||
{"s3.test:8333", "s3.test:8333", "", false},
|
||||
{"develop.s3.test:8333", "s3.test:8333,develop.s3.test:8333", "", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.host, func(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user