-
Notifications
You must be signed in to change notification settings - Fork 20
Look up header names in lowercase #82
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
base: main
Are you sure you want to change the base?
Conversation
|
👋 Thanks for assigning @tankyleo as a reviewer! |
tankyleo
left a comment
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.
TIL thank you ! Let's also do the same in the JWT implementation
rust/auth-impls/src/signature.rs
Outdated
| let auth_header = headers_map | ||
| .get("Authorization") | ||
| .iter() | ||
| .find_map( |
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.
Hmm rather than iterating over all the headers here, I think we can safely make the assumption that all the keys will be lowercased, and do get("authorization") directly. WDYT ?
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.
Since the Authorizer trait is internal to vss-server, I think we should do this, and add a quick doc in the Authorizer trait definition along the lines of "implementations can assume all header names to be lower case"
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.
Makes sense, done.
542950d to
ba10cf0
Compare
| /// Verifies authentication and authorization based on request headers. | ||
| /// Header names are downcased in `headers_map`. | ||
| /// Returns [`AuthResponse`] for an authenticated and authorized user or [`VssError::AuthError`] | ||
| /// for an unauthorized request. |
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.
nit: Let's use this instead thank you:
/// Verifies authentication and authorization based on request headers.
/// Returns [`AuthResponse`] for an authenticated and authorized user or [`VssError::AuthError`]
/// for an unauthorized request.
+ ///
+ /// Implementations can assume that all header names are downcased.
```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.
Done.
rust/server/src/vss_service.rs
Outdated
| .headers | ||
| .iter() | ||
| .map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap_or_default().to_string())) | ||
| .map(|(k, v)| (k.as_str().to_lowercase(), v.to_str().unwrap_or_default().to_string())) |
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.
Here let's do k.to_string(); HeaderName::to_string will always return a lowercased string. I am confident of this, but please double check on your side thank you.
Let's also add a short comment to note that HeaderName always yields lowercase strings.
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.
Right. It works on my end like this.
af24343 to
bdee792
Compare
tankyleo
left a comment
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.
LGTM, can you update the commit message to reflect the latest code ? Thank you, we don't actually do any downcasing anymore ourselves, since http::header::HeaderName already yields all header names in lowercase.
Since the http library downcase header names: https://docs.rs/http/latest/http/header/index.html#headername
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 for pointing this out, valid concern. However, I don't think we want to lean on any particular client-side behavior here, especially not from reqwest which we're about to drop.
The issue is real though, as RFC 2616 states: "Each header field consists of a name followed by a colon (":") and the field value. Field names are case-insensitive."
So rather than trusting that the client sends downcased header fields only, we need to downcase whatever we get before comparing/looking it up. In particular, AUTHORIZATION is just as valid as authorization.
rust/api/src/auth.rs
Outdated
| /// Returns [`AuthResponse`] for an authenticated and authorized user or [`VssError::AuthError`] | ||
| /// for an unauthorized request. | ||
| /// | ||
| /// Implementations can assume that all header names are downcased. |
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.
I don't think it makes sense to make this part of the public API, we should just do case-insensitive comparisons.
bdee792 to
a9c08bb
Compare
That is not about the client side (I misinterpreted it at the beginning). |
Ah, so it's not (only) a) I think it would be good to add comment (and potentially even a debug-assertion) at the point where we convert |
I see several options:
|
Well, I'd still be in favor of what I described above: following Postel's robustness principle, ensure and document we do downcase in our code, but not make it part of the public API whatsoever. |
|
Hm, not sure I understand. |
Where does it do that? You mean because I could imagine that at some point we might want to switch away from |
a9c08bb to
6ff3f8f
Compare
6ff3f8f to
e0bb4a5
Compare
Yup, pretty much! |
UPDATE:
Look up header names in lowercase
Since the http library downcase header names:
https://docs.rs/http/latest/http/header/index.html#headername
Since reqwest downcases header names:https://docs.rs/reqwest/latest/reqwest/header/index.html#headername