From af087a816f870ea943c61624d496b1d5e4d090b9 Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 16 Nov 2026 20:52:59 +0000 Subject: api: in a swarm holding a store, a node takes no token another issued At its API, a node of a swarm holding a store takes no token another node issued (`docs/store.md`, Members): whoever its operator lets in may use it, and its operator lets in nobody through another node. A token every node of a swarm takes would let whoever another operator let in in here too. `token::issued_here` refuses such a token (`ElsewhereIssued`), behind the `federation` feature. --- src/access/token.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'src/access/token.rs') diff --git a/src/access/token.rs b/src/access/token.rs index 2aefcd7..54dd7f8 100644 --- a/src/access/token.rs +++ b/src/access/token.rs @@ -98,6 +98,9 @@ pub(crate) enum Refused { Revoked, #[error("its session was ended (logged out of)")] Ended, + #[error("issued by another node, which a swarm holding a store takes none of")] + #[cfg(feature = "federation")] + ElsewhereIssued, } /// The claims of `token` if this node takes it: well formed, for `swarm`, signed by a @@ -147,6 +150,21 @@ pub(crate) fn verify( Ok(claims) } +/// `claims`, of a token `verify` took, if this node, `local`, takes it: in a swarm holding a +/// store, only one it issued itself (`docs/store.md`, Members), as a node there is +/// whoever its operator lets in, and lets in nobody's but its own. +#[cfg(feature = "federation")] +pub(crate) fn issued_here( + doc: &impl ReadDoc, + local: &str, + claims: Claims, +) -> Result { + match super::policy::of(doc) { + super::policy::Policy::Store(_) if claims.issuer != local => Err(Refused::ElsewhereIssued), + _ => Ok(claims), + } +} + /// Whether a token of `claims`, taken once (`verify`), still is by what `doc` says now: /// its issuer a member and not banned, its user registered, enabled, its tokens not /// revoked since and its session not ended. What a stream opened with it is judged by @@ -270,6 +288,31 @@ mod tests { serde_json::from_slice(&BASE64URL.decode(payload).unwrap()).unwrap() } + /// In a swarm holding a store, a node takes only the tokens it issued. + #[cfg(feature = "federation")] + #[test] + fn a_store_node_takes_its_own_tokens_only() { + let key = NodeKey::from_seed([1; 32]); + let other = NodeKey::from_seed([2; 32]); + let mut doc = crate::document::events::started("federation", b"a root").unwrap(); + member(&mut doc, &key); + member(&mut doc, &other); + let token = issue(&key, &claims(&key)); + let claims = verify(&doc, &token, "s", 1_500).unwrap(); + assert_eq!( + issued_here(&doc, &key.id(), claims.clone()), + Ok(claims.clone()) + ); + assert_eq!( + issued_here(&doc, &other.id(), claims.clone()), + Err(Refused::ElsewhereIssued) + ); + assert_eq!( + issued_here(&Document::new(), &other.id(), claims.clone()), + Ok(claims) + ); + } + #[test] fn only_members_issue_and_nobody_forges() { let key = NodeKey::from_seed([1; 32]); -- cgit v1.3.1