From 27dc84b5fd0792a88171c5ccdf5dff324f03fccc Mon Sep 17 00:00:00 2001 From: niksis02 Date: Fri, 17 Oct 2025 19:03:10 +0400 Subject: [PATCH] fix: implements proper error handling for malformed http requests Fixes #1364 When a completely malformed request is sent to the gateway, Fiber/Fasthttp fails to parse the request, and the code execution never reaches the routers or handlers. Instead, the error is caught by the global error handler. These kinds of errors (malformed requests that fail during request parsing) are prefixed with **"error when reading request headers"** in Fiber. The implementation adds a check in the global error handler for this specific error message and returns an S3-like XML **BadRequest** error instead. --- s3api/server.go | 7 +++++++ s3err/s3err.go | 6 ++++++ tests/integration/group-tests.go | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/s3api/server.go b/s3api/server.go index 76f66e2..a69e7c6 100644 --- a/s3api/server.go +++ b/s3api/server.go @@ -199,6 +199,13 @@ func globalErrorHandler(ctx *fiber.Ctx, er error) error { ctx.Status(http.StatusBadRequest) return nil } + if strings.Contains(fiberErr.Message, "error when reading request headers") { + // This error means fiber failed to parse the incoming request + // which is a malfoedmed one. Return a BadRequest in this case + err := s3err.GetAPIError(s3err.ErrCannotParseHTTPRequest) + ctx.Status(err.HTTPStatusCode) + return ctx.Send(s3err.GetAPIErrorResponse(err, "", "", "")) + } } // additionally log the internal error diff --git a/s3err/s3err.go b/s3err/s3err.go index ae528a9..7a1e34c 100644 --- a/s3err/s3err.go +++ b/s3err/s3err.go @@ -97,6 +97,7 @@ const ( ErrDuplicateTagKey ErrBucketTaggingLimited ErrObjectTaggingLimited + ErrCannotParseHTTPRequest ErrInvalidURLEncodedTagging ErrInvalidAuthHeader ErrUnsupportedAuthorizationType @@ -382,6 +383,11 @@ var errorCodeResponse = map[ErrorCode]APIError{ Description: "Object tags cannot be greater than 10", HTTPStatusCode: http.StatusBadRequest, }, + ErrCannotParseHTTPRequest: { + Code: "BadRequest", + Description: "An error occurred when parsing the HTTP request.", + HTTPStatusCode: http.StatusBadRequest, + }, ErrInvalidURLEncodedTagging: { Code: "InvalidArgument", Description: "The header 'x-amz-tagging' shall be encoded as UTF-8 then URLEncoded URL query parameters without tag name duplicates.", diff --git a/tests/integration/group-tests.go b/tests/integration/group-tests.go index 988d9c2..afde517 100644 --- a/tests/integration/group-tests.go +++ b/tests/integration/group-tests.go @@ -217,7 +217,7 @@ func TestGetObject(ts *TestState) { ts.Run(GetObject_zero_len_with_range) ts.Run(GetObject_dir_with_range) ts.Run(GetObject_invalid_parent) - ts.Run(GetObject_large_object) + ts.Sync(GetObject_large_object) ts.Run(GetObject_conditional_reads) //TODO: remove the condition after implementing checksums in azure if !ts.conf.azureTests {