From 5089a7167d25ac70edf08df6575d96de8e2a4b3e Mon Sep 17 00:00:00 2001 From: kannappanr <30541348+kannappanr@users.noreply.github.com> Date: Tue, 30 Jun 2020 16:44:24 -0700 Subject: [PATCH] Handle empty retention in get/put object retention (#9948) Fixes #9943 --- cmd/object-handlers.go | 14 ++++++++++++-- pkg/bucket/object/lock/lock.go | 11 +++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/cmd/object-handlers.go b/cmd/object-handlers.go index 2a1b83257..5e4b712a0 100644 --- a/cmd/object-handlers.go +++ b/cmd/object-handlers.go @@ -2844,8 +2844,13 @@ func (api objectAPIHandlers) PutObjectRetentionHandler(w http.ResponseWriter, r return } - objInfo.UserDefined[strings.ToLower(xhttp.AmzObjectLockMode)] = string(objRetention.Mode) - objInfo.UserDefined[strings.ToLower(xhttp.AmzObjectLockRetainUntilDate)] = objRetention.RetainUntilDate.UTC().Format(time.RFC3339) + if objRetention.Mode.Valid() { + objInfo.UserDefined[strings.ToLower(xhttp.AmzObjectLockMode)] = string(objRetention.Mode) + objInfo.UserDefined[strings.ToLower(xhttp.AmzObjectLockRetainUntilDate)] = objRetention.RetainUntilDate.UTC().Format(time.RFC3339) + } else { + delete(objInfo.UserDefined, strings.ToLower(xhttp.AmzObjectLockRetainUntilDate)) + delete(objInfo.UserDefined, strings.ToLower(xhttp.AmzObjectLockMode)) + } if objInfo.UserTags != "" { objInfo.UserDefined[xhttp.AmzObjectTagging] = objInfo.UserTags } @@ -2914,6 +2919,11 @@ func (api objectAPIHandlers) GetObjectRetentionHandler(w http.ResponseWriter, r retention := objectlock.GetObjectRetentionMeta(objInfo.UserDefined) + if !retention.Mode.Valid() { + writeErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrNoSuchObjectLockConfiguration), r.URL, guessIsBrowserReq(r)) + return + } + writeSuccessResponseXML(w, encodeResponse(retention)) // Notify object retention accessed via a GET request. sendEvent(eventArgs{ diff --git a/pkg/bucket/object/lock/lock.go b/pkg/bucket/object/lock/lock.go index 3acc85a80..44656ffb6 100644 --- a/pkg/bucket/object/lock/lock.go +++ b/pkg/bucket/object/lock/lock.go @@ -341,6 +341,14 @@ func ParseObjectRetention(reader io.Reader) (*ObjectRetention, error) { return &ret, ErrUnknownWORMModeDirective } + if ret.Mode.Valid() && ret.RetainUntilDate.IsZero() { + return &ret, ErrMalformedXML + } + + if !ret.Mode.Valid() && !ret.RetainUntilDate.IsZero() { + return &ret, ErrMalformedXML + } + t, err := UTCNowNTP() if err != nil { logger.LogIf(context.Background(), err) @@ -431,7 +439,10 @@ func GetObjectRetentionMeta(meta map[string]string) ObjectRetention { } if ok { mode = parseRetMode(modeStr) + } else { + return ObjectRetention{} } + tillStr, ok = meta[strings.ToLower(AmzObjectLockRetainUntilDate)] if !ok { tillStr, ok = meta[AmzObjectLockRetainUntilDate]