Support GetBucketCORS for CORS without xmlns attribute #594

Closed
opened 2024-12-23 09:51:01 +00:00 by pogpp · 2 comments
Member

Expected Behavior

support GetBucketCORS operation for such files

<CORSConfiguration>
    <CORSRule>
     ...
    </CORSRule>
</CORSConfiguration> 

Current Behavior

"description":"could not get cors","user":"","error":"unmarshal cors: expected element in name space http://s3.amazonaws.com/doc/2006-03-01/ but have no name space"

Possible Solution

Modify NewXmlDecoder()

Steps to Reproduce (for bugs)

--git a/api/handler/cors_test.go b/api/handler/cors_test.go
index 42008d76..d2383069 100644
--- a/api/handler/cors_test.go
+++ b/api/handler/cors_test.go
@@ -19,7 +19,15 @@ func TestCORSOriginWildcard(t *testing.T) {
     </CORSRule>
 </CORSConfiguration>
 `
-    hc := prepareHandlerContext(t)
+
+    body2 := `
+<CORSConfiguration>
+    <CORSRule>
+        <AllowedMethod>GET</AllowedMethod>
+        <AllowedOrigin>*</AllowedOrigin>
+    </CORSRule>
+</CORSConfiguration>`
+    hc := prepareHandlerContextWithMinCache(t)
 
     bktName := "bucket-for-cors"
     box, _ := createAccessBox(t)
@@ -39,6 +47,17 @@ func TestCORSOriginWildcard(t *testing.T) {
     w, r = prepareTestPayloadRequest(hc, bktName, "", nil)
     hc.Handler().GetBucketCorsHandler(w, r)
     assertStatus(t, w, http.StatusOK)
+
+    hc.config.useDefaultXMLNS = true
+    w, r = prepareTestPayloadRequest(hc, bktName, "", strings.NewReader(body2))
+    ctx = middleware.SetBox(r.Context(), &middleware.Box{AccessBox: box})
+    r = r.WithContext(ctx)
+    hc.Handler().PutBucketCorsHandler(w, r)
+    assertStatus(t, w, http.StatusOK)
+
+    w, r = prepareTestPayloadRequest(hc, bktName, "", nil)
+    hc.Handler().GetBucketCorsHandler(w, r)
+    assertStatus(t, w, http.StatusOK)
 }
 
 func TestPreflight(t *testing.T) {
diff --git a/api/handler/handlers_test.go b/api/handler/handlers_test.go
index 1cd5eda1..b3ed653d 100644
--- a/api/handler/handlers_test.go
+++ b/api/handler/handlers_test.go
@@ -81,6 +81,7 @@ type configMock struct {
     bypassContentEncodingInChunks bool
     md5Enabled                    bool
     tlsTerminationHeader          string
+    useDefaultXMLNS               bool
 }
 
 func (c *configMock) DefaultPlacementPolicy(_ string) netmap.PlacementPolicy {
@@ -102,7 +103,11 @@ func (c *configMock) DefaultCopiesNumbers(_ string) []uint32 {
 }
 
 func (c *configMock) NewXMLDecoder(r io.Reader, _ string) *xml.Decoder {
-    return xml.NewDecoder(r)
+    dec := xml.NewDecoder(r)
+    if c.useDefaultXMLNS {
+        dec.DefaultSpace = "http://s3.amazonaws.com/doc/2006-03-01/"
+    }
+    return dec
 }
 
 func (c *configMock) BypassContentEncodingInChunks(_ string) bool {

<!--- Provide a general summary of the issue in the Title above --> ## Expected Behavior support GetBucketCORS operation for such files ``` <CORSConfiguration> <CORSRule> ... </CORSRule> </CORSConfiguration> ``` ## Current Behavior "description":"could not get cors","user":"","error":"unmarshal cors: expected element <CORSConfiguration> in name space http://s3.amazonaws.com/doc/2006-03-01/ but have no name space" ## Possible Solution Modify `NewXmlDecoder()` ## Steps to Reproduce (for bugs) ```diff --git a/api/handler/cors_test.go b/api/handler/cors_test.go index 42008d76..d2383069 100644 --- a/api/handler/cors_test.go +++ b/api/handler/cors_test.go @@ -19,7 +19,15 @@ func TestCORSOriginWildcard(t *testing.T) { </CORSRule> </CORSConfiguration> ` - hc := prepareHandlerContext(t) + + body2 := ` +<CORSConfiguration> + <CORSRule> + <AllowedMethod>GET</AllowedMethod> + <AllowedOrigin>*</AllowedOrigin> + </CORSRule> +</CORSConfiguration>` + hc := prepareHandlerContextWithMinCache(t) bktName := "bucket-for-cors" box, _ := createAccessBox(t) @@ -39,6 +47,17 @@ func TestCORSOriginWildcard(t *testing.T) { w, r = prepareTestPayloadRequest(hc, bktName, "", nil) hc.Handler().GetBucketCorsHandler(w, r) assertStatus(t, w, http.StatusOK) + + hc.config.useDefaultXMLNS = true + w, r = prepareTestPayloadRequest(hc, bktName, "", strings.NewReader(body2)) + ctx = middleware.SetBox(r.Context(), &middleware.Box{AccessBox: box}) + r = r.WithContext(ctx) + hc.Handler().PutBucketCorsHandler(w, r) + assertStatus(t, w, http.StatusOK) + + w, r = prepareTestPayloadRequest(hc, bktName, "", nil) + hc.Handler().GetBucketCorsHandler(w, r) + assertStatus(t, w, http.StatusOK) } func TestPreflight(t *testing.T) { diff --git a/api/handler/handlers_test.go b/api/handler/handlers_test.go index 1cd5eda1..b3ed653d 100644 --- a/api/handler/handlers_test.go +++ b/api/handler/handlers_test.go @@ -81,6 +81,7 @@ type configMock struct { bypassContentEncodingInChunks bool md5Enabled bool tlsTerminationHeader string + useDefaultXMLNS bool } func (c *configMock) DefaultPlacementPolicy(_ string) netmap.PlacementPolicy { @@ -102,7 +103,11 @@ func (c *configMock) DefaultCopiesNumbers(_ string) []uint32 { } func (c *configMock) NewXMLDecoder(r io.Reader, _ string) *xml.Decoder { - return xml.NewDecoder(r) + dec := xml.NewDecoder(r) + if c.useDefaultXMLNS { + dec.DefaultSpace = "http://s3.amazonaws.com/doc/2006-03-01/" + } + return dec } func (c *configMock) BypassContentEncodingInChunks(_ string) bool { ```
pogpp added the
bug
label 2024-12-23 09:51:01 +00:00
Member

Possible Solution
Modify NewXmlDecoder()

Actually, not. Solution is using this method here

if err = xml.NewDecoder(obj.Payload).Decode(&cors); err != nil {

> Possible Solution > Modify NewXmlDecoder() Actually, not. Solution is using this method here https://git.frostfs.info/TrueCloudLab/frostfs-s3-gw/src/commit/16eb289929f126a8cdc6f3510f121cefa23803eb/api/layer/system_object.go#L192
pogpp was assigned by alexvanin 2024-12-23 10:05:31 +00:00
Owner

Done in #600

Done in #600
alexvanin added this to the v0.32.1 milestone 2025-01-16 14:02:24 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: TrueCloudLab/frostfs-s3-gw#594
No description provided.