-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug: Address insecure sub header fetch. (#803)
When insecurely getting the `sub` for tracking and errors, we were not properly decoding. This was resulting in a VAPID error being generated. This should not impact processing, but was generating a lot of logging messages. * FCM would reject TTLS > that 4 weeks, which is less than our 30 day max. Added a specific filter for that. * fixed some spelling mistakes. Closes: [SYNC-4514](https://mozilla-hub.atlassian.net/browse/SYNC-4514)
- Loading branch information
Showing
7 changed files
with
69 additions
and
46 deletions.
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 |
---|---|---|
|
@@ -87,7 +87,7 @@ impl FromRequest for Subscription { | |
if let Some(ref header) = vapid { | ||
let sub = header | ||
.vapid | ||
.sub() | ||
.insecure_sub() | ||
.map_err(|e: VapidError| { | ||
// Capture the type of error and add it to metrics. | ||
let mut tags = Tags::default(); | ||
|
@@ -332,6 +332,9 @@ fn validate_vapid_jwt( | |
jsonwebtoken::errors::ErrorKind::InvalidAudience => { | ||
return Err(VapidError::InvalidAudience.into()); | ||
} | ||
jsonwebtoken::errors::ErrorKind::MissingRequiredClaim(e) => { | ||
return Err(VapidError::InvalidVapid(format!("Missing required {}", e)).into()); | ||
} | ||
_ => { | ||
// Attempt to match up the majority of ErrorKind variants. | ||
// The third-party errors all defer to the source, so we can | ||
|
@@ -423,8 +426,8 @@ pub mod tests { | |
let enc_key = jsonwebtoken::EncodingKey::from_ec_der(&PRIV_KEY); | ||
let claims = VapidClaims { | ||
exp, | ||
aud: aud.to_string(), | ||
sub: sub.to_string(), | ||
aud: Some(aud.to_string()), | ||
sub: Some(sub.to_string()), | ||
}; | ||
let token = jsonwebtoken::encode(&jwk_header, &claims, &enc_key).unwrap(); | ||
|
||
|
@@ -566,8 +569,8 @@ pub mod tests { | |
let enc_key = jsonwebtoken::EncodingKey::from_ec_der(&PRIV_KEY); | ||
let claims = VapidClaims { | ||
exp: VapidClaims::default_exp() - 100, | ||
aud: domain.to_owned(), | ||
sub: "mailto:[email protected]".to_owned(), | ||
aud: Some(domain.to_owned()), | ||
sub: Some("mailto:[email protected]".to_owned()), | ||
}; | ||
let token = jsonwebtoken::encode(&jwk_header, &claims, &enc_key).unwrap(); | ||
// try standard form with padding | ||
|
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 |
---|---|---|
|
@@ -3,7 +3,6 @@ use std::fmt; | |
|
||
use base64::Engine; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_json::Value; | ||
use thiserror::Error; | ||
|
||
use crate::headers::util::split_key_value; | ||
|
@@ -15,23 +14,23 @@ pub const ALLOWED_SCHEMES: [&str; 3] = ["bearer", "webpush", "vapid"]; | |
The Assertion block for the VAPID header. | ||
Please note: We require the `sub` claim in addition to the `exp` and `aud`. | ||
See [HTTP Endpoints for Notficiations::Lexicon::{vapid_key}](https://mozilla-services.github.io/autopush-rs/http.html#lexicon-1) | ||
See [HTTP Endpoints for Notifications::Lexicon::{vapid_key}](https://mozilla-services.github.io/autopush-rs/http.html#lexicon-1) | ||
for details. | ||
*/ | ||
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)] | ||
pub struct VapidClaims { | ||
pub exp: u64, | ||
pub aud: String, | ||
pub sub: String, | ||
pub aud: Option<String>, | ||
pub sub: Option<String>, | ||
} | ||
|
||
impl Default for VapidClaims { | ||
fn default() -> Self { | ||
Self { | ||
exp: VapidClaims::default_exp(), | ||
aud: "No audience".to_owned(), | ||
sub: "No sub".to_owned(), | ||
aud: None, | ||
sub: None, | ||
} | ||
} | ||
} | ||
|
@@ -143,25 +142,27 @@ impl VapidHeader { | |
} | ||
} | ||
|
||
pub fn sub(&self) -> Result<String, VapidError> { | ||
let data: HashMap<String, Value> = serde_json::from_str(&self.token).map_err(|e| { | ||
warn!("🔐 Vapid: {:?}", e); | ||
VapidError::SubInvalid | ||
/// Return the claimed `sub` after doing some minimal checks for validity. | ||
/// WARNING: THIS FUNCTION DOES NOT VALIDATE THE VAPID HEADER AND SHOULD | ||
/// ONLY BE USED FOR LOGGING AND METRIC REPORTING FUNCTIONS. | ||
/// Proper validation should be done by [crate::extractors::subscription::validate_vapid_jwt()] | ||
pub fn insecure_sub(&self) -> Result<String, VapidError> { | ||
// This parses the VAPID header string | ||
let data = VapidClaims::try_from(self.clone()).inspect_err(|e| { | ||
warn!("🔐 Vapid: {:?} {:?}", e, &self.token); | ||
})?; | ||
|
||
if let Some(sub_candiate) = data.get("sub") { | ||
if let Some(sub) = sub_candiate.as_str() { | ||
if !sub.starts_with("mailto:") || !sub.starts_with("https://") { | ||
info!("🔐 Vapid: Bad Format {:?}", sub); | ||
return Err(VapidError::SubBadFormat); | ||
} | ||
if sub.is_empty() { | ||
info!("🔐 Empty Vapid sub"); | ||
return Err(VapidError::SubEmpty); | ||
} | ||
info!("🔐 Vapid: sub: {:?}", sub); | ||
return Ok(sub.to_owned()); | ||
if let Some(sub) = data.sub { | ||
if !sub.starts_with("mailto:") && !sub.starts_with("https://") { | ||
info!("🔐 Vapid: Bad Format {:?}", sub); | ||
return Err(VapidError::SubBadFormat); | ||
} | ||
if sub.is_empty() { | ||
info!("🔐 Empty Vapid sub"); | ||
return Err(VapidError::SubEmpty); | ||
} | ||
info!("🔐 Vapid: sub: {:?}", sub); | ||
return Ok(sub.to_owned()); | ||
} | ||
Err(VapidError::SubMissing) | ||
} | ||
|
@@ -254,9 +255,16 @@ mod tests { | |
returned_header.unwrap().claims(), | ||
Ok(VapidClaims { | ||
exp: 1713564872, | ||
aud: "https://push.services.mozilla.com".to_string(), | ||
sub: "mailto:[email protected]".to_string() | ||
aud: Some("https://push.services.mozilla.com".to_owned()), | ||
sub: Some("mailto:[email protected]".to_owned()) | ||
}) | ||
); | ||
|
||
// Ensure the parent `.sub()` returns a valid value. | ||
let returned_header = VapidHeader::parse(VALID_HEADER); | ||
assert_eq!( | ||
returned_header.unwrap().insecure_sub(), | ||
Ok("mailto:[email protected]".to_owned()) | ||
) | ||
} | ||
} |
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
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
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
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
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