-
Notifications
You must be signed in to change notification settings - Fork 0
Handle token generation failures in auth flow #151
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,23 @@ impl fmt::Display for TokenType { | |
| } | ||
| } | ||
|
|
||
| #[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 {} | ||
|
Comment on lines
+20
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For more idiomatic error handling, you can implement the 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 {} |
||
|
|
||
| pub fn is_valid_token<T: AsRef<str>>(token: T, token_type: TokenType) -> bool { | ||
| let secret = extract_secret_from_env(); | ||
| is_valid_token_with_secret(token, token_type, &secret) | ||
|
|
@@ -51,10 +68,13 @@ fn is_valid_token_with_secret_at<T: AsRef<str>>( | |
| &validation, | ||
| ) { | ||
| Ok(decoded) => decoded, | ||
| Err(_) => return false, | ||
| Err(err) => { | ||
| log::debug!("token decoding failed: {}", err); | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| decoded.claims.exp >= now_timestamp | ||
| decoded.claims.exp > now_timestamp | ||
| } | ||
|
|
||
| #[derive(Debug, Serialize, Deserialize)] | ||
|
|
@@ -64,7 +84,10 @@ struct Claims { | |
| aud: Vec<String>, | ||
| } | ||
|
|
||
| pub fn create_token_with_expiration_in(expiration: Duration, token_type: TokenType) -> String { | ||
| pub fn create_token_with_expiration_in( | ||
| expiration: Duration, | ||
| token_type: TokenType, | ||
| ) -> Result<String, TokenError> { | ||
| let secret = extract_secret_from_env(); | ||
| create_token_with_expiration_in_with_secret(expiration, token_type, &secret) | ||
| } | ||
|
|
@@ -73,7 +96,7 @@ fn create_token_with_expiration_in_with_secret( | |
| expiration: Duration, | ||
| token_type: TokenType, | ||
| secret: &str, | ||
| ) -> String { | ||
| ) -> Result<String, TokenError> { | ||
| create_token_with_expiration_in_with_secret_at( | ||
| expiration, | ||
| token_type, | ||
|
|
@@ -87,13 +110,13 @@ fn create_token_with_expiration_in_with_secret_at( | |
| token_type: TokenType, | ||
| secret: &str, | ||
| now_timestamp: i64, | ||
| ) -> String { | ||
| ) -> Result<String, TokenError> { | ||
| use jsonwebtoken::{encode, Algorithm, EncodingKey, Header}; | ||
|
|
||
| let expiration = now_timestamp | ||
| .checked_add(expiration.num_seconds()) | ||
| .expect("valid timestamp"); | ||
| let expiration = u64::try_from(expiration).expect("non-negative timestamp"); | ||
| .ok_or(TokenError::InvalidExpiration)?; | ||
| let expiration = u64::try_from(expiration).map_err(|_| TokenError::InvalidExpiration)?; | ||
|
|
||
| let claims = Claims { | ||
| sub: "yandex".to_owned(), | ||
|
|
@@ -108,7 +131,7 @@ fn create_token_with_expiration_in_with_secret_at( | |
| &claims, | ||
| &EncodingKey::from_secret(secret.as_bytes()), | ||
| ) | ||
| .unwrap() | ||
| .map_err(TokenError::Encoding) | ||
| } | ||
|
|
||
| fn extract_secret_from_env() -> String { | ||
|
|
@@ -139,7 +162,8 @@ mod tests { | |
| TokenType::Access, | ||
| SECRET, | ||
| NOW, | ||
| ); | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| assert!(is_valid_token_with_secret_at( | ||
| token, | ||
|
|
@@ -156,7 +180,8 @@ mod tests { | |
| TokenType::Access, | ||
| SECRET, | ||
| NOW, | ||
| ); | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| assert!(!is_valid_token_with_secret_at( | ||
| token, | ||
|
|
@@ -173,7 +198,8 @@ mod tests { | |
| TokenType::Access, | ||
| SECRET, | ||
| NOW, | ||
| ); | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| assert!(!is_valid_token_with_secret_at( | ||
| token, | ||
|
|
@@ -190,7 +216,8 @@ mod tests { | |
| TokenType::Access, | ||
| "secret-a", | ||
| NOW, | ||
| ); | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| assert!(!is_valid_token_with_secret_at( | ||
| token, | ||
|
|
@@ -207,19 +234,32 @@ mod tests { | |
| TokenType::Access, | ||
| SECRET, | ||
| NOW, | ||
| ); | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| assert!(is_valid_token_with_secret_at( | ||
| &token, | ||
| TokenType::Access, | ||
| SECRET, | ||
| (NOW + 30) as u64 | ||
| (NOW + 29) as u64 | ||
| )); | ||
| assert!(!is_valid_token_with_secret_at( | ||
| token, | ||
| TokenType::Access, | ||
| SECRET, | ||
| (NOW + 31) as u64 | ||
| (NOW + 30) as u64 | ||
| )); | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_expiration_returns_error() { | ||
| let result = create_token_with_expiration_in_with_secret_at( | ||
| Duration::seconds(1), | ||
| TokenType::Access, | ||
| SECRET, | ||
| i64::MAX, | ||
| ); | ||
|
|
||
| assert!(matches!(result, Err(super::TokenError::InvalidExpiration))); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The
redirect_uriparameter 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 authorizationcodeandstateparameters.To remediate this, you should validate the
redirect_uriagainst a list of allowed URIs, similar to the check performed inissue_token.rs.