Diffinery

homeflare/celldpublic

37 lines · 1.8 KBRaw History
1// Copyright 2026 Deno Land Inc. Apache-2.0 license.
2
3//! Peer-authentication predicates, reified sans-IO. The signed-request checks in
4//! `peer_auth::verify` — a well-formed identity, a timestamp within the clock
5//! window, and replay-cache retention — are pure predicates over scalars, and
6//! each is a SECURITY fence. A widened identity charset lets a `target` smuggle
7//! a path segment past the WrongTarget check; a one-sided clock window replays
8//! an old signature forever; retention below the clock window forgets a nonce
9//! while a signature bearing it is still acceptable, reopening the replay hole
10//! the cache exists to close. Signing/MAC stays in production; the gates move.
11
12pub type Ms = u64;
13
14/// Is a peer identity (`source`/`target`) well-formed? Non-empty, at most 128
15/// bytes, and only ASCII alphanumerics plus `_ - .`. The charset is the fence —
16/// admit `/` and a `target` could carry a path segment.
17pub fn valid_identity(id: &str) -> bool {
18 !id.is_empty()
19 && id.len() <= 128
20 && id
21 .bytes()
22 .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'-' | b'.'))
23}
24
25/// Is a request timestamp within the accepted clock window of `now`? The window
26/// is TWO-sided (`abs_diff`): a check that accepted arbitrarily old timestamps
27/// would let a captured signature replay forever.
28pub fn within_clock_window(now: Ms, timestamp: Ms, window: Ms) -> bool {
29 now.abs_diff(timestamp) <= window
30}
31
32/// May a replay-cache nonce be evicted — has its retention window elapsed?
33/// Retention MUST be at least the clock window, or a nonce is forgotten while a
34/// signature bearing it is still within the clock window and would be accepted.
35pub fn replay_entry_expired(now: Ms, seen_ms: Ms, retention: Ms) -> bool {
36 now.saturating_sub(seen_ms) > retention
37}