-
Notifications
You must be signed in to change notification settings - Fork 16
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
bug: Do not double-check VAPID aud #772
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
use std::borrow::Cow; | ||
use std::error::Error; | ||
use std::str::FromStr; | ||
|
||
use actix_web::{dev::Payload, web::Data, FromRequest, HttpRequest}; | ||
use autopush_common::{ | ||
|
@@ -12,7 +11,6 @@ use cadence::{CountedExt, StatsdClient}; | |
use futures::{future::LocalBoxFuture, FutureExt}; | ||
use jsonwebtoken::{Algorithm, DecodingKey, Validation}; | ||
use openssl::hash::MessageDigest; | ||
use url::Url; | ||
use uuid::Uuid; | ||
|
||
use crate::error::{ApiError, ApiErrorKind, ApiResult}; | ||
|
@@ -293,8 +291,9 @@ fn validate_vapid_jwt( | |
|
||
let public_key = decode_public_key(public_key)?; | ||
let mut validation = Validation::new(Algorithm::ES256); | ||
let audience: Vec<&str> = settings.vapid_aud.iter().map(|s| s.as_str()).collect(); | ||
validation.set_audience(&audience); | ||
// Set the audiences we allow. This obsoletes the need to manually match | ||
// against values later. | ||
validation.set_audience(&[settings.endpoint_url().origin().ascii_serialization()]); | ||
validation.set_required_spec_claims(&["exp", "aud", "sub"]); | ||
|
||
let token_data = match jsonwebtoken::decode::<VapidClaims>( | ||
|
@@ -389,27 +388,6 @@ fn validate_vapid_jwt( | |
return Err(VapidError::FutureExpirationToken.into()); | ||
} | ||
|
||
let aud = match Url::from_str(&token_data.claims.aud) { | ||
Ok(v) => v, | ||
Err(_) => { | ||
error!("Bad Aud: Invalid audience {:?}", &token_data.claims.aud); | ||
metrics.clone().incr("notification.auth.bad_vapid.aud"); | ||
return Err(VapidError::InvalidAudience.into()); | ||
} | ||
}; | ||
|
||
let domain = &settings.endpoint_url(); | ||
|
||
if domain != &aud { | ||
info!( | ||
"Bad Aud: I am <{:?}>, asked for <{:?}> ", | ||
domain.as_str(), | ||
token_data.claims.aud | ||
); | ||
metrics.clone().incr("notification.auth.bad_vapid.domain"); | ||
return Err(VapidError::InvalidAudience.into()); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
|
@@ -512,6 +490,23 @@ pub mod tests { | |
)); | ||
} | ||
|
||
#[test] | ||
fn vapid_aud_valid_for_alternate_host() { | ||
let domain = "https://example.org"; | ||
let test_settings = Settings { | ||
endpoint_url: domain.to_owned(), | ||
..Default::default() | ||
}; | ||
let header = make_vapid( | ||
"mailto:[email protected]", | ||
domain, | ||
VapidClaims::default_exp() - 100, | ||
PUB_KEY.to_owned(), | ||
); | ||
let result = validate_vapid_jwt(&header, &test_settings, &Metrics::noop()); | ||
assert!(result.is_ok()); | ||
} | ||
|
||
#[test] | ||
fn vapid_exp_is_string() { | ||
#[derive(Debug, Deserialize, Serialize)] | ||
|
@@ -523,7 +518,7 @@ pub mod tests { | |
|
||
let domain = "https://push.services.mozilla.org"; | ||
let test_settings = Settings { | ||
endpoint_url: "domain".to_owned(), | ||
endpoint_url: domain.to_owned(), | ||
..Default::default() | ||
}; | ||
let jwk_header = jsonwebtoken::Header::new(jsonwebtoken::Algorithm::ES256); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I gather jsonwebtoken will now do an equivalent check against our
settings.vapid_aud
(viavalidation.set_audience()
) -- however I don't see us assigning this newAUTOEND__VAPID_AUD
setting anywhere, which I think means it's defaulting to incorrect values (notsettings.endpoint_url()
).Do we even need
settings.vapid_aud
-- might we always set something along the lines ofvalidation.set_audience(&[settings.endpoint_url()])
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and prod's basically
endpoint_url()
for reference here