Skip to content

fix(response): decode stacked Content-Encoding values in reverse order - #1293

Open
Twiink wants to merge 2 commits into
go-resty:v3from
Twiink:fix/stacked-content-encoding-20261124
Open

Twiink wants to merge 2 commits into
go-resty:v3from
Twiink:fix/stacked-content-encoding-20261124

Conversation

@Twiink

@Twiink Twiink commented Nov 24, 2026

Copy link
Copy Markdown

Responses with a Content-Encoding list currently fail with ErrContentDecompresserNotFound. When the codings are in separate header lines, only the first line is considered, which can select the wrong decoder and return corrupt data errors.

Combine the header values, normalize coding names, and apply the decompressors in reverse order. Validate the entire stack first so DoNotParse callers retain the original bytes for unsupported codings. Close acquired readers when a later decoder cannot be initialized.

The public-client regression covers gzip twice, gzip plus zlib-wrapped deflate, mixed-case names, and separate header lines. All three decoding cases fail on the unchanged v3 baseline. A control verifies unsupported stacks remain untouched in DoNotParse mode.

Validation: the full shuffled race/coverage test suite passes (99.8% statement coverage), plus go vet. HTTP proxy environment variables were unset for the full run because they interfere with existing timeout tests against resty-nowhere.local.

Comment thread response.go
}

if decFunc, f := r.Request.client.ContentDecompressers()[strings.ToLower(ce)]; f {
codings := strings.Split(ce, ",")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things about this line, both run on 5ed1be9.

1. Empty list members. Split keeps them, so they end up as a coding named "" and the response fails. This is a regression for headers that work on v3 today (gzip body, one layer):

Content-Encoding lines v3 this PR
gzip + empty line decoded ErrContentDecompresserNotFound
empty line + empty line (plain body) raw body, no error ErrContentDecompresserNotFound
gzip, ErrContentDecompresserNotFound ErrContentDecompresserNotFound

RFC 9110 section 5.6.1.2 says a recipient has to ignore empty list elements.

2. No limit on the number of codings. Each coding adds a bufio reader and a gzip reader, and the server picks how many. I nested gzip around a one byte body and sent a matching header, with SetResponseBodyLimit(1 << 20) on the client:

codings header body on the wire peak memory
100 0.5 KB 2 KB 17 MB
1000 5 KB 27 KB 26 MB
10000 48 KB 374 KB 459 MB

The body limit does not help, the memory goes into the readers. On v3 these requests fail right away. curl had the same problem (CVE-2023-23916) and now allows 5 codings.

With this all the headers in the first table are accepted, the 100 to 10000 coding responses return the not found error (peak about 20 MB), and your tests still pass:

codings := strings.FieldsFunc(ce, func(c rune) bool { return c == ',' || c == ' ' || c == '\t' })
if len(codings) == 0 {
	return nil
}
if len(codings) > maxContentCodings { // e.g. 5, like curl
	return ErrContentDecompresserNotFound
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the concrete examples. Both issues are addressed in a267649.

Empty Content-Encoding list members are ignored across all header lines. The complete list of nonempty codings is checked before constructing any decoders and is limited to five layers in total. Automatic response decoding returns ErrContentDecompresserNotFound for an over-limit stack; caller-managed responses retain the original bytes.

The 24 focused encoding regressions pass, including empty members and headers, split header lines, the five-layer boundary, and a 100-layer input that constructs no decoders. Full Go 1.27 race tests (99.8% coverage) and vet pass; Go 1.23 full ordinary tests and the encoding-specific race tests and vet also pass. A separate multipart Reader race in the full Go 1.23 race run was reproduced with the same conflicting accesses on unchanged upstream; it was not changed by this revision.

The pushed HEAD is a267649. Its CI and Plumber workflows are currently awaiting maintainer approval.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked on a267649: the empty-member cases from my table decode again, the count is checked before any reader is built, and the encoding tests pass here. Looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants