From c526f7e33f19bb5e595ec17334be106d7866e286 Mon Sep 17 00:00:00 2001 From: Marina Biryukova Date: Tue, 26 Nov 2024 13:32:00 +0300 Subject: [PATCH] [#563] Ignore precondition headers with invalid date format Signed-off-by: Marina Biryukova --- api/handler/get.go | 9 ++++----- api/handler/head_test.go | 10 ++++++++++ api/handler/patch.go | 5 ++--- api/handler/patch_test.go | 7 +++++++ 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/api/handler/get.go b/api/handler/get.go index c524dfc..881edc1 100644 --- a/api/handler/get.go +++ b/api/handler/get.go @@ -291,17 +291,16 @@ func checkPreconditions(info *data.ObjectInfo, args *conditionalArgs, md5Enabled } func parseConditionalHeaders(headers http.Header) (*conditionalArgs, error) { - var err error args := &conditionalArgs{ IfMatch: data.UnQuote(headers.Get(api.IfMatch)), IfNoneMatch: data.UnQuote(headers.Get(api.IfNoneMatch)), } - if args.IfModifiedSince, err = parseHTTPTime(headers.Get(api.IfModifiedSince)); err != nil { - return nil, err + if httpTime, err := parseHTTPTime(headers.Get(api.IfModifiedSince)); err == nil { + args.IfModifiedSince = httpTime } - if args.IfUnmodifiedSince, err = parseHTTPTime(headers.Get(api.IfUnmodifiedSince)); err != nil { - return nil, err + if httpTime, err := parseHTTPTime(headers.Get(api.IfUnmodifiedSince)); err == nil { + args.IfUnmodifiedSince = httpTime } return args, nil diff --git a/api/handler/head_test.go b/api/handler/head_test.go index 4eebc8c..b6aec29 100644 --- a/api/handler/head_test.go +++ b/api/handler/head_test.go @@ -68,6 +68,16 @@ func TestConditionalHead(t *testing.T) { api.IfModifiedSince: zeroTime.UTC().Format(http.TimeFormat), } headObject(t, tc, bktName, objName, headers, http.StatusNotModified) + + headers = map[string]string{ + api.IfUnmodifiedSince: zeroTime.UTC().Format(time.RFC3339), // invalid format, header is ignored + } + headObject(t, tc, bktName, objName, headers, http.StatusOK) + + headers = map[string]string{ + api.IfModifiedSince: objInfo.Created.Add(time.Minute).Format(time.RFC3339), // invalid format, header is ignored + } + headObject(t, tc, bktName, objName, headers, http.StatusOK) } func headObject(t *testing.T, tc *handlerContext, bktName, objName string, headers map[string]string, status int) { diff --git a/api/handler/patch.go b/api/handler/patch.go index a9e79bc..fab7a15 100644 --- a/api/handler/patch.go +++ b/api/handler/patch.go @@ -138,13 +138,12 @@ func (h *handler) PatchObjectHandler(w http.ResponseWriter, r *http.Request) { } func parsePatchConditionalHeaders(headers http.Header) (*conditionalArgs, error) { - var err error args := &conditionalArgs{ IfMatch: data.UnQuote(headers.Get(api.IfMatch)), } - if args.IfUnmodifiedSince, err = parseHTTPTime(headers.Get(api.IfUnmodifiedSince)); err != nil { - return nil, err + if httpTime, err := parseHTTPTime(headers.Get(api.IfUnmodifiedSince)); err == nil { + args.IfUnmodifiedSince = httpTime } return args, nil diff --git a/api/handler/patch_test.go b/api/handler/patch_test.go index aa29423..33aa339 100644 --- a/api/handler/patch_test.go +++ b/api/handler/patch_test.go @@ -60,6 +60,13 @@ func TestPatch(t *testing.T) { api.IfMatch: etag, }, }, + { + name: "If-Unmodified-Since invalid format, header is ignored", + rng: "bytes 0-2/*", + headers: map[string]string{ + api.IfUnmodifiedSince: created.Add(-24 * time.Hour).Format(time.RFC3339), + }, + }, { name: "invalid range syntax", rng: "bytes 0-2",