-
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: Make VAPID sub assertion optional (#831)
Only reports `sub` if it is present and non-empty. Closes: [SYNC-4564](https://mozilla-hub.atlassian.net/browse/SYNC-4564) [SYNC-4564]: https://mozilla-hub.atlassian.net/browse/SYNC-4564?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
- Loading branch information
Showing
7 changed files
with
49 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,26 +149,23 @@ impl VapidHeader { | |
/// 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> { | ||
pub fn insecure_sub(&self) -> Result<Option<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) = 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()); | ||
let Some(sub) = data.sub else { return Ok(None) }; | ||
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); | ||
} | ||
|
||
Err(VapidError::SubMissing) | ||
info!("🔐 Vapid: sub: {sub}"); | ||
Ok(Some(sub)) | ||
} | ||
|
||
pub fn claims(&self) -> Result<VapidClaims, VapidError> { | ||
|
@@ -268,7 +265,7 @@ mod tests { | |
let returned_header = VapidHeader::parse(VALID_HEADER); | ||
assert_eq!( | ||
returned_header.unwrap().insecure_sub(), | ||
Ok("mailto:[email protected]".to_owned()) | ||
Ok(Some("mailto:[email protected]".to_owned())) | ||
) | ||
} | ||
|
||
|
@@ -277,7 +274,7 @@ mod tests { | |
let header = VapidHeader::parse(VALID_HEADER).unwrap(); | ||
assert_eq!( | ||
header.insecure_sub().unwrap(), | ||
"mailto:[email protected]".to_string() | ||
Some("mailto:[email protected]".to_string()) | ||
); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1378,7 +1378,7 @@ async def test_with_key(test_client: AsyncPushTestClient) -> None: | |
claims = { | ||
"aud": f"http://127.0.0.1:{ENDPOINT_PORT}", | ||
"exp": int(time.time()) + 86400, | ||
"sub": "[email protected]", | ||
"sub": "mailto:[email protected]", | ||
} | ||
vapid = _get_vapid(private_key, claims) | ||
pk_hex = vapid["crypto-key"] | ||
|
@@ -1397,6 +1397,24 @@ async def test_with_key(test_client: AsyncPushTestClient) -> None: | |
await test_client.send_notification(vapid=vapid, status=401) | ||
|
||
|
||
async def test_empty_vapid(test_client: AsyncPushTestClient) -> None: | ||
"""Test with a minimal VAPID assertion set""" | ||
private_key = ecdsa.SigningKey.generate(curve=ecdsa.NIST256p) | ||
claims = { | ||
"aud": f"http://127.0.0.1:{ENDPOINT_PORT}", | ||
"exp": int(time.time()) + 86400, | ||
} | ||
vapid = _get_vapid(private_key, claims) | ||
pk_hex = vapid["crypto-key"] | ||
chid = str(uuid.uuid4()) | ||
await test_client.connect() | ||
await test_client.hello() | ||
await test_client.register(channel_id=chid, key=pk_hex) | ||
|
||
# Send an update with a properly formatted key. | ||
await test_client.send_notification(vapid=vapid) | ||
|
||
|
||
async def test_with_bad_key(test_client: AsyncPushTestClient): | ||
"""Test that a message registration request with bad VAPID public key is rejected.""" | ||
chid: str = str(uuid.uuid4()) | ||
|