From 26783fbcb37549ab7e46a1240e89e41af02b382a Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Sat, 31 Oct 2026 17:41:42 +0530 Subject: [PATCH] s3: keep an unset CORS MaxAgeSeconds unset (#12272) A rule without MaxAgeSeconds was stored as 0, so GetBucketCors returned MaxAgeSeconds 0 and preflights sent Access-Control-Max-Age: 0. Store -1 for unset, which corsRuleFromProto already reads back as nil. --- weed/s3api/s3api_bucket_config.go | 4 ++-- weed/s3api/s3api_bucket_cors_proto_test.go | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 weed/s3api/s3api_bucket_cors_proto_test.go diff --git a/weed/s3api/s3api_bucket_config.go b/weed/s3api/s3api_bucket_config.go index 6040a589bf..254b2adb83 100644 --- a/weed/s3api/s3api_bucket_config.go +++ b/weed/s3api/s3api_bucket_config.go @@ -860,10 +860,10 @@ func corsConfigFromProto(protoConfig *s3_pb.CORSConfiguration) *cors.CORSConfigu } } -// getMaxAgeSecondsValue safely extracts max age seconds value +// getMaxAgeSecondsValue safely extracts max age seconds value; -1 stores an unset MaxAgeSeconds func getMaxAgeSecondsValue(maxAge *int) int { if maxAge == nil { - return 0 + return -1 } return *maxAge } diff --git a/weed/s3api/s3api_bucket_cors_proto_test.go b/weed/s3api/s3api_bucket_cors_proto_test.go new file mode 100644 index 0000000000..30742049bf --- /dev/null +++ b/weed/s3api/s3api_bucket_cors_proto_test.go @@ -0,0 +1,22 @@ +package s3api + +import ( + "testing" + + "github.com/seaweedfs/seaweedfs/weed/s3api/cors" +) + +func TestCorsRuleProtoRoundTripMaxAge(t *testing.T) { + unset := corsRuleFromProto(corsRuleToProto(cors.CORSRule{AllowedMethods: []string{"GET"}, AllowedOrigins: []string{"*"}})) + if unset.MaxAgeSeconds != nil { + t.Errorf("unset MaxAgeSeconds came back as %d", *unset.MaxAgeSeconds) + } + + for _, age := range []int{0, 3000} { + age := age + got := corsRuleFromProto(corsRuleToProto(cors.CORSRule{AllowedMethods: []string{"GET"}, AllowedOrigins: []string{"*"}, MaxAgeSeconds: &age})) + if got.MaxAgeSeconds == nil || *got.MaxAgeSeconds != age { + t.Errorf("MaxAgeSeconds %d came back as %v", age, got.MaxAgeSeconds) + } + } +}