From d82a1da511b414bb5bf4a9397ffd55b91e3364d6 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Fri, 20 Oct 2017 13:57:57 -0700 Subject: [PATCH] Fix notification unmarshalling, unmarshal only when size is > 0 (#5087) Fixes #5085 --- cmd/bucket-notification-handlers.go | 4 ++-- cmd/event-notifier.go | 28 +++++++++++++++++++--------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/cmd/bucket-notification-handlers.go b/cmd/bucket-notification-handlers.go index 922bf814c..fb8548327 100644 --- a/cmd/bucket-notification-handlers.go +++ b/cmd/bucket-notification-handlers.go @@ -63,13 +63,13 @@ func (api objectAPIHandlers) GetBucketNotificationHandler(w http.ResponseWriter, // Attempt to successfully load notification config. nConfig, err := loadNotificationConfig(bucket, objAPI) - if err != nil && err != errNoSuchNotifications { + if err != nil && errorCause(err) != errNoSuchNotifications { errorIf(err, "Unable to read notification configuration.") writeErrorResponse(w, toAPIErrorCode(err), r.URL) return } // For no notifications we write a dummy XML. - if err == errNoSuchNotifications { + if errorCause(err) == errNoSuchNotifications { // Complies with the s3 behavior in this regard. nConfig = ¬ificationConfig{} } diff --git a/cmd/event-notifier.go b/cmd/event-notifier.go index 72d306ef5..9f09d032f 100644 --- a/cmd/event-notifier.go +++ b/cmd/event-notifier.go @@ -377,18 +377,24 @@ func loadNotificationConfig(bucket string, objAPI ObjectLayer) (*notificationCon // 'errNoSuchNotifications'. This is default when no // bucket notifications are found on the bucket. if isErrObjectNotFound(err) || isErrIncompleteBody(err) { - return nil, errNoSuchNotifications + return nil, traceError(errNoSuchNotifications) } errorIf(err, "Unable to load bucket-notification for bucket %s", bucket) // Returns error for other errors. return nil, err } + // if `notifications.xml` is empty we should return NoSuchNotifications. + if buffer.Len() == 0 { + return nil, traceError(errNoSuchNotifications) + } + // Unmarshal notification bytes. notificationConfigBytes := buffer.Bytes() notificationCfg := ¬ificationConfig{} - if err = xml.Unmarshal(notificationConfigBytes, ¬ificationCfg); err != nil { - return nil, err + // Unmarshal notification bytes only if we read data. + if err = xml.Unmarshal(notificationConfigBytes, notificationCfg); err != nil { + return nil, traceError(err) } // Return success. @@ -418,23 +424,27 @@ func loadListenerConfig(bucket string, objAPI ObjectLayer) ([]listenerConfig, er var buffer bytes.Buffer err := objAPI.GetObject(minioMetaBucket, lcPath, 0, -1, &buffer) if err != nil { - // 'notification.xml' not found return + // 'listener.json' not found return // 'errNoSuchNotifications'. This is default when no - // bucket listeners are found on the bucket. - if isErrObjectNotFound(err) { - return nil, errNoSuchNotifications + // bucket listeners are found on the bucket + if isErrObjectNotFound(err) || isErrIncompleteBody(err) { + return nil, traceError(errNoSuchNotifications) } errorIf(err, "Unable to load bucket-listeners for bucket %s", bucket) // Returns error for other errors. return nil, err } - // Unmarshal notification bytes. + // if `listener.json` is empty we should return NoSuchNotifications. + if buffer.Len() == 0 { + return nil, traceError(errNoSuchNotifications) + } + var lCfg []listenerConfig lConfigBytes := buffer.Bytes() if err = json.Unmarshal(lConfigBytes, &lCfg); err != nil { errorIf(err, "Unable to unmarshal listener config from JSON.") - return nil, err + return nil, traceError(err) } // Return success.