diff --git a/pkgs/id/crates/id-directory/src/invites/event.rs b/pkgs/id/crates/id-directory/src/invites/event.rs index 97d36e9ac1..2ecf08ea67 100644 --- a/pkgs/id/crates/id-directory/src/invites/event.rs +++ b/pkgs/id/crates/id-directory/src/invites/event.rs @@ -327,8 +327,10 @@ impl InviteEvent { /// /// # Errors /// - /// Fails if a source invite's depth exceeds [`MAX_SOURCE_DEPTH`] (§2), or the - /// encoded body exceeds [`MAX_BODY_LEN`] (§1). + /// Fails if an invite's depth exceeds [`MAX_SOURCE_DEPTH`] (§2) — a source + /// invite's directly, and a child invite's too: terms are public and can be + /// built without [`InviteBody::child_terms`], so signing enforces the cap on + /// both — or the encoded body exceeds [`MAX_BODY_LEN`] (§1). pub fn sign_terms( key: &SigningKey, ticket: &EndpointTicket, @@ -340,6 +342,12 @@ impl InviteEvent { "a source invite's depth is {}, over the cap of {MAX_SOURCE_DEPTH}", terms.depth_remaining ); + } else { + ensure!( + terms.depth_remaining <= MAX_SOURCE_DEPTH, + "a child invite's depth is {}, over the cap of {MAX_SOURCE_DEPTH}", + terms.depth_remaining + ); } let body = InviteBody { inviter: key.verifying_key().to_bytes(), diff --git a/pkgs/id/tests/invites.rs b/pkgs/id/tests/invites.rs index 890b3a27b1..002f7ad79d 100644 --- a/pkgs/id/tests/invites.rs +++ b/pkgs/id/tests/invites.rs @@ -7,8 +7,8 @@ use anyhow::Result; use ed25519_dalek::SigningKey; use id::groups::Role; use id::invites::{ - AcceptSender, Invitation, InviteEvent, InviteSource, InviteTarget, InviteTerms, RouteStatus, - check_route, join, + AcceptSender, Invitation, InviteEvent, InviteSource, InviteTarget, InviteTerms, + MAX_SOURCE_DEPTH, RouteStatus, check_route, join, }; use iroh_base::{EndpointAddr, SecretKey, TransportAddr}; use iroh_tickets::endpoint::EndpointTicket; @@ -389,6 +389,59 @@ fn an_invite_with_terms_round_trips_and_still_checks_the_route() { assert_eq!(sent.sent.len(), 1); } +#[test] +#[allow(clippy::unwrap_used)] +fn sign_terms_refuses_a_directly_built_child_over_the_depth_cap() { + // Oracle (#13217): `InviteTerms` is public, so a child invite's terms can + // be built directly with a parent digest and an over-cap depth, bypassing + // `InviteBody::child_terms` and its strict decrease. Signing must refuse + // them: the cap binds child terms too, not only source terms. + let ticket = ticket(7); + let over_cap = InviteTerms { + parent_digest: Some([0x42; 32]), + depth_remaining: u8::MAX, + ..InviteTerms::source( + InviteSource::OpenDelegation, + InviteTarget::Group([0xaa; 32]), + Role::Write, + EXPIRES, + ) + }; + let error = InviteEvent::sign_terms(&inviter(), &ticket, over_cap).unwrap_err(); + assert!(format!("{error:#}").contains("over the cap"), "{error:#}"); + + // A valid child still signs: minted through `child_terms` from a source + // at the cap, its depth strictly decreases and stays under the cap. + let source = InviteEvent::sign_terms( + &inviter(), + &ticket, + InviteTerms::source( + InviteSource::OpenDelegation, + InviteTarget::Group([0xaa; 32]), + Role::Write, + EXPIRES, + ) + .with_depth(MAX_SOURCE_DEPTH) + .unwrap(), + ) + .unwrap(); + let child = source + .body + .child_terms( + InviteTarget::Group([0xaa; 32]), + Role::Read, + EXPIRES, + MAX_SOURCE_DEPTH - 1, + None, + None, + None, + ) + .unwrap(); + let event = InviteEvent::sign_terms(&inviter(), &ticket, child).unwrap(); + assert_eq!(event.body.depth_remaining, MAX_SOURCE_DEPTH - 1); + assert_eq!(event.body.parent_digest, Some(source.body.digest())); +} + #[test] #[allow(clippy::unwrap_used)] fn a_mint_whose_paste_form_is_over_budget_is_refused() {