Skip to content
Merged
Show file tree
Hide file tree
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
54 changes: 52 additions & 2 deletions pkgs/id/src/abuse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@

use std::collections::{HashMap, HashSet};
use std::net::IpAddr;
use std::sync::Arc;

use crate::event_store::BlockList;

/// A remote endpoint identity: the 32 bytes of its verifying key,
/// the same bytes the event store uses as an author key.
Expand Down Expand Up @@ -673,6 +676,11 @@ pub struct AbuseEvaluator {
/// exactly: token buckets only, no budget scopes charged.
budgets: Option<BudgetPolicy>,
budget_state: HashMap<BudgetKey, Vec<WindowCounter>>,
/// The operator block list (#2245), when the evaluator's owner
/// attached one: [`AbuseEvaluator::open_connection`] refuses a
/// blocked endpoint before any bucket or cap is consulted. `None`
/// keeps the pre-#2245 behaviour exactly.
blocks: Option<Arc<dyn BlockList>>,
}

impl AbuseEvaluator {
Expand All @@ -686,9 +694,23 @@ impl AbuseEvaluator {
registry: CounterRegistry::new(),
budgets: None,
budget_state: HashMap::new(),
blocks: None,
}
}

/// Like [`AbuseEvaluator::new`], with the operator block list
/// (#2245) attached: a new connection from an endpoint blocked by
/// key is refused with [`Refusal::blocked_by_operator`]. The list
/// is consulted live on every open, so a block or an unblock lands
/// on the next connection. Frame and byte checks are unchanged —
/// a block refuses new sessions; it does not retroactively charge
/// or drop a session that is already open.
#[must_use]
pub fn with_block_list(mut self, blocks: Arc<dyn BlockList>) -> Self {
self.blocks = Some(blocks);
self
}

