-
Notifications
You must be signed in to change notification settings - Fork 968
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
Added utility function to convert body into bytes #2373
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure but we need to careful about making sure the body has a limit. That was a common source of vulnerabilities with hyper's to_bytes
since it didn't set any default limit.
What about
pub async fn to_bytes(body: Body, limit: usize)
Then you can do to_bytes(body, usize::MAX)
if you really don't want a limit.
A limit does get applied by axum when using extractors but if you were to extract the whole Request
and then use to_bytes
then no limit would be applied.
I see, didn't think about that. How should I implement length limiting in that case? I'm thinking about converting into data frames stream and keep joining them until they exceed length limit. |
You can use https://docs.rs/http-body-util/latest/http_body_util/struct.Limited.html. That should handle everything for you. |
Co-authored-by: David Pedersen <david.pdrsn@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!
One last thing: Wanna add a line to the changelog?
Will add |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
This release removes the re-export of hyper::body, which has moved to the http-body crate. Worse, the `to_bytes` method we were using wasn't stablized with the 1.0 release of that crate, and was instead moved to a new http-body-util crate. axum 0.7.2 will likely add a native Body::to_bytes method we can use for this. In the meantime use a similar conversion ourselves. See tokio-rs/axum#2373
Motivation
Sometimes it is useful to convert axum body into bytes, for that one needs to use collect, so to avoid pulling in
http-body-util
I want to provide this function.