diff --git a/weed/command/s3.go b/weed/command/s3.go index 6a6607f5a4..99c3506e78 100644 --- a/weed/command/s3.go +++ b/weed/command/s3.go @@ -449,17 +449,7 @@ func (s3opt *S3Options) startS3Server() bool { // The Vectors API rides the main -port listener whenever a weed-vector // backend is reachable — /s3vectors/ in the SigV4 credential scope picks // the vectors router. - vectorsHandler := http.Handler(router) - if vectorsRouter := s3opt.vectorsRouter(s3ApiServer, grpcDialOption, masterAddresses, filerGroup); vectorsRouter != nil { - vr, mainRouter := vectorsRouter, router - vectorsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if isVectorsRequest(r) { - vr.ServeHTTP(w, r) - return - } - mainRouter.ServeHTTP(w, r) - }) - } + vectorsHandler := s3Handler(router, s3opt.vectorsRouter(s3ApiServer, grpcDialOption, masterAddresses, filerGroup)) if runtime.GOOS != "windows" { localSocket := *s3opt.localSocket @@ -768,6 +758,23 @@ func (s3opt *S3Options) vectorsRouter(s3ApiServer *s3api.S3ApiServer, grpcDialOp return vectorsRouter } +// s3Handler serves router, and vectors for Vectors API requests when set. mux +// matches Queries() before router middleware runs, so the escape wraps the +// router for routing and authorization to read one query. +func s3Handler(router, vectors *mux.Router) http.Handler { + main := util_http.EscapeSemicolonsInQuery(router) + if vectors == nil { + return main + } + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if isVectorsRequest(r) { + vectors.ServeHTTP(w, r) + return + } + main.ServeHTTP(w, r) + }) +} + // isVectorsRequest routes by the SigV4 credential scope — the s3vectors // service name lives there, not in the path. func isVectorsRequest(r *http.Request) bool { diff --git a/weed/command/s3_handler_test.go b/weed/command/s3_handler_test.go new file mode 100644 index 0000000000..a26141545e --- /dev/null +++ b/weed/command/s3_handler_test.go @@ -0,0 +1,30 @@ +package command + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/gorilla/mux" + "github.com/stretchr/testify/assert" +) + +// A raw ';' pair must not select a Queries() route that the handler, reading +// the escaped query, does not see. +func TestS3HandlerEscapesSemicolonsBeforeRouting(t *testing.T) { + router := mux.NewRouter().SkipClean(true) + var served string + var hasUploads bool + router.Methods(http.MethodGet).Path("/{bucket}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + served, hasUploads = "uploads", r.URL.Query().Has("uploads") + }).Queries("uploads", "") + router.Methods(http.MethodGet).Path("/{bucket}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + served, hasUploads = "list", r.URL.Query().Has("uploads") + }) + + for query, want := range map[string]string{"uploads": "uploads", "uploads;x=1": "list", "x=1;uploads": "list"} { + s3Handler(router, nil).ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/bkt?"+query, nil)) + assert.Equal(t, want, served, query) + assert.Equal(t, want == "uploads", hasUploads, query) + } +} diff --git a/weed/s3api/auth_credentials.go b/weed/s3api/auth_credentials.go index ad5a2c0e68..f314926680 100644 --- a/weed/s3api/auth_credentials.go +++ b/weed/s3api/auth_credentials.go @@ -1948,6 +1948,14 @@ func isBucketListingRequest(r *http.Request, action Action) bool { return object == "" || object == "/" } +// addEmptyListPrefix reports a listing that names no prefix as s3:prefix "", +// as AWS and the advanced IAM path do. +func addEmptyListPrefix(conditions map[string][]string, r *http.Request, action Action) { + if _, ok := conditions["s3:prefix"]; !ok && isBucketListingRequest(r, action) { + conditions["s3:prefix"] = []string{""} + } +} + // multipartListingAction returns the scoped action for a request the router // sends to ListObjectPartsHandler or ListMultipartUploadsHandler, or "" for // any other request. It follows the route order in s3api_server.go and reads @@ -2752,6 +2760,7 @@ func (iam *IdentityAccessManagement) evaluateAttachedIAMPolicies(r *http.Request principal := buildPrincipalARN(identity, r) explicitAllow := false conditions := engine.ExtractConditionValuesFromRequest(r) + addEmptyListPrefix(conditions, r, action) for key, values := range vectorIAMConditions(r) { conditions[key] = values } diff --git a/weed/s3api/iam_list_prefix_regression_test.go b/weed/s3api/iam_list_prefix_regression_test.go index 77f46895de..a1030b98dd 100644 --- a/weed/s3api/iam_list_prefix_regression_test.go +++ b/weed/s3api/iam_list_prefix_regression_test.go @@ -86,6 +86,50 @@ func TestEvaluateIAMPolicies_ListBucketPrefixCondition(t *testing.T) { "prefix outside the s3:prefix condition must be denied") } +// A listing without ?prefix is the empty prefix, so the home-directory policy +// that grants s3:prefix "" for the root level matches it in attached and +// bucket policies alike. +func TestListBucketWithoutPrefixMatchesEmptyPrefix(t *testing.T) { + const bucket = "test-bucket" + statement := map[string]any{ + "Effect": "Allow", + "Action": "s3:ListBucket", + "Resource": "arn:aws:s3:::" + bucket, + "Condition": map[string]any{"StringEquals": map[string]any{ + "s3:prefix": []string{"", "home/"}, + "s3:delimiter": "/", + }}, + } + iam := &IdentityAccessManagement{} + require.NoError(t, iam.PutPolicy("home-root", mustPolicy(t, map[string]any{ + "Version": "2012-10-17", "Statement": []map[string]any{statement}, + }))) + identity := &Identity{Name: "carol", Account: &AccountAdmin, PolicyNames: []string{"home-root"}} + + statement["Principal"] = "*" + engine := NewBucketPolicyEngine() + require.NoError(t, engine.engine.SetBucketPolicy(bucket, mustPolicy(t, map[string]any{ + "Version": "2012-10-17", "Statement": []map[string]any{statement}, + }))) + + for _, tt := range []struct { + query string + allowed bool + }{ + {"list-type=2&delimiter=/", true}, + {"list-type=2&prefix=&delimiter=/", true}, + {"list-type=2&prefix=home/&delimiter=/", true}, + {"list-type=2&prefix=other/&delimiter=/", false}, + {"list-type=2", false}, + } { + req := httptest.NewRequest("GET", "/"+bucket+"?"+tt.query, nil) + require.Equal(t, tt.allowed, iam.evaluateIAMPolicies(req, identity, s3_constants.ACTION_LIST, bucket, ""), "attached policy, %s", tt.query) + allowed, evaluated, err := engine.EvaluatePolicy(bucket, "", string(s3_constants.ACTION_LIST), "arn:aws:iam::000000000000:user/carol", req, nil, nil) + require.NoError(t, err) + require.Equal(t, tt.allowed, evaluated && allowed, "bucket policy, %s", tt.query) + } +} + // Listing variants keep their specific action even when a prefix is promoted // into object: ?versions resolves to s3:ListBucketVersions, not s3:ListBucket. func TestEvaluateIAMPolicies_ListBucketVersionsWithPrefix(t *testing.T) { diff --git a/weed/s3api/s3api_ambiguous_subresource_test.go b/weed/s3api/s3api_ambiguous_subresource_test.go index 7af21e0a34..c53a18a5f9 100644 --- a/weed/s3api/s3api_ambiguous_subresource_test.go +++ b/weed/s3api/s3api_ambiguous_subresource_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/gorilla/mux" + util_http "github.com/seaweedfs/seaweedfs/weed/util/http" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -140,3 +141,32 @@ func TestRouteQueryKeysAreClassified(t *testing.T) { }) require.NoError(t, err) } + +// mux matches Queries() on the raw query, splitting pairs on ';', before any +// router middleware runs, while handlers and the IAM resolver read the escaped +// query. The server escapes ahead of the router so both see one operation. +func TestSemicolonQueryRoutesAsAuthorized(t *testing.T) { + router := mux.NewRouter().SkipClean(true) + setupRoutingTestServer(t).registerRouter(router) + match := func(r *http.Request) *mux.Route { + var m mux.RouteMatch + require.True(t, router.Match(r, &m)) + return m.Route + } + raw := func(query string) *mux.Route { + return match(httptest.NewRequest(http.MethodGet, "/bucket?"+query, nil)) + } + escaped := func(query string) (route *mux.Route) { + util_http.EscapeSemicolonsInQuery(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.False(t, r.URL.Query().Has("uploads")) + route = match(r) + })).ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/bucket?"+query, nil)) + return route + } + + uploads, listing := raw("uploads"), raw("x=1") + require.NotSame(t, uploads, listing) + assert.Same(t, uploads, raw("uploads;x=1"), "unescaped, mux reads the ';' pair as uploads") + assert.Same(t, listing, escaped("uploads;x=1")) + assert.Same(t, listing, escaped("x=1;uploads")) +} diff --git a/weed/s3api/s3api_bucket_policy_engine.go b/weed/s3api/s3api_bucket_policy_engine.go index b2708c486e..f2c105c710 100644 --- a/weed/s3api/s3api_bucket_policy_engine.go +++ b/weed/s3api/s3api_bucket_policy_engine.go @@ -147,6 +147,7 @@ func (bpe *BucketPolicyEngine) EvaluatePolicy(bucket, object, action, principal if r != nil { args.OriginalGrantConditions = policy_engine.OriginalGrantConditionsFromRequest(r) args.Conditions = bpe.engine.ExtractConditionValuesFromRequest(r) + addEmptyListPrefix(args.Conditions, r, Action(action)) // Extract principal-related variables (aws:username, etc.) from principal ARN principalVars := principalVariables(principal, claims) diff --git a/weed/s3api/s3api_server.go b/weed/s3api/s3api_server.go index 7afa6f8a71..0e8de408dc 100644 --- a/weed/s3api/s3api_server.go +++ b/weed/s3api/s3api_server.go @@ -962,7 +962,6 @@ func (s3a *S3ApiServer) UnifiedPostHandler(w http.ResponseWriter, r *http.Reques } func (s3a *S3ApiServer) registerRouter(router *mux.Router) { - // runs before subrouter matching so Queries() matchers see the fixed query router.Use(util_http.EscapeSemicolonsInQuery) // a request naming two subresources routes one way and authorizes another; // reject it rather than guess