HttpHeaders: reject Content-Length with leading + or - sign#67635
Open
DeagleGross wants to merge 6 commits into
Open
HttpHeaders: reject Content-Length with leading + or - sign#67635DeagleGross wants to merge 6 commits into
+ or - sign#67635DeagleGross wants to merge 6 commits into
Conversation
RFC 9112 section 6.2 defines Content-Length as 1*DIGIT. The HTTP/1.1 request header parser used Utf8Parser.TryParse<long>, which accepts a leading '+' or '-' sign. Values like '+123' or '-0' were therefore accepted (parsed as 123 and 0) instead of being rejected with 400 Bad Request. The custom-encoding path used long.TryParse with NumberStyles.Integer, which also allows a leading sign, so it had the same defect. Add a leading-digit check on the byte path and switch the custom-encoding path to NumberStyles.None. Extend BadRequestIfContentLengthInvalid to cover '+1', '+0', '-0'.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR tightens Kestrel’s Content-Length parsing to align with RFC 9110 §8.6 (Content-Length = 1*DIGIT) by rejecting values that Utf8Parser.TryParse would otherwise accept due to leading sign handling.
Changes:
- Reject
Content-Lengthvalues that start with+or-in the fast-path byte parsing. - Tighten the custom-encoding parsing path by switching from
NumberStyles.IntegertoNumberStyles.Noneto disallow signs/whitespace. - Extend functional tests to cover
+1,+0, and-0invalid values.
Show a summary per file
| File | Description |
|---|---|
| src/Servers/Kestrel/test/InMemory.FunctionalTests/BadHttpRequestTests.cs | Adds coverage for signed Content-Length values that must now be rejected. |
| src/Servers/Kestrel/Core/src/Internal/Http/HttpRequestHeaders.cs | Adds leading-sign rejection in the byte parsing path and tightens the custom-encoding parsing style. |
Copilot's findings
- Files reviewed: 2/2 changed files
- Comments generated: 1
BrennanConroy
approved these changes
Jul 8, 2026
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.
Per RFC 9110 8.6
Content-Lengthis1*DIGIT, butUtf8Parser.TryParse()accepts leading+or-, which accepts values such as+1. Added a guard to dissallow leading+or-.