Skip to content

Commit fa4ba07

Browse files
committed
Fix potential panic with bad exp set
Closes #388 All credits to @0xd-0
1 parent afbb44e commit fa4ba07

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/validation.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ pub(crate) fn validate(claims: ClaimsForValidation, options: &Validation) -> Res
271271
if options.validate_exp || options.validate_nbf {
272272
let now = get_current_timestamp();
273273

274+
if matches!(claims.exp, TryParse::Parsed(exp) if exp < options.reject_tokens_expiring_in_less_than)
275+
{
276+
return Err(new_error(ErrorKind::InvalidToken));
277+
}
278+
274279
if matches!(claims.exp, TryParse::Parsed(exp) if options.validate_exp
275280
&& exp - options.reject_tokens_expiring_in_less_than < now - options.leeway )
276281
{
@@ -817,4 +822,17 @@ mod tests {
817822
let res = validate(deserialize_claims(&claims), &validation);
818823
assert!(res.is_ok());
819824
}
825+
826+
// https://github.com/Keats/jsonwebtoken/issues/388
827+
#[test]
828+
#[wasm_bindgen_test]
829+
fn doesnt_panic_with_leeway_overflow() {
830+
let claims = json!({ "exp": 1 });
831+
832+
let mut validation = Validation::new(Algorithm::HS256);
833+
validation.reject_tokens_expiring_in_less_than = 100;
834+
835+
let res = validate(deserialize_claims(&claims), &validation);
836+
assert!(res.is_err());
837+
}
820838
}

0 commit comments

Comments
 (0)