From 94de65b3b5d79b85e1112321199e81ccc1273cc1 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 18 Nov 2026 11:03:57 -0800 Subject: [PATCH] s3api: answer a malformed Cache-Control on PutObject with InvalidRequest (#12733) PutObject rejected a Cache-Control value it could not parse with InvalidDigest, whose message is about Content-MD5, even when the request sent no Content-MD5. CreateMultipartUpload already answers the same parse error with InvalidRequest; PutObject now does too. Co-authored-by: Claude Opus 5.5 --- weed/s3api/s3api_object_handlers_put.go | 2 +- ..._object_handlers_put_cache_control_test.go | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 weed/s3api/s3api_object_handlers_put_cache_control_test.go diff --git a/weed/s3api/s3api_object_handlers_put.go b/weed/s3api/s3api_object_handlers_put.go index cea0ef884b..f15bdc8dc1 100644 --- a/weed/s3api/s3api_object_handlers_put.go +++ b/weed/s3api/s3api_object_handlers_put.go @@ -147,7 +147,7 @@ func (s3a *S3ApiServer) PutObjectHandler(w http.ResponseWriter, r *http.Request) if r.Header.Get("Cache-Control") != "" { if _, err = cacheobject.ParseRequestCacheControl(r.Header.Get("Cache-Control")); err != nil { - s3err.WriteErrorResponse(w, r, s3err.ErrInvalidDigest) + s3err.WriteErrorResponse(w, r, s3err.ErrInvalidRequest) return } } diff --git a/weed/s3api/s3api_object_handlers_put_cache_control_test.go b/weed/s3api/s3api_object_handlers_put_cache_control_test.go new file mode 100644 index 0000000000..24c8be3c98 --- /dev/null +++ b/weed/s3api/s3api_object_handlers_put_cache_control_test.go @@ -0,0 +1,39 @@ +package s3api + +import ( + "net/http" + "testing" + + "github.com/seaweedfs/seaweedfs/weed/s3api/s3err" + "github.com/stretchr/testify/require" +) + +func TestPutObjectMalformedCacheControlIsInvalidRequest(t *testing.T) { + for _, value := range []string{"max-age=", "max-age=A", "public, max-age=abc"} { + t.Run(value, func(t *testing.T) { + s3a, filer, volume := newHeaderTestServer(t, "") + req, probe := newHeaderTestRequest("/b/o", "o", "hello world", "Cache-Control", value) + + s3a.PutObjectHandler(probe, req) + + requireRejected(t, probe, volume, s3err.ErrInvalidRequest) + filer.mu.Lock() + defer filer.mu.Unlock() + require.Empty(t, filer.entries) + }) + } +} + +func TestPutObjectStoresValidCacheControl(t *testing.T) { + s3a, filer, _ := newHeaderTestServer(t, "") + req, probe := newHeaderTestRequest("/b/o", "o", "hello world", "Cache-Control", "public, max-age=60") + + s3a.PutObjectHandler(probe, req) + + require.Equal(t, http.StatusOK, probe.Code, probe.Body.String()) + filer.mu.Lock() + defer filer.mu.Unlock() + entry := filer.entries["/buckets/b/o"] + require.NotNil(t, entry) + require.Equal(t, "public, max-age=60", string(entry.Extended["Cache-Control"])) +}