From 0ce31daf90dc32ec38adf15fe456e6e50f356232 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 8 Oct 2025 14:24:10 -0700 Subject: Fix #7305: Return 400 BadDigest instead of 500 InternalError for MD5 mismatch (#7306) 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 --- weed/s3api/s3err/s3api_errors.go | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'weed/s3api/s3err/s3api_errors.go') diff --git a/weed/s3api/s3err/s3api_errors.go b/weed/s3api/s3err/s3api_errors.go index 3da79e817..24f8e1b56 100644 --- a/weed/s3api/s3err/s3api_errors.go +++ b/weed/s3api/s3err/s3api_errors.go @@ -4,6 +4,8 @@ import ( "encoding/xml" "fmt" "net/http" + + "github.com/seaweedfs/seaweedfs/weed/util/constants" ) // APIError structure @@ -59,6 +61,7 @@ const ( ErrInvalidBucketName ErrInvalidBucketState ErrInvalidDigest + ErrBadDigest ErrInvalidMaxKeys ErrInvalidMaxUploads ErrInvalidMaxParts @@ -187,6 +190,11 @@ var errorCodeResponse = map[ErrorCode]APIError{ Description: "The Content-Md5 you specified is not valid.", HTTPStatusCode: http.StatusBadRequest, }, + ErrBadDigest: { + Code: "BadDigest", + Description: constants.ErrMsgBadDigest, + HTTPStatusCode: http.StatusBadRequest, + }, ErrInvalidMaxUploads: { Code: "InvalidArgument", Description: "Argument max-uploads must be an integer between 0 and 2147483647", -- cgit v1.2.3