-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
check max_content_length more consistently #1513
Comments
I also just stumbled across this when developing my flask application. I had assumed that simply setting Instead, a 413 was never raised, because |
Reviewing #690, I think it's probably time to deprecate this attribute. As I pointed out there, limiting the body size is better handled by the HTTP (or WSGI) server. We could check the Actually, unless I'm misreading the code, Werkzeug's form parser right now only seems to do that basic check. If Then there's the problem that the length of a JSON body can be very deceptive. It's possible to craft relatively small bodies that parse to huge memory sizes, and that's not something that we (or any part of the server pipeline, probably) would be able to detect. If that's a concern, you should subclass Overall, I think having this property in Werkzeug is more misleading than helpful. While we could move the rudimentary check to when |
I don't think it should be done at the server level, as a user may want to have a higher limit on some routes compared with others. For example a route that purely consumes any data (without building up in memory) may want no limit, or an authorized upload route may want a higher limit than unauthorized. I think this is quite feasible with streamed bodies, especially async consumption. |
So the changes would be something like:
The one problem here is that |
@greyli As you requested, I proved further details in connection with the ticket here.With this setup, it is working properly (OS: Windows 10 Pro 64-bit):
|
Following up on my previous comment: If a max content length is set, it should wrap the stream regardless of if |
On second thought, it's probably a bad idea to raise |
We already raise |
pallets/flask#1200 was about
MAX_CONTENT_LENGTH
not being checked always, except when callingrequest.form
.request.get_json()
and others (request.get_data()
?) did not trigger the check. This was fixed in https://github.com/pallets/werkzeug/pull/1126/files (Jun 2017).However, in another ticket #690 David wrote (Jan 2019) that:
This makes me wonder if the feature is supported, or useful, or not? If the check works, why shouldn't we use it?
Even in pallets/flask#2690 (Apr 2018):
nginx, uWSGI et al. might have it, but gunicorn doesn't, ref benoitc/gunicorn#1733 (just a fyi for anyone reading).
In pallets/flask#2690 (Apr 2018):
Questions:
Why not make the check always, asap, when reading the request? I mean, headers come first right, so if the value given in
Content-Length
header exceedsMAX_CONTENT_LENGTH
, why not raise error right away, before even attempting to read the request body?If I set
MAX_CONTENT_LENGTH
to 1 MB, and client sends 1 MB + 1 byte, will werkzeug read until 1 MB and only then raise error when it sees that there would be 1 byte more? Or will it actually just check the header?Maybe clarify the docs a bit. Now it says:
The docs don't mention how the check is forced (the questions above). Docs don't even mention what happens when the constraint is violated - will it raise error or gap the stream?
The text was updated successfully, but these errors were encountered: