-
Notifications
You must be signed in to change notification settings - Fork 18.1k
net/http: unwrap ResponseWriter in MaxBytesReader for finding requestTooLarger #73754
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
Comments
cc @neild, not sure if this needs to be a proposal? |
This change seems fine to me. I don't think it needs a proposal. This doesn't add any new exported APIs, and while there's a user-visible behavioral change I think the current behavior of not unwrapping the ResponseWriter is closer to a bug than a missing feature. |
…sReader The current implementation of MaxBytesReader only calls requestTooLarge when the provided ResponseWriter directly implements the internal requestTooLarger interface. This breaks expected behavior when the ResponseWriter is wrapped (e.g., middleware) and the inner writer implements requestTooLarger. This change adds an unwrapping loop, similar to what http.newResponseController does, to traverse through any Unwrap() chain and properly call requestTooLarge if found. This improves compatibility with custom or middleware-wrapped writers while preserving current behavior for unwrapped ones. Fixes golang#73754
Change https://go.dev/cl/676676 mentions this issue: |
Change https://go.dev/cl/677055 mentions this issue: |
Uh oh!
There was an error while loading. Please reload this page.
Summary
http.MaxBytesReader
in the standard library uses an internal interface (requestTooLarger
) on the providedhttp.ResponseWriter
to notify the server when a request body exceeds the specified limit. However, when a customResponseWriter
wrapper is used, this internal callback is not invoked because the library does not unwrap the writer chain to find an implementation.This causes the server to miss the "request too large" event, not closing the connection.
Proposal
Modify
http.MaxBytesReader
to recursively unwrap the givenResponseWriter
if it implements anUnwrap() http.ResponseWriter
method (similar to howhttp.ResponseController
is handled), and call the internalrequestTooLarge()
callback on the innermostResponseWriter
that implements it.This change would maintain backward compatibility, and improve behavior in common scenarios where middleware wraps the writer.
Motivation
When developers wrap
http.ResponseWriter
to implement middleware features like logging, metrics, or response modification, they typically embed the originalResponseWriter
.The current design of
MaxBytesReader
checks only the top-level writer for the internalrequestTooLarger
interface. As a result, an oversized request does not close the connection.Example and Reproduction Steps
Here is a minimal example that illustrates the problem when wrapping
ResponseWriter
:Curl with a request body exceeding the limit doesn't close the connection.
I have a patch ready implementing this fix and can submit a PR upon approval.
The text was updated successfully, but these errors were encountered: