Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid Content-Length values should be rejected #2014

Open
kenballus opened this issue Jan 4, 2025 · 4 comments
Open

Invalid Content-Length values should be rejected #2014

kenballus opened this issue Jan 4, 2025 · 4 comments
Labels

Comments

@kenballus
Copy link

When cpp-httplib receives a request with an invalid Content-Length header value, it treats it as equivalent to a value of 0 instead of rejecting the request with a 400.

For example, the following request should be rejected with status 400, but is instead accepted:

POST / HTTP/1.1\r\n
Host: whatever\r\n
Content-Length: x\r\n
\r\n

This is caused by the strtoull call on httplib.h:2022. The HTTP RFCs require stricter parsing than strtoull enforces by default.

@yhirose
Copy link
Owner

yhirose commented Jan 5, 2025

@kenballus thanks for the report.

The HTTP RFCs require stricter parsing than strtoull enforces by default.

Where can we see the information?

@kenballus
Copy link
Author

From RFC 9112:

If a message is received without Transfer-Encoding and with an invalid Content-Length header field, then the message framing is invalid and the recipient MUST treat it as an unrecoverable error, unless the field value can be successfully parsed as a comma-separated list (Section 5.6.1 of [HTTP]), all values in the list are valid, and all values in the list are the same (in which case, the message is processed with that single value used as the Content-Length field value). If the unrecoverable error is in a request message, the server MUST respond with a 400 (Bad Request) status code and then close the connection.

RFC 9110 gives the following ABNF rule for Content-Length values:

Content-Length = 1*DIGIT

@kenballus
Copy link
Author

kenballus commented Jan 5, 2025

If you want to use strtoull, you need to check the endptr argument to make sure that the entire length of the data was processed and contained no invalid characters.

You also need to check the beginning of the value to make sure that - and + characters are not there, since strtoull accepts them.

@yhirose
Copy link
Owner

yhirose commented Jan 5, 2025

@kenballus thanks for the detailed helpful informaiton!

@yhirose yhirose added the bug label Jan 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants