-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Exceeding request_body
's max_size
causes unexpected rejection behavior
#4558
Comments
Hmm. Do you have evidence in your logs that that's the response code it writes? I'm not convinced that Caddy itself sends Basically, Caddy is not specifically handling this error (and it's kinda tricky to because it would involve a string comparison to check it, because it's not a specific error type), so it'll just trigger Caddy's default error handling behaviour. You can use
|
Thanks for opening an issue! We'll look into this. It's not immediately clear to me what is going on, so I'll need your help to understand it better. Ideally, we need to be able to reproduce the bug in the most minimal way possible. This allows us to write regression tests to verify the fix is working. If we can't reproduce it, then you'll have to test our changes for us until it's fixed -- and then we can't add test cases, either. I've attached a template below that will help make this easier and faster! This will require some effort on your part -- please understand that we will be dedicating time to fix the bug you are reporting if you can just help us understand it and reproduce it easily. This template will ask for some information you've already provided; that's OK, just fill it out the best you can. 👍 I've also included some helpful tips below the template. Feel free to let me know if you have any questions! Thank you again for your report, we look forward to resolving it! Template
Instructions -- please heed otherwise we cannot help you (help us help you!)
Example of a tutorial: Create a config file: |
@francislavoie That's what's reported in the browser console. I'll investigate further. Is the expected behavior to return |
Please do. What's in Caddy's logs? Turn on the
No, there's no "expected behaviour", really. Like I said, there is no specific HTTP status code attached to |
@francislavoie Testing with |
Ah, okay I understand what's going on then. So since it's a reader wrapper, basically the thing that invokes reading the request body is what will receive the error. So the I don't think we can really do much about this unless we either get the Go stdlib to change the error it emits to something we can do a type assertion on to change the response code, or we re-implement the wrapper in Caddy itself to do that. It's a really simple wrapper, so we could just do that. 🤷♂️ |
@francislavoie Thank you for the insight. It would be great for Caddy's wrapper to be enhanced. If it's added, I'd be available to test if needed. In the meantime, I'll handle the request size limit in the app. |
@francislavoie Would the wrapper enhancement use |
Digging into the code a bit more, it's not possible to yoink out the code from stdlib since it uses private/unexported functions to manipulate the HTTP response state. There is an open issue golang/go#30715 which is relevant here, if it were implemented (which it seems it will be, the proposal was accepted), we could add a check for this specific error to write a specific status code. So I'll keep this issue open and mark it as a feature request that requires an upstream change.
Have you tried my suggestion of handling it with
No, that's not a reliable way to do it. Clients (bad actors) can lie and send an invalid |
Thank you once again for the excellent information.
Frankly no. Respectfully, IMHO the Caddy docs aren't very good, so I don't have confidence I'd be able to figure out how to use it in the amount of time I would have budgeted for the task, and I'd rather not clog this issue up with only tangentially related questions. If you believe it would be helpful to others until the Go enhancement is in place, and you wouldn't mind implementation questions here or in another issue, say the word and I'll give it a... go.
Exactly! |
Digging into this a bit deeper again. I tried out my suggested So even if we could change the response code, the client would never see it. Quick google search shows that's intended and the only way it can actually work to stop receiving data from the client. See https://stackoverflow.com/questions/35173819/request-body-too-large-causing-connection-reset-in-go for example. This is how I tested it: Caddyfile:
Send a
In Caddy's logs:
First line is the Notice that the last log line does show that Caddy attempts to write status I think all that we can really do is flesh out the docs on https://caddyserver.com/docs/caddyfile/directives/request_body to better explain how it will behave, clarifying that the client connection will be closed if too many bytes are tried to be read from the request. |
@francislavoie Does the block
configure Caddy to return 413 for all HTTP request errors, potentially including some unrelated to exceeding request max size? |
Caddy v1 had a special "limits" handler that used a |
@mholt we already do this https://github.com/caddyserver/caddy/blob/master/modules/caddyhttp/requestbody/requestbody.go The code in the v1 branch is a copy of the stdlib, but I don't think it actually works correctly because |
Ah, sorry, I misunderstood this issue. I do think this could be handled better in the code though too. Maybe we can somehow wrap the returned error from MaxBytesReader with one of our structured errors that adds status code. |
I verified that 3d616e8 will now return HTTP 413 from |
Caddy version: 2.4.6
The documentation does not state how it's handled when a request exceeds
max_size
. I would expect413 Payload Too Large
however502 Bad Gateway
is how Caddy responds.May I ask, if that is expected behavior, what the rationale is for using that code?
The text was updated successfully, but these errors were encountered: