Skip to content

Handle verification when multiple algorithms are allowed in the settings #421

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/decoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,10 @@ fn verify_signature<'a>(
return Err(new_error(ErrorKind::MissingAlgorithm));
}

if validation.validate_signature {
for alg in &validation.algorithms {
if key.family != alg.family() {
return Err(new_error(ErrorKind::InvalidAlgorithm));
}
}
if validation.validate_signature
&& !validation.algorithms.iter().any(|alg| alg.family() == key.family)
{
return Err(new_error(ErrorKind::InvalidAlgorithm));
}

let (signature, message) = expect_two!(token.rsplitn(2, '.'));
Expand All @@ -229,6 +227,10 @@ fn verify_signature<'a>(
return Err(new_error(ErrorKind::InvalidAlgorithm));
}

if header.alg.family() != key.family {
return Err(new_error(ErrorKind::InvalidAlgorithm));
}

if validation.validate_signature && !verify(signature, message.as_bytes(), key, header.alg)? {
return Err(new_error(ErrorKind::InvalidSignature));
}
Expand Down
54 changes: 54 additions & 0 deletions tests/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ fn decode_token() {
claims.unwrap();
}

#[test]
#[wasm_bindgen_test]
fn decode_token_with_multiple_algorithms_allowed() {
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJiQGIuY29tIiwiY29tcGFueSI6IkFDTUUiLCJleHAiOjI1MzI1MjQ4OTF9.9r56oF7ZliOBlOAyiOFperTGxBtPykRQiWNFxhDCW98";
let mut validation = Validation::new(Algorithm::HS256);
validation.algorithms.push(Algorithm::RS256);
let claims = decode::<Claims>(token, &DecodingKey::from_secret(b"secret"), &validation);
claims.unwrap();
}

#[test]
#[wasm_bindgen_test]
#[should_panic(expected = "InvalidToken")]
Expand Down Expand Up @@ -126,6 +136,50 @@ fn decode_token_wrong_algorithm() {
claims.unwrap();
}

#[test]
#[wasm_bindgen_test]
#[should_panic(expected = "MissingAlgorithm")]
fn decode_missing_algorithm() {
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJiQGIuY29tIiwiY29tcGFueSI6IkFDTUUifQ.I1BvFoHe94AFf09O6tDbcSB8-jp8w6xZqmyHIwPeSdY";
let mut validation = Validation::new(Algorithm::HS256);
validation.algorithms = vec![];
let claims = decode::<Claims>(token, &DecodingKey::from_secret(b"secret"), &validation);
claims.unwrap();
}

#[test]
#[wasm_bindgen_test]
#[should_panic(expected = "InvalidAlgorithm")]
fn decode_mismatched_key_algorithm() {
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJiQGIuY29tIiwiY29tcGFueSI6IkFDTUUifQ.I1BvFoHe94AFf09O6tDbcSB8-jp8w6xZqmyHIwPeSdY";
let mut validation = Validation::new(Algorithm::HS256);
validation.algorithms.push(Algorithm::RS256);
let key = &DecodingKey::from_rsa_components("aGk", "aGk").unwrap();
let claims = decode::<Claims>(token, &key, &validation);
claims.unwrap();
}

#[test]
#[wasm_bindgen_test]
#[should_panic(expected = "InvalidAlgorithm")]
fn decode_invalid_header_algorithm() {
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJiQGIuY29tIiwiY29tcGFueSI6IkFDTUUifQ.I1BvFoHe94AFf09O6tDbcSB8-jp8w6xZqmyHIwPeSdY";
let validation = Validation::new(Algorithm::RS256);
let key = &DecodingKey::from_rsa_components("aGk", "aGk").unwrap();
let claims = decode::<Claims>(token, &key, &validation);
claims.unwrap();
}

#[test]
#[wasm_bindgen_test]
#[should_panic(expected = "InvalidAlgorithm")]
fn wrong_decoding_key_family() {
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJiQGIuY29tIiwiY29tcGFueSI6IkFDTUUifQ.I1BvFoHe94AFf09O6tDbcSB8-jp8w6xZqmyHIwPeSdY";
let validation = Validation::new(Algorithm::RS256);
let claims = decode::<Claims>(token, &DecodingKey::from_secret(b"secret"), &validation);
claims.unwrap();
}

#[test]
#[wasm_bindgen_test]
#[should_panic(expected = "InvalidAlgorithm")]
Expand Down