When an S3 upload has a mismatched Content-MD5 header, SeaweedFS was incorrectly returning a 500 Internal Server Error instead of the proper 400 Bad Request with error code BadDigest (per AWS S3 specification). Changes: - Created weed/util/constants/filer.go with error message constants - Added ErrMsgBadDigest constant for MD5 mismatch errors - Added ErrMsgOperationNotPermitted constant for WORM permission errors - Added ErrBadDigest error code with proper 400 status code mapping - Updated filerErrorToS3Error() to detect MD5 mismatch and return ErrBadDigest - Updated filer autoChunk() to return 400 Bad Request for MD5 mismatch - Refactored error handling to use switch statement for better readability - Ordered error checks with exact matches first for better maintainability - Updated all error handling to use centralized constants - Added comprehensive unit tests All error messages now use constants from a single location for better maintainability and consistency. Constants placed in util package to avoid architectural dependency issues. Fixes #7305
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/seaweedfs/seaweedfs/weed/operation"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/security"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util/constants"
|
||||
"github.com/seaweedfs/seaweedfs/weed/stats"
|
||||
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||
@@ -168,7 +169,7 @@ func (fs *FilerServer) move(ctx context.Context, w http.ResponseWriter, r *http.
|
||||
return
|
||||
} else if wormEnforced {
|
||||
// you cannot move a worm file or directory
|
||||
err = fmt.Errorf("cannot move write-once entry from '%s' to '%s': operation not permitted", src, dst)
|
||||
err = fmt.Errorf("cannot move write-once entry from '%s' to '%s': %s", src, dst, constants.ErrMsgOperationNotPermitted)
|
||||
writeJsonError(w, r, http.StatusForbidden, err)
|
||||
return
|
||||
}
|
||||
@@ -228,7 +229,7 @@ func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
writeJsonError(w, r, http.StatusInternalServerError, err)
|
||||
return
|
||||
} else if wormEnforced {
|
||||
writeJsonError(w, r, http.StatusForbidden, errors.New("operation not permitted"))
|
||||
writeJsonError(w, r, http.StatusForbidden, errors.New(constants.ErrMsgOperationNotPermitted))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"github.com/seaweedfs/seaweedfs/weed/operation"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util/constants"
|
||||
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
||||
"github.com/seaweedfs/seaweedfs/weed/util"
|
||||
)
|
||||
@@ -50,13 +51,17 @@ func (fs *FilerServer) autoChunk(ctx context.Context, w http.ResponseWriter, r *
|
||||
reply, md5bytes, err = fs.doPutAutoChunk(ctx, w, r, chunkSize, contentLength, so)
|
||||
}
|
||||
if err != nil {
|
||||
if err.Error() == "operation not permitted" {
|
||||
errStr := err.Error()
|
||||
switch {
|
||||
case errStr == constants.ErrMsgOperationNotPermitted:
|
||||
writeJsonError(w, r, http.StatusForbidden, err)
|
||||
} else if strings.HasPrefix(err.Error(), "read input:") || err.Error() == io.ErrUnexpectedEOF.Error() {
|
||||
case strings.HasPrefix(errStr, "read input:") || errStr == io.ErrUnexpectedEOF.Error():
|
||||
writeJsonError(w, r, util.HttpStatusCancelled, err)
|
||||
} else if strings.HasSuffix(err.Error(), "is a file") || strings.HasSuffix(err.Error(), "already exists") {
|
||||
case strings.HasSuffix(errStr, "is a file") || strings.HasSuffix(errStr, "already exists"):
|
||||
writeJsonError(w, r, http.StatusConflict, err)
|
||||
} else {
|
||||
case errStr == constants.ErrMsgBadDigest:
|
||||
writeJsonError(w, r, http.StatusBadRequest, err)
|
||||
default:
|
||||
writeJsonError(w, r, http.StatusInternalServerError, err)
|
||||
}
|
||||
} else if reply != nil {
|
||||
@@ -110,7 +115,7 @@ func (fs *FilerServer) doPostAutoChunk(ctx context.Context, w http.ResponseWrite
|
||||
headerMd5 := r.Header.Get("Content-Md5")
|
||||
if headerMd5 != "" && !(util.Base64Encode(md5bytes) == headerMd5 || fmt.Sprintf("%x", md5bytes) == headerMd5) {
|
||||
fs.filer.DeleteUncommittedChunks(ctx, fileChunks)
|
||||
return nil, nil, errors.New("The Content-Md5 you specified did not match what we received.")
|
||||
return nil, nil, errors.New(constants.ErrMsgBadDigest)
|
||||
}
|
||||
filerResult, replyerr = fs.saveMetaData(ctx, r, fileName, contentType, so, md5bytes, fileChunks, chunkOffset, smallContent)
|
||||
if replyerr != nil {
|
||||
@@ -142,7 +147,7 @@ func (fs *FilerServer) doPutAutoChunk(ctx context.Context, w http.ResponseWriter
|
||||
headerMd5 := r.Header.Get("Content-Md5")
|
||||
if headerMd5 != "" && !(util.Base64Encode(md5bytes) == headerMd5 || fmt.Sprintf("%x", md5bytes) == headerMd5) {
|
||||
fs.filer.DeleteUncommittedChunks(ctx, fileChunks)
|
||||
return nil, nil, errors.New("The Content-Md5 you specified did not match what we received.")
|
||||
return nil, nil, errors.New(constants.ErrMsgBadDigest)
|
||||
}
|
||||
filerResult, replyerr = fs.saveMetaData(ctx, r, fileName, contentType, so, md5bytes, fileChunks, chunkOffset, smallContent)
|
||||
if replyerr != nil {
|
||||
@@ -171,7 +176,7 @@ func (fs *FilerServer) checkPermissions(ctx context.Context, r *http.Request, fi
|
||||
return err
|
||||
} else if enforced {
|
||||
// you cannot change a worm file
|
||||
return errors.New("operation not permitted")
|
||||
return errors.New(constants.ErrMsgOperationNotPermitted)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user