Skip to content

Commit

Permalink
Fix potential panic with bad exp set
Browse files Browse the repository at this point in the history
Closes #388
All credits to @0xd-0
  • Loading branch information
Keats committed May 27, 2024
1 parent afbb44e commit fa4ba07
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ pub(crate) fn validate(claims: ClaimsForValidation, options: &Validation) -> Res
if options.validate_exp || options.validate_nbf {
let now = get_current_timestamp();

if matches!(claims.exp, TryParse::Parsed(exp) if exp < options.reject_tokens_expiring_in_less_than)
{
return Err(new_error(ErrorKind::InvalidToken));
}

if matches!(claims.exp, TryParse::Parsed(exp) if options.validate_exp
&& exp - options.reject_tokens_expiring_in_less_than < now - options.leeway )
{
Expand Down Expand Up @@ -817,4 +822,17 @@ mod tests {
let res = validate(deserialize_claims(&claims), &validation);
assert!(res.is_ok());
}

// https://github.com/Keats/jsonwebtoken/issues/388
#[test]
#[wasm_bindgen_test]
fn doesnt_panic_with_leeway_overflow() {
let claims = json!({ "exp": 1 });

let mut validation = Validation::new(Algorithm::HS256);
validation.reject_tokens_expiring_in_less_than = 100;

let res = validate(deserialize_claims(&claims), &validation);
assert!(res.is_err());
}
}

0 comments on commit fa4ba07

Please sign in to comment.