/// Like [`AbuseEvaluator::new`], with the #2247 rolling-window
/// budgets configured: charges through
/// [`AbuseEvaluator::check_budget`] and
Expand Down Expand Up @@ -951,13 +973,23 @@ impl AbuseEvaluator {
///
/// # Errors
///
/// Returns a [`RefusalReason::RateLimited`] refusal when the
/// endpoint already holds the policy's maximum connections.
/// Returns a [`RefusalReason::BlockedByOperator`] refusal when the
/// operator block list attached with
/// [`AbuseEvaluator::with_block_list`] blocks `endpoint`, and a
/// [`RefusalReason::RateLimited`] refusal when the endpoint already
/// holds the policy's maximum connections.
pub fn open_connection(
&mut self,
endpoint: EndpointId,
now_ms: u64,
) -> Result<ConnectionId, Refusal> {
if self
.blocks
.as_ref()
.is_some_and(|blocks| blocks.peer_blocked(&endpoint))
{
return Err(Refusal::blocked_by_operator());
}
let cap = self.policy.max_connections_per_peer;
// Ids are unique and never reused; a refused open may skip
// an id, which is harmless.
Expand Down Expand Up @@ -1200,6 +1232,24 @@ mod tests {
assert!(eval.check_frame(ep, true, 0).is_err());
}

#[test]
fn a_blocked_endpoint_is_refused_a_new_connection() {
// #2245 session admission: with a block list attached, a blocked
// endpoint's new connection is refused as an operator block —
// permanent, no retry offered, and no connection registered.
let blocks = crate::pull_policy::testing::stub_blocks(&[endpoint(7)], &[]);
let mut eval = evaluator().with_block_list(blocks);
let refused = eval
.open_connection(endpoint(7), 0)
.expect_err("a blocked endpoint was admitted");
assert_eq!(refused, Refusal::blocked_by_operator());
assert_eq!(refused.reason, RefusalReason::BlockedByOperator);
assert_eq!(refused.retry_after_ms, None);
assert_eq!(eval.active_connection_count(&endpoint(7)), 0);
eval.open_connection(endpoint(8), 0)
.expect("an unblocked endpoint still connects");
}

#[test]
fn connection_cap_is_per_peer() {
let mut eval = evaluator();
Expand Down
101 changes: 95 additions & 6 deletions pkgs/id/src/blob_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use iroh_blobs::{
};

use crate::access::AccessPolicy;
use crate::event_store::BlockList;
use crate::groups::{Role, worlds::Worlds};

/// The two questions the gate asks of the world graph.
Expand Down Expand Up @@ -98,6 +99,29 @@ pub fn decide(
hash: &Hash,
request: Request,
) -> bool {
decide_with_blocks(access, policy, None, requester, hash, request)
}

/// [`decide`], with the operator block list composed in (#2245): a
/// requester blocked by endpoint key is refused fetch and observe alike,
/// member or not. The seam's shape stays the crate's bool verdict (the
/// provider's `AbortReason::Permission` is what a refusal becomes in
/// [`blob_events_with_blocks`]). Pushes are not decided here: a block
/// changes serving only, never what the store ingests.
///
/// `blocks` is `None` for a gate with no list attached — exactly
/// [`decide`]'s behaviour.
pub fn decide_with_blocks(
access: &impl WorldAccess,
policy: &BlobPolicy,
blocks: Option<&dyn BlockList>,
requester: &EndpointId,
hash: &Hash,
request: Request,
) -> bool {
if blocks.is_some_and(|blocks| blocks.peer_blocked(requester.as_bytes())) {
return false;
}
access.worlds_referencing(hash).iter().any(|world| {
access.is_current_member(world, requester)
&& match request {
Expand Down Expand Up @@ -167,6 +191,21 @@ pub fn blob_events<A: WorldAccess>(
access: Arc<A>,
policy: BlobPolicy,
writes: AccessPolicy,
) -> EventSender {
blob_events_with_blocks(access, policy, writes, None)
}

/// [`blob_events`], with the operator block list composed into every
/// serving verdict (#2245): a blocked endpoint is refused `get`,
/// `get_many`, and `observe` through the same `AbortReason::Permission`
/// as a non-member, so a refusal still does not reveal which rule made
/// it. Push verdicts are unchanged — writes are ingest, and a block
/// never rejects what a peer sends, only what it is served.
pub fn blob_events_with_blocks<A: WorldAccess>(
access: Arc<A>,
policy: BlobPolicy,
writes: AccessPolicy,
blocks: Option<Arc<dyn BlockList>>,
) -> EventSender {
let (tx, mut rx) = tokio::sync::mpsc::channel::<ProviderMessage>(64);
let mask = EventMask {
Expand All @@ -191,22 +230,42 @@ pub fn blob_events<A: WorldAccess>(
}
ProviderMessage::GetRequestReceived(m) => {
let verdict = admit(peers.get(&m.connection_id), |id| {
decide(&*access, &policy, id, &m.request.hash, Request::Fetch)
decide_with_blocks(
&*access,
&policy,
blocks.as_deref(),
id,
&m.request.hash,
Request::Fetch,
)
});
let _ = m.tx.send(verdict).await;
}
ProviderMessage::GetManyRequestReceived(m) => {
let verdict = admit(peers.get(&m.connection_id), |id| {
m.request
.hashes
.iter()
.all(|hash| decide(&*access, &policy, id, hash, Request::Fetch))
m.request.hashes.iter().all(|hash| {
decide_with_blocks(
&*access,
&policy,
blocks.as_deref(),
id,
hash,
Request::Fetch,
)
})
});
let _ = m.tx.send(verdict).await;
}
ProviderMessage::ObserveRequestReceived(m) => {
let verdict = admit(peers.get(&m.connection_id), |id| {
decide(&*access, &policy, id, &m.request.hash, Request::Observe)
decide_with_blocks(
&*access,
&policy,
blocks.as_deref(),
id,
&m.request.hash,
Request::Observe,
)
});
let _ = m.tx.send(verdict).await;
}
Expand Down Expand Up @@ -303,6 +362,36 @@ mod tests {
));
}

#[test]
fn a_blocked_member_is_refused_fetch_and_observe() {
// #2245: the block composes over membership — a current member
// blocked by endpoint key is served nothing.
let t = one_world(id(1), blob(9));
let p = BlobPolicy::default().with_observe();
let blocks = crate::pull_policy::testing::stub_blocks(&[*id(1).as_bytes()], &[]);
for request in [Request::Fetch, Request::Observe] {
assert!(!decide_with_blocks(
&t,
&p,
Some(&*blocks),
&id(1),
&blob(9),
request
));
}
// A list that blocks someone else leaves the member's verdict
// exactly what membership decides.
let blocks = crate::pull_policy::testing::stub_blocks(&[*id(2).as_bytes()], &[]);
assert!(decide_with_blocks(
&t,
&p,
Some(&*blocks),
&id(1),
&blob(9),
Request::Fetch
));
}

#[test]
fn a_member_of_an_unrelated_world_is_refused() {
let t = Table {
Expand Down
Loading
Loading