mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-18 17:55:26 +01:00
s3: route ';' queries as authorized, and report an empty s3:prefix to policies (#12702)
* s3: escape raw semicolons before the router matches a route registerRouter applied EscapeSemicolonsInQuery with router.Use, but mux runs router middleware only after it has picked the route. Queries() matchers split pairs on ';', so GET /bucket?uploads;x=1 reached the multipart listing handler while the handler and the IAM resolver read uploads%3Bx=1 and saw no uploads subresource. Wrap the S3 router with the escape so routing and authorization read the same query. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: report a listing without prefix as s3:prefix "" in policy conditions The advanced IAM path already sets s3:prefix to "" when a bucket listing names no prefix, as AWS does, but bucket policies and attached policies read conditions from ExtractConditionValuesFromRequest, which leaves the key out. The AWS home-directory policy that grants s3:prefix ["", "home/"] then denied the root listing, and a Deny keyed on the empty prefix never applied. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3: test the served handler composition for the semicolon escape Move the escape and the Vectors split into s3Handler so a command test can send ';' queries through the handler the server actually serves. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3: keep the isVectorsRequest doc comment on its function 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
1d30b0de21
commit
c4e1802e3e
7 files changed
+132
-12
No files matched your search
+18
-11
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user