Skip to content

Commit 0c6011d

Browse files
authored
Fix potential panic with bad exp set (#390)
Closes #388 All credits to @0xd-0
1 parent 4d5e43f commit 0c6011d

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
@@ -275,6 +275,11 @@ pub(crate) fn validate(claims: ClaimsForValidation, options: &Validation) -> Res
275275
if options.validate_exp || options.validate_nbf {
276276
let now = get_current_timestamp();
277277

278+
if matches!(claims.exp, TryParse::Parsed(exp) if exp < options.reject_tokens_expiring_in_less_than)
279+
{
280+
return Err(new_error(ErrorKind::InvalidToken));
281+
}
282+
278283
if matches!(claims.exp, TryParse::Parsed(exp) if options.validate_exp
279284
&& exp - options.reject_tokens_expiring_in_less_than < now - options.leeway )
280285
{
@@ -822,4 +827,17 @@ mod tests {
822827
let res = validate(deserialize_claims(&claims), &validation);
823828
assert!(res.is_ok());
824829
}
830+
831+
// https://github.com/Keats/jsonwebtoken/issues/388
832+
#[test]
833+
#[wasm_bindgen_test]
834+
fn doesnt_panic_with_leeway_overflow() {
835+
let claims = json!({ "exp": 1 });
836+
837+
let mut validation = Validation::new(Algorithm::HS256);
838+
validation.reject_tokens_expiring_in_less_than = 100;
839+
840+
let res = validate(deserialize_claims(&claims), &validation);
841+
assert!(res.is_err());
842+
}
825843
}

0 commit comments

Comments
 (0)