Skip to content

Commit a55b45c

Browse files
authored
Reject tokens when claims has an aud, none expected (#332)
* Reject tokens when claims has an aud, none expected From the RFC: > Each principal intended to process the JWT MUST > identify itself with a value in the audience claim. If the principal > processing the claim does not identify itself with a value in the > "aud" claim when this claim is present, then the JWT MUST be >rejected. Closes #329 * Note the RFC section we're complying with by rejecting None aud.
1 parent b7599eb commit a55b45c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/validation.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,14 @@ pub(crate) fn validate(claims: ClaimsForValidation, options: &Validation) -> Res
263263
}
264264

265265
match (claims.aud, options.aud.as_ref()) {
266+
// Each principal intended to process the JWT MUST
267+
// identify itself with a value in the audience claim. If the principal
268+
// processing the claim does not identify itself with a value in the
269+
// "aud" claim when this claim is present, then the JWT MUST be
270+
// rejected.
271+
(TryParse::Parsed(_), None) => {
272+
return Err(new_error(ErrorKind::InvalidAudience));
273+
}
266274
(TryParse::Parsed(Audience::Single(aud)), Some(correct_aud)) => {
267275
if !correct_aud.contains(&*aud) {
268276
return Err(new_error(ErrorKind::InvalidAudience));
@@ -632,6 +640,22 @@ mod tests {
632640
};
633641
}
634642

643+
#[test]
644+
fn aud_none_fails() {
645+
let claims = json!({"aud": ["Everyone"]});
646+
let mut validation = Validation::new(Algorithm::HS256);
647+
validation.validate_exp = false;
648+
validation.required_spec_claims = HashSet::new();
649+
validation.aud = None;
650+
let res = validate(deserialize_claims(&claims), &validation);
651+
assert!(res.is_err());
652+
653+
match res.unwrap_err().kind() {
654+
ErrorKind::InvalidAudience => (),
655+
_ => unreachable!(),
656+
};
657+
}
658+
635659
#[test]
636660
fn aud_missing_fails() {
637661
let claims = json!({});

0 commit comments

Comments
 (0)