Diffinery

homeflare/celldpublic

39 lines · 1.9 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 node name or peer identity (`source`/`target`) well-formed? Non-empty,
15/// at most 128 bytes, and only ASCII alphanumerics plus `_ - .`. The special
16/// path components `.` and `..` are not names. The charset is the fence — admit
17/// `/` and an identity could carry a path segment.
18pub fn valid_identity(id: &str) -> bool {
19 !id.is_empty()
20 && id.len() <= 128
21 && !matches!(id, "." | "..")
22 && id
23 .bytes()
24 .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'-' | b'.'))
25}
26
27/// Is a request timestamp within the accepted clock window of `now`? The window
28/// is TWO-sided (`abs_diff`): a check that accepted arbitrarily old timestamps
29/// would let a captured signature replay forever.
30pub fn within_clock_window(now: Ms, timestamp: Ms, window: Ms) -> bool {
31 now.abs_diff(timestamp) <= window
32}
33
34/// May a replay-cache nonce be evicted — has its retention window elapsed?
35/// Retention MUST be at least the clock window, or a nonce is forgotten while a
36/// signature bearing it is still within the clock window and would be accepted.
37pub fn replay_entry_expired(now: Ms, seen_ms: Ms, retention: Ms) -> bool {
38 now.saturating_sub(seen_ms) > retention
39}