This will depend on your external server. If it has any configuration to block a large request even before it enters your application, use it.
If you want to block this with code, I see two approaches:
- See the HTTP header Content-Length. If this is more than you can handle, immediately reject the request.
- Do not trust the headers and start reading the body of the request until you reach your limit. Please note that this is not a very smart way, but it may work. =)
Trusting the HTTP header, you may run into some problems. Suppose someone sends a request with Content-Length: 1024, but sends a 1GB request body. If your front-end server trusts the header, it will start reading this request and later find out that the request body is actually much larger than it should be. This situation can still fill your server disk, even if it is a request that "passes" too much verification.
Although this may happen, I think that trusting the title would be a good starting point.
source share