Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion pkgs/id/src/web/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ const MAX_NAME_LENGTH: usize = 48;
/// Maximum token age before it's considered expired (30 days in seconds).
const TOKEN_MAX_AGE_SECS: u64 = 30 * 24 * 60 * 60;

/// Maximum tolerated clock skew for a token issued in the future (5 minutes).
const TOKEN_FUTURE_SKEW_SECS: u64 = 5 * 60;

/// Default admission bound on registered web identities (#1766).
///
/// Registration used to grow the identity store — the in-memory maps
Expand Down Expand Up @@ -523,8 +526,11 @@ impl IdentityStore {
// Deserialize payload
let payload: TokenPayload = serde_json::from_slice(payload_bytes).ok()?;

// Reject expired tokens (older than TOKEN_MAX_AGE_SECS)
// Reject tokens dated implausibly far in the future, then apply the age cap.
let now = unix_timestamp();
if payload.created_at > now.saturating_add(TOKEN_FUTURE_SKEW_SECS) {
return None;
}
if now.saturating_sub(payload.created_at) > TOKEN_MAX_AGE_SECS {
return None;
}
Expand Down Expand Up @@ -1393,6 +1399,30 @@ mod tests {
assert!(store.verify_token_client_id(&expired_token).is_none());
}

#[tokio::test]
async fn test_future_dated_token_within_and_beyond_skew() {
let store = IdentityStore::new_ephemeral();
let (_, identity) = store.register(None).await.unwrap();
let now = unix_timestamp();

let within_skew = store
.create_token(
&identity.client_id,
now.saturating_add(TOKEN_FUTURE_SKEW_SECS - 60),
)
.unwrap();
assert!(store.verify_token(&within_skew).await.is_some());

let beyond_skew = store
.create_token(
&identity.client_id,
now.saturating_add(TOKEN_FUTURE_SKEW_SECS + 60),
)
.unwrap();
assert!(store.verify_token(&beyond_skew).await.is_none());
assert!(store.verify_token_client_id(&beyond_skew).is_none());
}

/// A token with the given payload and 64 signature bytes, in the wire format.
fn token_of(payload: &[u8], signature: &[u8; 64]) -> String {
let mut bytes = u32::try_from(payload.len()).unwrap().to_le_bytes().to_vec();
Expand Down
Loading