mirror of
https://github.com/pgsty/minio.git
synced 2026-11-01 05:12:07 +03:00
fix: preserve global CORS response semantics
Guard only the per-bucket metadata lookup, then retain the global handler Vary and originless preflight behavior for non-CORS traffic. Signed-off-by: Feng Ruohang <rh@vonng.com>
This commit is contained in:
+13
-15
@@ -785,23 +785,21 @@ func corsHandler(handler http.Handler) http.Handler {
|
|||||||
}
|
}
|
||||||
globalCors := cors.New(opts).Handler(handler)
|
globalCors := cors.New(opts).Handler(handler)
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Header.Get("Origin") == "" {
|
if r.Header.Get("Origin") != "" {
|
||||||
handler.ServeHTTP(w, r)
|
if bucket, _ := request2BucketObjectName(r); bucket != "" && globalBucketMetadataSys != nil {
|
||||||
return
|
cfg, _, err := globalBucketMetadataSys.GetCorsConfig(bucket)
|
||||||
}
|
if err == nil && cfg != nil {
|
||||||
if bucket, _ := request2BucketObjectName(r); bucket != "" && globalBucketMetadataSys != nil {
|
if applyBucketCors(w, r, cfg) {
|
||||||
cfg, _, err := globalBucketMetadataSys.GetCorsConfig(bucket)
|
return
|
||||||
if err == nil && cfg != nil {
|
}
|
||||||
if applyBucketCors(w, r, cfg) {
|
handler.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil && !errors.Is(err, errConfigNotFound) {
|
||||||
|
internalLogOnceIf(r.Context(), err, "bucket-cors-metadata")
|
||||||
|
handler.ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
handler.ServeHTTP(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err != nil && !errors.Is(err, errConfigNotFound) && r.Header.Get("Origin") != "" {
|
|
||||||
internalLogOnceIf(r.Context(), err, "bucket-cors-metadata")
|
|
||||||
handler.ServeHTTP(w, r)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
globalCors.ServeHTTP(w, r)
|
globalCors.ServeHTTP(w, r)
|
||||||
|
|||||||
@@ -313,11 +313,32 @@ func testBucketCorsSkipsMetadataLookupWithoutOrigin(obj ObjectLayer, _ string, _
|
|||||||
if rec.Code != http.StatusNoContent {
|
if rec.Code != http.StatusNoContent {
|
||||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusNoContent)
|
t.Fatalf("status = %d, want %d", rec.Code, http.StatusNoContent)
|
||||||
}
|
}
|
||||||
|
requireCorsOriginVary(t, rec.Header())
|
||||||
if got := counting.getObjectNInfoCalls.Load(); got != 0 {
|
if got := counting.getObjectNInfoCalls.Load(); got != 0 {
|
||||||
t.Fatalf("request without Origin performed %d bucket metadata reads", got)
|
t.Fatalf("request without Origin performed %d bucket metadata reads", got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBucketCorsOriginlessPreflightShapeUsesGlobalHandler(t *testing.T) {
|
||||||
|
nextCalled := false
|
||||||
|
wrapped := corsHandler(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
nextCalled = true
|
||||||
|
w.WriteHeader(http.StatusTeapot)
|
||||||
|
}))
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
req := httptest.NewRequest(http.MethodOptions, getGetObjectURL("", "api", "v1/login"), nil)
|
||||||
|
req.Header.Set("Access-Control-Request-Method", http.MethodGet)
|
||||||
|
wrapped.ServeHTTP(rec, req)
|
||||||
|
|
||||||
|
if rec.Code != http.StatusNoContent {
|
||||||
|
t.Fatalf("status = %d, want %d", rec.Code, http.StatusNoContent)
|
||||||
|
}
|
||||||
|
if nextCalled {
|
||||||
|
t.Fatal("originless preflight-shaped OPTIONS reached the application handler")
|
||||||
|
}
|
||||||
|
requireCorsOriginVary(t, rec.Header())
|
||||||
|
}
|
||||||
|
|
||||||
func TestBucketCorsNoConfigUsesGlobalFallback(t *testing.T) {
|
func TestBucketCorsNoConfigUsesGlobalFallback(t *testing.T) {
|
||||||
ExecObjectLayerAPITest(ExecObjectLayerAPITestArgs{
|
ExecObjectLayerAPITest(ExecObjectLayerAPITestArgs{
|
||||||
t: t,
|
t: t,
|
||||||
|
|||||||
Reference in New Issue
Block a user