diff --git a/weed/s3api/auth_revoked_requests.go b/weed/s3api/auth_revoked_requests.go index a9116af9c3..5bc89266f6 100644 --- a/weed/s3api/auth_revoked_requests.go +++ b/weed/s3api/auth_revoked_requests.go @@ -121,6 +121,7 @@ func (iam *IdentityAccessManagement) abortWhenRevoked(f http.HandlerFunc, identi req.body = r.Body req.stream = http.NewResponseController(w) } + r = r.WithContext(context.WithValue(ctx, inFlightRequestKey{}, req)) tracked.add(req) defer tracked.remove(req) if tracked.generation.Load() != since { @@ -130,6 +131,20 @@ func (iam *IdentityAccessManagement) abortWhenRevoked(f http.HandlerFunc, identi } } +type inFlightRequestKey struct{} + +// untrackInFlight stops re-checking r before it stores a change to access, +// which would otherwise judge r by its own change and abort it after the +// change is stored. It reports false when r was aborted already, so the change +// must not be stored. +func (iam *IdentityAccessManagement) untrackInFlight(r *http.Request) bool { + req, ok := r.Context().Value(inFlightRequestKey{}).(*inFlightRequest) + if !ok || iam == nil || iam.inFlight == nil { + return true + } + return iam.inFlight.remove(req) +} + // identitySource tells from how the request authenticated how a re-check finds // its identity again, and the credential it was signed with. func (iam *IdentityAccessManagement) identitySource(r *http.Request, identity *Identity) (identitySource, *Credential) { @@ -251,10 +266,13 @@ func (t *inFlightRequests) add(req *inFlightRequest) { t.mu.Unlock() } -func (t *inFlightRequests) remove(req *inFlightRequest) { +// remove reports whether req was still tracked, that is not aborted. +func (t *inFlightRequests) remove(req *inFlightRequest) bool { t.mu.Lock() + defer t.mu.Unlock() + _, tracked := t.requests[req] delete(t.requests, req) - t.mu.Unlock() + return tracked } // pending takes the requests waiting for a re-check. diff --git a/weed/s3api/auth_revoked_requests_test.go b/weed/s3api/auth_revoked_requests_test.go index 7f40206d9d..fa5c74e9eb 100644 --- a/weed/s3api/auth_revoked_requests_test.go +++ b/weed/s3api/auth_revoked_requests_test.go @@ -286,6 +286,46 @@ func TestAbortRequestsDeniedByBucketPolicy(t *testing.T) { assert.NoError(t, downloadResult(t, other, 5*time.Second), "no policy covers other") } +// A request that untracks itself before storing its change to access keeps +// running when that change denies it, while a read the change denies is +// aborted. A request aborted before it untracks itself must not store it. +func TestUntrackInFlightBeforeOwnChange(t *testing.T) { + iam := newRevokeIAM(t, revokeIdentity("bob", revokeKeyB, revokeSecretB, "Admin")) + iam.policyEngine = NewBucketPolicyEngine() + started := make(chan struct{}, 2) + read := download(t, serveRevokeS3(t, started, authRead(iam))+"/bkt/object", revokeKeyB, revokeSecretB) + change := download(t, serveRevokeS3(t, started, func(f http.HandlerFunc) http.HandlerFunc { + return iam.Auth(func(w http.ResponseWriter, r *http.Request) { + if iam.untrackInFlight(r) { + f(w, r) + } + }, s3_constants.ACTION_PUT_BUCKET_POLICY) + })+"/bkt/object", revokeKeyB, revokeSecretB) + waitStarted(t, started, 2) + + require.NoError(t, iam.policyEngine.engine.SetBucketPolicy("bkt", + `{"Version":"2012-10-17","Statement":[{"Effect":"Deny","Principal":"*","Action":"s3:*","Resource":["arn:aws:s3:::bkt","arn:aws:s3:::bkt/*"]}]}`)) + iam.revalidateBucketInFlight("bkt") + + assert.Error(t, downloadResult(t, read, time.Second), "the bucket policy denies the read") + assert.NoError(t, downloadResult(t, change, 5*time.Second), "the change untracked itself first") + + r := mux.SetURLVars(httptest.NewRequest(http.MethodPut, "/bkt?policy", nil), map[string]string{"bucket": "bkt"}) + r.Header.Set(s3_constants.AmzAuthType, "SigV4") + r = r.WithContext(s3_constants.SetAccessKeyIdInContext(r.Context(), revokeKeyB)) + stored := true + iam.abortWhenRevoked(func(w http.ResponseWriter, r *http.Request) { + iam.revalidateBucketInFlight("bkt") + select { + case <-r.Context().Done(): + case <-time.After(5 * time.Second): + t.Fatal("the denied request was not aborted") + } + stored = iam.untrackInFlight(r) + }, iam.lookupByIdentityName("bob"), s3_constants.ACTION_PUT_BUCKET_POLICY, iam.inFlightGeneration())(httptest.NewRecorder(), r) + assert.False(t, stored, "an aborted request must not store its change") +} + // A re-check judges time conditions as of when the request was admitted, so a // grant that expires while the request runs does not abort it. func TestAbortRevokedRequestsJudgesTimeAsOfAdmission(t *testing.T) { diff --git a/weed/s3api/s3api_bucket_handlers.go b/weed/s3api/s3api_bucket_handlers.go index a19229279c..73fa2bd8ae 100644 --- a/weed/s3api/s3api_bucket_handlers.go +++ b/weed/s3api/s3api_bucket_handlers.go @@ -504,6 +504,9 @@ func (s3a *S3ApiServer) DeleteBucketHandler(w http.ResponseWriter, r *http.Reque } } + if !s3a.iam.untrackInFlight(r) { + return + } // The filer resolves and drops the bucket's collection inside the delete; // it keeps a shared one rather than risk another bucket's volumes. err := s3a.rm(r.Context(), s3a.option.BucketsPath, bucket, false, true) diff --git a/weed/s3api/s3api_bucket_policy_handlers.go b/weed/s3api/s3api_bucket_policy_handlers.go index 526c938143..fe5fad6f37 100644 --- a/weed/s3api/s3api_bucket_policy_handlers.go +++ b/weed/s3api/s3api_bucket_policy_handlers.go @@ -115,6 +115,9 @@ func (s3a *S3ApiServer) PutBucketPolicyHandler(w http.ResponseWriter, r *http.Re return } + if !s3a.iam.untrackInFlight(r) { + return + } // Store bucket policy if err := s3a.setBucketPolicy(bucket, &policyDoc); err != nil { if errors.Is(err, ErrBucketNotFound) { @@ -178,6 +181,9 @@ func (s3a *S3ApiServer) DeleteBucketPolicyHandler(w http.ResponseWriter, r *http return } + if !s3a.iam.untrackInFlight(r) { + return + } // Delete bucket policy if err := s3a.deleteBucketPolicy(bucket); err != nil { if errors.Is(err, ErrBucketNotFound) {