Handle token generation failures in auth flow#151
Conversation
Summary of ChangesHello @chipp, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the robustness and reliability of the authentication flow by implementing comprehensive error handling for token generation and URL parsing. It ensures that the system can gracefully recover from failures, provide meaningful error responses to clients, and prevent unexpected crashes, thereby improving the overall stability of the web service. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request significantly enhances the robustness of the authentication service by replacing panics with proper Result-based error handling in the token generation flow. However, a high-severity Open Redirect vulnerability was identified in the authorize endpoint, where the redirect_uri provided by the user is not validated against an allow-list. This could lead to the leakage of authorization codes and must be addressed. Additionally, while the changes are consistently applied and tests updated, consider making the error handling even more idiomatic using the From trait to simplify error propagation.
| let mut url = match Url::parse(auth.redirect_uri.as_ref()) { | ||
| Ok(url) => url, | ||
| Err(_) => return Ok(None), | ||
| }; |
There was a problem hiding this comment.
The redirect_uri parameter is parsed and used as the base for a redirect without validation against an allow-list of trusted domains. This constitutes an Open Redirect vulnerability. An attacker can craft a malicious link that, once the user authenticates, redirects them to an attacker-controlled site, potentially leaking the authorization code and state parameters.
To remediate this, you should validate the redirect_uri against a list of allowed URIs, similar to the check performed in issue_token.rs.
| let mut url = match Url::parse(auth.redirect_uri.as_ref()) { | |
| Ok(url) => url, | |
| Err(_) => return Ok(None), | |
| }; | |
| let mut url = match Url::parse(auth.redirect_uri.as_ref()) { | |
| Ok(url) if url.as_str() == "https://social.yandex.net/broker/redirect" => url, | |
| _ => return Ok(None), | |
| }; |
| #[derive(Debug)] | ||
| pub enum TokenError { | ||
| InvalidExpiration, | ||
| Encoding(jsonwebtoken::errors::Error), | ||
| } | ||
|
|
||
| impl fmt::Display for TokenError { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| match self { | ||
| Self::InvalidExpiration => write!(f, "invalid token expiration"), | ||
| Self::Encoding(err) => write!(f, "token encoding failed: {err}"), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl std::error::Error for TokenError {} |
There was a problem hiding this comment.
For more idiomatic error handling, you can implement the From trait for jsonwebtoken::errors::Error. This will allow you to use the ? operator to automatically convert the error type at call sites.
After applying this suggestion, you can simplify the code at lines 129-134 to:
encode(
&header,
&claims,
&EncodingKey::from_secret(secret.as_bytes()),
)?#[derive(Debug)]
pub enum TokenError {
InvalidExpiration,
Encoding(jsonwebtoken::errors::Error),
}
impl From<jsonwebtoken::errors::Error> for TokenError {
fn from(err: jsonwebtoken::errors::Error) -> Self {
Self::Encoding(err)
}
}
impl fmt::Display for TokenError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidExpiration => write!(f, "invalid token expiration"),
Self::Encoding(err) => write!(f, "token encoding failed: {err}"),
}
}
}
impl std::error::Error for TokenError {}
No description provided.