fix: apply MaxBodySize to the decompressed body, not the compressed stream - #926
Open
haoku123 wants to merge 1 commit into
Open
fix: apply MaxBodySize to the decompressed body, not the compressed stream#926haoku123 wants to merge 1 commit into
haoku123 wants to merge 1 commit into
Conversation
haoku123
force-pushed
the
fix/max-body-size-after-decompression
branch
from
August 14, 2026 10:16
83c334f to
4c64816
Compare
Contributor
Author
|
Thanks for catching both issues!
Added |
…tream MaxBodySize was wrapped around res.Body before gzip detection, so the limit applied to compressed bytes while io.ReadAll consumed the decompressed stream. This failed to cap decompression bombs (CWE-409) and truncated legitimate compressed responses whose decompressed body was under the limit. Apply a single LimitReader after gzip decompression so MaxBodySize caps the final body handed to callbacks, never the wire bytes. Fixes gocolly#923
haoku123
force-pushed
the
fix/max-body-size-after-decompression
branch
from
August 14, 2026 14:38
4c64816 to
b8fb62f
Compare
Contributor
Author
|
Rebased onto latest master after #925 landed — the multipart tests are no longer duplicated in this branch, and only the MaxBodySize changes remain. Local full test suite passes. |
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.
Description
Fixes #923:
MaxBodySizeis applied to the compressed response stream before gzip decompression, so a small gzip payload can expand far beyond the limit (CWE-409 decompression bomb).Root Cause
In
http_backend.go,io.LimitReaderwrapsres.Bodybefore gzip detection. When colly's own gzip branch runs (e.g..xml.gzresponses withoutContent-Encoding, or a client with compression disabled),io.ReadAllconsumes the decompressed stream with no size limit.Reproduced locally: 2 KiB compressed → 2 MiB decompressed body passed to callbacks with
MaxBodySize(1MiB), matching the issue's repro (2072 → 2097152 bytes).Changes
Keep the existing limit on the raw stream (first line of defense against malformed compression layers) and add a second
LimitReaderafter the gzip branch, so the final body never exceedsMaxBodySize:Verification
TestMaxBodySizeAfterDecompressionserves gzip data on a.xml.gzpath (noContent-Encodingheader, so Go's Transport does not auto-decompress and colly's gzip branch runs):response body = 2097152 bytes, exceeds MaxBodySize 1048576(FAIL)go test ./...: all packages PASSgo vet .: clean