Skip to content
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

return a 4xx status code on non-ascii headers #172

Closed
wants to merge 1 commit into from

Conversation

ririsoft
Copy link

@ririsoft ririsoft commented Jan 4, 2021

Hello,

server::decode panics on valid UTF-8 but none ASCII headers and return 500 Internal Server Error on none valid UTF-8 headers.
I believe a 400 Bad Request should be returned instead in both cases.

Note: Theorically the additional check will slightly decrease server side headers decoding performances. I am not sure about the project's policy, but usage of unsafe code std::str::from_utf8_unchecked could remove the performance penalty while remaining 100% safe. I have not benchmarked this at all however.

What do you think ?
Cheers.

None utf-8 or ascii headers should not
make a server panic, but return 400 BadRequest
instead.
@@ -80,6 +80,8 @@ where
req.set_version(Some(http_types::Version::Http1_1));

for header in httparse_req.headers.iter() {
// https://tools.ietf.org/html/rfc822#section-3.1
http_types::ensure_status!(header.value.is_ascii(), 400, "None ascii header");
Copy link
Member

@jbr jbr Jan 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This requires two iterations over each byte — one for the is_ascii() check and the other for the from_utf8. I believe we should just map the error value for the result of from_utf8, like std::str::from_utf8(header.value).map_err(|_| Error::from_str(400, "non-ascii header"))? or using Status, std::str::from_utf8(header.value).status(400)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbr
Copy link
Member

jbr commented Jan 25, 2021

On further examination, how is this a panic? The current code uses a ? to return an Err, which is not a panic. It might not have the desired status code, but a 500 is not a panic

@jbr jbr changed the title fix panic on none ascii headers return a 4xx status code on non-ascii headers Jan 25, 2021
@ririsoft
Copy link
Author

Hi @jbr ,
Thank you for your time on this.

On further examination, how is this a panic? The current code uses a ? to return an Err, which is not a panic. It might not have the desired status code, but a 500 is not a panic

The panic is easily reproducible with the test cases I added to the pull request.

Unwrap occurs here: https://github.com/http-rs/http-types/blob/41655460b0229064535308e329417f8de014c2a8/src/headers/headers.rs#L66
Error comes from here:
https://github.com/http-rs/http-types/blob/41655460b0229064535308e329417f8de014c2a8/src/headers/header_value.rs#L80

I believe the original title was valid, there is truly a panic.

@jbr
Copy link
Member

jbr commented Jan 27, 2021

My apologies, I misunderstood. It seems to me like we should fix this by removing the unwrap in headers.rs, especially since the http-types v3 window is open.

@yoshuawuyts, what do you think about append returning Result<()>? Or doing a lossy conversion? Panicking whenever to_header_values() fails seems problematic, and fixing it by handling this specific reason it might fail doesn't address the root concern. In particular, if someone implements the ToHeaderValues trait for their type, the interface gives the impression that the Result will be handled gracefully, but then we unwrap it in the primary usage of that api. Maybe ToHeaderValues should not return a Result to make implementers address failures, or we should propagate that Result up to user code

@jbr
Copy link
Member

jbr commented Jan 27, 2021

This seems more complex than "only ascii utf8 allowed in headers" — https://www.rfc-editor.org/rfc/rfc8187.html

@ririsoft
Copy link
Author

This seems more complex than "only ascii utf8 allowed in headers" — https://www.rfc-editor.org/rfc/rfc8187.html

From my understanding, I may be wrong, RFC8187 introduce the possibility to add UTF-8 characters percent encoded, so that it remains in the ascii space.

@jbr
Copy link
Member

jbr commented May 1, 2021

I believe this is the same issue as #187

@shanipribadi
Copy link

shanipribadi commented May 4, 2022

I believe the panic thrown by http_types::headers::Headers append/insert is now handled in http-rs/http-types#385 by the functions returning a Result now.

Although that version is main-only and 3.0.0 is unreleased yet.

@ririsoft
Copy link
Author

I am not using http-rs anymore and I am thus not interested in working on this PR further.

@ririsoft ririsoft closed this May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants