diff --git a/pkgs/id/src/commands/serve.rs b/pkgs/id/src/commands/serve.rs index 49721c7562..13e4d417fd 100644 --- a/pkgs/id/src/commands/serve.rs +++ b/pkgs/id/src/commands/serve.rs @@ -1068,6 +1068,16 @@ pub fn start_server_config(path: &Path) -> Result { }) } +/// The pull policy the live event-sync gate serves under (#5119): the server +/// config's `[policy] pull_policy` when a config was loaded, else the code +/// default ([`crate::pull_policy::PullPolicy::Open`]), so a `serve` without +/// `--server-config` behaves exactly as before. +#[cfg(feature = "world")] +fn serve_pull_policy(started: Option<&StartedServerConfig>) -> crate::pull_policy::PullPolicy { + started.map_or(crate::pull_policy::PullPolicy::Open, |started| { + started.config.pull_policy() + }) +} /// The world protocol serve registers for a hub: the loaded operator /// config's #2247 budget policy when a server config was given, the @@ -1195,6 +1205,19 @@ pub async fn cmd_serve(opts: ServeOptions) -> Result<()> { } None => None, }; + // #5119: the pull policy the live event-sync gate uses, resolved once from + // the same startup config load. No config keeps the code default (open). + #[cfg(feature = "web")] + let event_sync_policy = { + #[cfg(feature = "world")] + { + serve_pull_policy(_started_server_config.as_ref()) + } + #[cfg(not(feature = "world"))] + { + crate::pull_policy::PullPolicy::Open + } + }; // #2145: resolve the web listener set once, before any startup work, // so a flag conflict or a malformed [web_bindings] line fails fast. let web_bindings = resolve_web_bindings( @@ -1499,7 +1522,10 @@ pub async fn cmd_serve(opts: ServeOptions) -> Result<()> { let router_builder = match &event_events { Some(events) => router_builder.accept( crate::event_store::sync::ALPN, - crate::event_sync::EventSyncProtocol::new(Arc::clone(events)), + crate::event_sync::EventSyncProtocol::with_gate( + Arc::clone(events), + crate::event_sync::gate(event_sync_policy), + ), ), None => router_builder, }; @@ -1540,9 +1566,22 @@ pub async fn cmd_serve(opts: ServeOptions) -> Result<()> { #[cfg(feature = "web")] let _event_sync_handle = event_events.clone().map(|events| { status!("event sync: /id-range-sync/1 enabled (the web event store)"); + // #5119: the live pull gate reads the operator config's + // `[policy] pull_policy`. Without a config the policy is the code + // default (open), exactly as before. The local event store is not a + // world and has no membership source, so `members` fails closed — + // every pull is refused — and says so here rather than silently + // serving open. + let policy = event_sync_policy; + if policy == crate::pull_policy::PullPolicy::Members { + status!( + "event sync: [policy] pull_policy=members — the local event store has no \ + membership source, so every pull is refused (fail closed)" + ); + } let endpoint = router.endpoint().clone(); let discovery = peer_discovery.clone(); - let gate = crate::event_sync::open_gate(); + let gate = crate::event_sync::gate(policy); tokio::spawn(async move { loop { tokio::time::sleep(crate::event_sync::SYNC_INTERVAL).await; @@ -3499,6 +3538,30 @@ mod tests { let _ = std::fs::remove_dir_all(&dir); } + #[cfg(feature = "world")] + #[test] + fn serve_pull_policy_defaults_to_open_without_a_config() { + assert_eq!( + serve_pull_policy(None), + crate::pull_policy::PullPolicy::Open + ); + } + + /// #5119: the live event-sync gate takes its policy from the loaded + /// config's `[policy] pull_policy`, not from the code default. + #[cfg(feature = "world")] + #[test] + fn serve_pull_policy_reads_the_configured_policy_table() { + let dir = serve_config_dir("pull-policy"); + let path = dir.join("server.toml"); + std::fs::write(&path, "[policy]\npull_policy = \"members\"\n").unwrap(); + let started = start_server_config(&path).unwrap(); + assert_eq!( + serve_pull_policy(Some(&started)), + crate::pull_policy::PullPolicy::Members + ); + let _ = std::fs::remove_dir_all(&dir); + } // #2247: the loaded [budgets] must reach the world protocol serve // registers, not just the parser. diff --git a/pkgs/id/src/event_sync.rs b/pkgs/id/src/event_sync.rs index 5651fc1d0b..a9d6b45748 100644 --- a/pkgs/id/src/event_sync.rs +++ b/pkgs/id/src/event_sync.rs @@ -21,10 +21,14 @@ //! reads. Sharing the instance (rather than opening the same directory twice) is also //! the only option: the store's database admits one opener at a time. //! -//! The pull gate is [`PullPolicy::Open`], the documented default for sync: `id serve` -//! does not read a per-world pull policy from config yet, and there is no world whose -//! membership this store belongs to. An open gate never consults membership, so the -//! [`WorldAccess`] it carries is a formality that is never read. +//! The pull gate is built from the operator server config's `[policy] pull_policy` +//! ([`crate::server_config::ServerConfig::pull_policy`]) when `id serve` has one, and +//! is [`PullPolicy::Open`] otherwise — so with no `--server-config` behaviour is +//! exactly as before. This store is not a world and has no membership source: under +//! [`PullPolicy::Members`] [`gate`] therefore refuses every pull (fail closed, see +//! [`NoWorlds`]) rather than silently admitting peers. That is the explicit safe +//! behaviour for the no-world case; a membership source can replace it when the +//! store grows one. use std::sync::Arc; use std::time::Duration; @@ -53,9 +57,11 @@ pub const SYNC_INTERVAL: Duration = Duration::from_secs(30); /// `EventSyncProtocol::accept`). pub(crate) const CLOSE_WAIT: Duration = Duration::from_secs(5); -/// Membership answers for a gate that never asks. [`PullPolicy::Open`] admits every -/// peer without reading membership (see the module docs), so these answers are never -/// used; the type exists only because a [`PullGate`] is built over a [`WorldAccess`]. +/// Membership answers for this store, which is not a world and has no membership +/// source. [`PullPolicy::Open`] admits every peer without reading membership, so +/// under the default policy these answers are never used. Under +/// [`PullPolicy::Members`] they are read, and every answer is `false`: the no-world +/// case fails closed and refuses every pull instead of silently serving open. #[derive(Debug)] struct NoWorlds; @@ -69,10 +75,22 @@ impl WorldAccess for NoWorlds { } } -/// The pull gate for the node's own event store: open, per the module docs. +/// The pull gate for the node's own event store under the configured `policy`. +/// +/// [`PullPolicy::Open`] (the default when there is no server config) admits every +/// peer. [`PullPolicy::Members`] reads [`NoWorlds`], which answers `false` for every +/// peer because this store has no membership source: the configured members policy +/// refuses every pull rather than silently serving open (see the module docs). +#[must_use] +pub fn gate(policy: PullPolicy) -> PullGate { + PullGate::new(policy, "local-events", Arc::new(NoWorlds)) +} + +/// The pull gate for the node's own event store when no policy is configured: open, +/// per the module docs. #[must_use] pub fn open_gate() -> PullGate { - PullGate::new(PullPolicy::Open, "local-events", Arc::new(NoWorlds)) + gate(PullPolicy::Open) } /// Serves range-sync sessions over the node's shared [`LocalEvents`] store. @@ -101,6 +119,13 @@ impl EventSyncProtocol { gate: open_gate(), } } + + /// Serves `events` under `gate` — the policy from the operator server config, + /// applied to accepted sessions as well as dialled ones (#5119). + #[must_use] + pub fn with_gate(events: Arc>, gate: PullGate) -> Self { + Self { events, gate } + } } impl ProtocolHandler for EventSyncProtocol { @@ -310,4 +335,53 @@ mod tests { client.close().await; server.close().await; } + + /// #5119: under a configured `members` pull policy this store has no + /// membership source, so the gate refuses every pull — fail closed, never + /// silently open. The accepting side refuses to send, and the dialling side + /// refuses too, so nothing crosses either way. + #[tokio::test(flavor = "multi_thread")] + async fn test_members_gate_refuses_every_pull_when_the_store_has_no_membership_source() { + let dir_a = tempfile::tempdir().unwrap(); + let dir_b = tempfile::tempdir().unwrap(); + let a = node(dir_a.path(), 5); + let b = node(dir_b.path(), 6); + let published = { + let mut a = a.lock().await; + a.publish("text/plain", b"never served", &[]).unwrap() + }; + + let server = endpoint().await; + let router = Router::builder(server.clone()) + .accept( + ALPN, + EventSyncProtocol::with_gate(b.clone(), gate(PullPolicy::Members)), + ) + .spawn(); + let client = endpoint().await; + // A refused pull ends the session with `PullRefused` (the sync layer's + // documented contract: a refused peer gets no event, and the session + // reports the refusal rather than a successful zero transfer - serve + // logs the error and carries on). Nothing may move in either direction. + let error = sync_with_peer( + &client, + loopback(&server), + &a, + &gate(PullPolicy::Members), + ) + .await + .expect_err("a members gate with no membership source completed a session"); + assert!( + error.to_string().contains("pull policy refuses"), + "the session failed for a reason other than the gate: {error:#}" + ); + assert!( + b.lock().await.history(published.id).is_err(), + "B received an event under a refusing members gate" + ); + + router.shutdown().await.unwrap(); + client.close().await; + server.close().await; + } } diff --git a/pkgs/id/src/pull_policy.rs b/pkgs/id/src/pull_policy.rs index 44bb5e02cc..75f02662ae 100644 --- a/pkgs/id/src/pull_policy.rs +++ b/pkgs/id/src/pull_policy.rs @@ -20,7 +20,9 @@ //! //! This is a config setting, not a protocol rule. The policy belongs with the world's policy. The //! server config's `[policy]` table parses it ([`crate::server_config::ServerConfig::pull_policy`]), -//! but `id serve` does not read it yet, and no session builds a gate from config. Blobs are not +//! and `id serve` builds its live event-sync gate from that value +//! ([`crate::event_sync::gate`]); the node's own event store is not a world and has no membership +//! source, so `members` there refuses every pull rather than serving open. Blobs are not //! governed here. The blob gate ([`crate::blob_gate`]) still requires membership to fetch, whatever //! this policy says. Authorized pull is advisory: it stops an honest server from serving a //! non-member. It does not stop re-serving, recall copies, or hide metadata.