Look up header names in lowercase#82
Conversation
|
👋 Thanks for assigning @tankyleo as a reviewer! |
tankyleo
left a comment
There was a problem hiding this comment.
TIL thank you ! Let's also do the same in the JWT implementation
| let auth_header = headers_map | ||
| .get("Authorization") | ||
| .iter() | ||
| .find_map( |
There was a problem hiding this comment.
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.
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.
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.
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.
```| .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.
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.
Right. It works on my end like this.
af24343 to
bdee792
Compare
tankyleo
left a comment
There was a problem hiding this comment.
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.
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.
| /// 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.
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