Repository navigation
fix(middleware): return 400 for invalid gzip request bodies in Decompress - #3187
Open
nk-batman21 wants to merge 1 commit into
Open
nk-batman21 wants to merge 1 commit into
nk-batman21 wants to merge 1 commit into
Conversation
…ress When a request has Content-Encoding: gzip but the body does not start with a valid gzip header (a plain body, or a truncated one), gzip.Reader.Reset fails with gzip.ErrHeader or io.ErrUnexpectedEOF. Decompress returned that error as is, so the default error handler answered 500 for malformed client input. Corrupt data after a valid header already ends up as 400 through Bind. Wrap the error as 400 Bad Request, keeping errors that already carry a status code (such as a BodyLimit 413), the same way the JSON serializer handles body read errors.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Decompressanswers 500 when a request hasContent-Encoding: gzipbut the body is not a gzip stream.gzip.Reader.Resetfails withgzip.ErrHeaderfor a plain body orio.ErrUnexpectedEOFfor a truncated header. The middleware returns that error unchanged, so the default error handler treats it as an internal error.This happens with a client that sets the header but sends the body uncompressed (for example a retry path that resends the original buffer), with a truncated upload, or with a scanner. The input is malformed, but it is logged as a server error, it trips 5xx alerts, and clients that retry on 5xx will retry a request that can never succeed. Corrupt data after a valid header already gets 400, because the read error comes out of
Bind. Withe.Use(middleware.Decompress())and a handler that binds JSON, on master:The change wraps the error in
echo.ErrBadRequestthe same wayDefaultJSONSerializer.Deserializehandles body read errors. An error that already has a status is returned unchanged, so aBodyLimit413 raised while the header is read stays 413. The original error is still available througherrors.Is. This follows #2170 (fixed in #2191), where BasicAuth changed invalid base64 from 500 to 400.Ran:
go test ./middleware/ ./echotest/,go vet ./...,golint -set_exit_status ./...,staticcheck ./...Checked:
TestDecompressInvalidGzipBodyfails on master (500 != 400).TestDecompressKeepsBodyLimitErrorfails if the status check is removed (400 != 413).