Validate Content-Length format in ClientRequest#12385
Validate Content-Length format in ClientRequest#12385jmestwa-coder wants to merge 3 commits intoaio-libs:masterfrom
Conversation
|
Could you add an end-to-end test in test_web_functional.py? I think this code is actually unreachable and even the current check can be removed. I'm pretty sure we already validate it in the parser. |
49f902f to
4296e72
Compare
4296e72 to
dd083ea
Compare
Reject non-RFC-compliant Content-Length values to align with HTTP parser behavior and ensure consistent validation.
dd083ea to
a66944d
Compare
for more information, see https://pre-commit.ci
|
Added an end-to-end test in Let me know if you'd like any changes. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #12385 +/- ##
=======================================
Coverage 98.92% 98.92%
=======================================
Files 134 134
Lines 46616 46641 +25
Branches 2429 2430 +1
=======================================
+ Hits 46114 46139 +25
Misses 373 373
Partials 129 129
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Merging this PR will not alter performance
Comparing Footnotes
|
| with pytest.raises(ValueError, match="Invalid Content-Length header"): | ||
| req._get_content_length() | ||
|
|
||
| req.headers["Content-Length"] = " 100" |
There was a problem hiding this comment.
Strictly speaking, this produces a valid header: Content-Length: 100
We probably don't have a real preference on whether it's rejected or not, so probably better to just use another test example. e.g. ५ <- devengali number 5, which would pass through an int() call.
Sorry, I thought this was validating the response. I've just realised this is validating the request, i.e. the data from the developer. I guess this looks fine as a simple improvement to sanity checking then. |
What do these changes do?
This change adds strict validation for the
Content-Lengthheader inClientRequest.Currently,
_get_content_length()uses Python’sint()for parsing, which accepts non-RFC-compliant values such as negative numbers or values with whitespace. This patch introduces a DIGITS-only validation to ensure the header follows the same format already enforced by the HTTP parser.Are there changes in behavior for the user?
Yes, but only for invalid inputs.
Content-Lengthvalues (e.g.,"0","100") are unaffected."-100"," 100","+100") will now raise aValueErrorinstead of being accepted.This aligns behavior across send and receive paths and avoids unexpected behavior when invalid values are provided.
Is it a substantial burden for the maintainers to support this?
No.
This should not increase maintenance overhead.
Related issue number
N/A
Checklist
CONTRIBUTORS.txtCHANGES/folder