Visitar URL original
gh-150751: validate http.client Content-Length and chunk-size by metsw24-max · Pull Request #150752 · python/cpython · GitHub
Skip to content

gh-150751: validate http.client Content-Length and chunk-size - #150752

Merged
encukou merged 2 commits into
python:mainfrom
metsw24-max:httpclient-strict-length-parsing
Oct 8, 2026
Merged

encukou merged 2 commits into
python:mainfrom
metsw24-max:httpclient-strict-length-parsing

Conversation

@metsw24-max

@metsw24-max metsw24-max commented Jun 2, 2026 •

Copy link
Copy Markdown
Contributor

Noticed begin and _read_next_chunk_size derive the response body framing from int(length) and int(line, 16), but int also accepts a leading sign, underscores, surrounding whitespace and an 0x prefix that RFC 9112 forbids for Content-Length (1DIGIT) and chunk-size (1HEXDIG). So +5, 5_0 or a negative chunk size parse cleanly here while a strict front end frames the response differently. This matches both tokens against the grammar before converting.

RFC 9112 defines Content-Length as 1*DIGIT and chunk-size as 1*HEXDIG, but int() also accepts a sign, underscores, surrounding whitespace and an 0x prefix, so malformed framing values were parsed instead of rejected.
@metsw24-max

Copy link
Copy Markdown
Contributor Author

gentle ping

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Sep 22, 2026
Comment thread Lib/http/client.py Outdated
Comment on lines +161 to +164
# RFC 9112: Content-Length = 1*DIGIT and chunk-size = 1*HEXDIG. int() is more
# permissive (it accepts a leading sign, underscores, surrounding whitespace
# and, in base 16, an "0x" prefix and non-ASCII digits), so the body-framing
# values are matched against the grammar before being passed to int().

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is too verbose.
(There have been suggestions to extend what int() accepts. We'll not go over comments like this if that happens.)

Suggested change
# RFC 9112: Content-Length = 1*DIGIT and chunk-size = 1*HEXDIG. int() is more
# permissive (it accepts a leading sign, underscores, surrounding whitespace
# and, in base 16, an "0x" prefix and non-ASCII digits), so the body-framing
# values are matched against the grammar before being passed to int().
# RFC 9112: Content-Length = 1*DIGIT and chunk-size = 1*HEXDIG.
# int() is more permissive, so we match against the grammar before calling it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Applied your wording. Fair point that the enumeration would go stale if int() is extended, and the grammar reference is the part that actually needs to be there.

Comment thread Lib/http/client.py Outdated
i = line.find(b";")
if i >= 0:
line = line[:i] # strip chunk-extensions
line = line.strip()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
line = line.strip()
line = line.rstrip()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. rstrip() is the better fit, since leading whitespace isn't in the grammar either and should fail the match rather than get trimmed away first. I've added 5 to test_malformed_chunk_size so that case stays covered.

@encukou
encukou merged commit 2eb2af8 into python:main Oct 8, 2026
55 checks passed
@encukou

encukou commented Oct 8, 2026

Copy link
Copy Markdown
Member

Thank you!

@encukou encukou added needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Oct 8, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @metsw24-max for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Thanks @metsw24-max for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Oct 8, 2026

Copy link
Copy Markdown

GH-159020 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Oct 8, 2026
@bedevere-app

bedevere-app Bot commented Oct 8, 2026

Copy link
Copy Markdown

GH-159021 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants