Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP: Can I trust 'Content-Length' request header value

I'm developing a file uploading service. I want my users to be restricted for total uploaded files sizes, i.e. they have quotas for uploaded files. So I want to check for available quota as a user starts upload a new file. The easiest way is to take 'Content-Length' header value of the POST request and check it against remaining user's quota. But I'm anxious about whether I can trust 'Content-Length' value. What if a bad guy specifies a small value in 'Content-Length' header and starts uploading a huge file.

Should I check additionally during reading from input stream (and saving a file on disk) or it's redundant (and such a situation should be detected by Web servers)?

like image 835
Shrike Avatar asked Jun 17 '14 16:06

Shrike


People also ask

Is Content Length header reliable?

It should be, and usually is, accurate. However it is entirely possible for a web server to report a incorrect content length although this obviously doesn't happen often (I recall old versions of apache retuning nonsensical content lengths on files > 2GB). Save this answer.

Is Content Length header necessary in HTTP?

The Content-Length is optional in an HTTP request. For a GET or DELETE the length must be zero. For POST, if Content-Length is specified and it does not match the length of the message-line, the message is either truncated, or padded with nulls to the specified length.

What is Content Length in HTTP request header?

The Content-Length header indicates the size of the message body, in bytes, sent to the recipient.

Who adds Content Length header?

Clients (browsers, mobile apps) automatically add a Content-Length header to the POST request based on the size of your data in the request.


1 Answers

Short answer: it's safe

Long answer: Generally, servers are required to read (at most) as many bytes as are specified in the Content-Length request header. Any bytes that comes after that are expected to indicate a completely new request (reusing the same connection).

I'd assume that this requirement is validated on the server by checking that the next few bytes can be parsed as a request-line.

request-line = method SP request-target SP HTTP-version CRLF

If your bad guy is not smart enough to inject request headers in the right locations of the message body, the server should (must?) automatically treat the entire chain of requests as invalid and abort the file upload.

If your guy does inject new request headers in the message body (a.k.a. request smuggling), each resulting request is technically still valid and you'll still be able to trust that the Content-Length is valid for each message body. You just have to watch out for a different kind of attacks. For example: you might have a proxy installed that filters incoming requests, but only does so by checking the headers of the first request. The smuggled requests get a free pass, which is an obvious security breach.

like image 138
Steven Liekens Avatar answered Sep 28 '22 08:09

Steven Liekens