[#563] Ignore precondition headers with invalid date format

Signed-off-by: Marina Biryukova <m.biryukova@yadro.com>
This commit is contained in:
Marina Biryukova 2024-11-26 13:32:00 +03:00
parent a12fea8a5b
commit e3141fc8e3
7 changed files with 43 additions and 31 deletions

View file

@ -73,7 +73,7 @@ func (h *handler) GetObjectAttributesHandler(w http.ResponseWriter, r *http.Requ
ctx := r.Context() ctx := r.Context()
reqInfo := middleware.GetReqInfo(ctx) reqInfo := middleware.GetReqInfo(ctx)
params, err := parseGetObjectAttributeArgs(r) params, err := parseGetObjectAttributeArgs(r, h.reqLogger(ctx))
if err != nil { if err != nil {
h.logAndSendError(ctx, w, "invalid request", reqInfo, err) h.logAndSendError(ctx, w, "invalid request", reqInfo, err)
return return
@ -145,7 +145,7 @@ func writeAttributesHeaders(h http.Header, info *data.ExtendedObjectInfo, isBuck
// x-amz-request-charged // x-amz-request-charged
} }
func parseGetObjectAttributeArgs(r *http.Request) (*GetObjectAttributesArgs, error) { func parseGetObjectAttributeArgs(r *http.Request, log *zap.Logger) (*GetObjectAttributesArgs, error) {
res := &GetObjectAttributesArgs{ res := &GetObjectAttributesArgs{
VersionID: r.URL.Query().Get(api.QueryVersionID), VersionID: r.URL.Query().Get(api.QueryVersionID),
} }
@ -178,8 +178,8 @@ func parseGetObjectAttributeArgs(r *http.Request) (*GetObjectAttributesArgs, err
} }
} }
res.Conditional, err = parseConditionalHeaders(r.Header) res.Conditional = parseConditionalHeaders(r.Header, log)
return res, err return res, nil
} }
func encodeToObjectAttributesResponse(info *data.ObjectInfo, p *GetObjectAttributesArgs, md5Enabled bool) (*GetObjectAttributesResponse, error) { func encodeToObjectAttributesResponse(info *data.ObjectInfo, p *GetObjectAttributesArgs, md5Enabled bool) (*GetObjectAttributesResponse, error) {

View file

@ -13,6 +13,7 @@ import (
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors" "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer" "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware" "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware"
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -154,11 +155,7 @@ func (h *handler) GetObjectHandler(w http.ResponseWriter, r *http.Request) {
reqInfo = middleware.GetReqInfo(ctx) reqInfo = middleware.GetReqInfo(ctx)
) )
conditional, err := parseConditionalHeaders(r.Header) conditional := parseConditionalHeaders(r.Header, h.reqLogger(ctx))
if err != nil {
h.logAndSendError(ctx, w, "could not parse request params", reqInfo, err)
return
}
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName) bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
if err != nil { if err != nil {
@ -290,21 +287,24 @@ func checkPreconditions(info *data.ObjectInfo, args *conditionalArgs, md5Enabled
return nil return nil
} }
func parseConditionalHeaders(headers http.Header) (*conditionalArgs, error) { func parseConditionalHeaders(headers http.Header, log *zap.Logger) *conditionalArgs {
var err error
args := &conditionalArgs{ args := &conditionalArgs{
IfMatch: data.UnQuote(headers.Get(api.IfMatch)), IfMatch: data.UnQuote(headers.Get(api.IfMatch)),
IfNoneMatch: data.UnQuote(headers.Get(api.IfNoneMatch)), IfNoneMatch: data.UnQuote(headers.Get(api.IfNoneMatch)),
} }
if args.IfModifiedSince, err = parseHTTPTime(headers.Get(api.IfModifiedSince)); err != nil { if httpTime, err := parseHTTPTime(headers.Get(api.IfModifiedSince)); err == nil {
return nil, err args.IfModifiedSince = httpTime
} else {
log.Warn(logs.FailedToParseHTTPTime, zap.String(api.IfModifiedSince, headers.Get(api.IfModifiedSince)), zap.Error(err))
} }
if args.IfUnmodifiedSince, err = parseHTTPTime(headers.Get(api.IfUnmodifiedSince)); err != nil { if httpTime, err := parseHTTPTime(headers.Get(api.IfUnmodifiedSince)); err == nil {
return nil, err args.IfUnmodifiedSince = httpTime
} else {
log.Warn(logs.FailedToParseHTTPTime, zap.String(api.IfUnmodifiedSince, headers.Get(api.IfUnmodifiedSince)), zap.Error(err))
} }
return args, nil return args
} }
func parseHTTPTime(data string) (*time.Time, error) { func parseHTTPTime(data string) (*time.Time, error) {

View file

@ -36,11 +36,7 @@ func (h *handler) HeadObjectHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
conditional, err := parseConditionalHeaders(r.Header) conditional := parseConditionalHeaders(r.Header, h.reqLogger(ctx))
if err != nil {
h.logAndSendError(ctx, w, "could not parse request params", reqInfo, err)
return
}
p := &layer.HeadObjectParams{ p := &layer.HeadObjectParams{
BktInfo: bktInfo, BktInfo: bktInfo,

View file

@ -68,6 +68,16 @@ func TestConditionalHead(t *testing.T) {
api.IfModifiedSince: zeroTime.UTC().Format(http.TimeFormat), api.IfModifiedSince: zeroTime.UTC().Format(http.TimeFormat),
} }
headObject(t, tc, bktName, objName, headers, http.StatusNotModified) 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) { func headObject(t *testing.T, tc *handlerContext, bktName, objName string, headers map[string]string, status int) {

View file

@ -12,6 +12,7 @@ import (
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors" "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer" "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware" "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware"
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -31,11 +32,7 @@ func (h *handler) PatchObjectHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
conditional, err := parsePatchConditionalHeaders(r.Header) conditional := parsePatchConditionalHeaders(r.Header, h.reqLogger(ctx))
if err != nil {
h.logAndSendError(ctx, w, "could not parse conditional headers", reqInfo, err)
return
}
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName) bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
if err != nil { if err != nil {
@ -137,17 +134,18 @@ func (h *handler) PatchObjectHandler(w http.ResponseWriter, r *http.Request) {
} }
} }
func parsePatchConditionalHeaders(headers http.Header) (*conditionalArgs, error) { func parsePatchConditionalHeaders(headers http.Header, log *zap.Logger) *conditionalArgs {
var err error
args := &conditionalArgs{ args := &conditionalArgs{
IfMatch: data.UnQuote(headers.Get(api.IfMatch)), IfMatch: data.UnQuote(headers.Get(api.IfMatch)),
} }
if args.IfUnmodifiedSince, err = parseHTTPTime(headers.Get(api.IfUnmodifiedSince)); err != nil { if httpTime, err := parseHTTPTime(headers.Get(api.IfUnmodifiedSince)); err == nil {
return nil, err args.IfUnmodifiedSince = httpTime
} else {
log.Warn(logs.FailedToParseHTTPTime, zap.String(api.IfUnmodifiedSince, headers.Get(api.IfUnmodifiedSince)), zap.Error(err))
} }
return args, nil return args
} }
func parsePatchByteRange(rangeStr string, objSize uint64) (*layer.RangeParams, error) { func parsePatchByteRange(rangeStr string, objSize uint64) (*layer.RangeParams, error) {

View file

@ -60,6 +60,13 @@ func TestPatch(t *testing.T) {
api.IfMatch: etag, 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", name: "invalid range syntax",
rng: "bytes 0-2", rng: "bytes 0-2",

View file

@ -177,4 +177,5 @@ const (
MultinetConfigWontBeUpdated = "multinet config won't be updated" MultinetConfigWontBeUpdated = "multinet config won't be updated"
MultinetDialSuccess = "multinet dial successful" MultinetDialSuccess = "multinet dial successful"
MultinetDialFail = "multinet dial failed" MultinetDialFail = "multinet dial failed"
FailedToParseHTTPTime = "failed to parse http time, header is ignored"
) )