Skip to content

Limit chained Content-Encoding decoders to 5#1027

Open
Kludex wants to merge 5 commits into
mainfrom
limit-content-encoding-chain
Open

Limit chained Content-Encoding decoders to 5#1027
Kludex wants to merge 5 commits into
mainfrom
limit-content-encoding-chain

Conversation

@Kludex
Copy link
Copy Markdown
Member

@Kludex Kludex commented Jun 6, 2026

A response can carry multiple Content-Encoding values, which we decode by building a MultiDecoder chain. Today that chain is unbounded - a response can list an arbitrary number of encodings and we'll happily build a decoder for each one.

Real responses apply one encoding, occasionally two. There's no legitimate reason to chain more than a handful, so this caps the chain at 5 and raises DecodingError past that.

The limit lives as a ClassVar on MultiDecoder so it stays easy to find and adjust.

AI Disclaimer

This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 6, 2026

Docs preview:

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Jun 6, 2026

Merging this PR will not alter performance

✅ 15 untouched benchmarks
⏩ 7 skipped benchmarks1


Comparing limit-content-encoding-chain (dea9472) with main (873af0a)

Open in CodSpeed

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

No issues found across 3 files

Re-trigger cubic

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0190de0d72

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/httpx2/httpx2/_decoders.py Outdated
Comment on lines +240 to +241
if len(children) > self.max_decode_links:
raise DecodingError(f"Too many content encodings in the chain: {len(children)} > {self.max_decode_links}")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Enforce the decoder limit before allocating children

Because this check runs only after _get_content_decoder() has already appended decoder_cls() for every supported Content-Encoding value, a response header containing thousands of gzip/deflate entries still allocates thousands of zlib decoder objects before this error is raised. That leaves the intended DoS guard largely ineffective for large but valid header blocks; the limit needs to be checked while parsing/counting encodings, before instantiating additional decoders.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good point - moved the check to count the parsed encodings before instantiating any decoders, so an over-long header is rejected up front. ef171fc

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 2 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread src/httpx2/httpx2/_decoders.py
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 3 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread src/httpx2/httpx2/_models.py Outdated
@Kludex Kludex deployed to cloudflare June 6, 2026 10:48 — with GitHub Actions Active
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant