diff --git a/crates/credentials-module/src/connectors_tests.rs b/crates/credentials-module/src/connectors_tests.rs index 7a447729..c3d6fab3 100644 --- a/crates/credentials-module/src/connectors_tests.rs +++ b/crates/credentials-module/src/connectors_tests.rs @@ -2757,4 +2757,111 @@ mod connectors_contract { } assert!(rx.try_recv().is_err()); } + + /// AN ENROLLED CONSUMER MUST NOT REACH A FENCED KEY. + /// + /// The transfer-key fence names the ACCESS, not the grant: the store admits a + /// category grant over a category that holds the transfer key, and the read + /// surface is the half that refuses it. The enrolled path has its own fence + /// arm, and this test is what says that arm is not optional. + #[tokio::test] + async fn an_enrollment_token_does_not_reach_a_fenced_key() { + use credentials_core::federation::VAULT_TRANSFER_ID; + let (surface, admin, store, _db, _root) = rig(233); + let pem = credentials_core::kem::generate_key().expect("transfer key"); + store + .create_audited( + VAULT_TRANSFER_ID, + &VaultRecord::new_static( + CredentialKind::KemKey, + "operator", + pem.into_bytes(), + None, + ), + AuditCtx::admin(AuditOp::Put), + ) + .expect("transfer init"); + store + .set_categories_audited( + VAULT_TRANSFER_ID, + credentials_core::store::SetCategoryMode::Set, + &["federation-keys".to_string()], + AuditCtx::admin(AuditOp::SetCategory), + ) + .expect("the store lets the key into a category"); + // The same category holds an ordinary key, so the enrolled grant covers + // something the fence must NOT refuse. Without that control read a route + // that dropped the token would fall to the reserved branch and answer + // not_found for the same reason the fence answers it. + let ordinary = "apikey:fence-probe"; + store + .create_audited( + ordinary, + &VaultRecord::new_static(CredentialKind::ApiKey, "test", vec![1], None), + AuditCtx::admin(AuditOp::Put), + ) + .expect("ordinary key"); + store + .set_categories_audited( + ordinary, + credentials_core::store::SetCategoryMode::Set, + &["federation-keys".to_string()], + AuditCtx::admin(AuditOp::SetCategory), + ) + .expect("the store lets the key into a category"); + store + .create_read_grant_audited( + "enrolled", + "fence-probe", + credentials_core::store::SelectorKind::Category, + "federation-keys", + GrantOperation::Read, + AuditCtx::admin(AuditOp::GrantCreate), + ) + .expect("grant"); + let secret = "07".repeat(32); + let hash = credentials_core::enrollment::enrollment_secret_hash(&secret).unwrap(); + let proposed = store.propose_enrollment("fence-probe", &hash).unwrap(); + store + .approve_enrollment_with_grants(&proposed.request_id, "fence-probe", true, "operator") + .unwrap(); + let credentials_core::enrollment::EnrollmentPoll::Approved { token, .. } = + store.poll_enrollment(&proposed.request_id, &secret).unwrap() else { + panic!("expected an approved enrollment"); + }; + declared_bind( + &surface, + &admin, + 77, + Some(reserved("fence-probe")), + "self", + None, + ) + .await; + // CONTROL: the same token serves the ordinary key in the same category. + let control = routed_json( + &surface, + &admin, + 77, + OP_GET_SCOPED, + json!({"credential_id": ordinary, "enrollment_token": token}), + ) + .await; + assert!( + control["result"].get("payload").is_some(), + "the enrollment token must serve the ordinary key it covers: {control}" + ); + let reply = routed_json( + &surface, + &admin, + 77, + OP_GET_SCOPED, + json!({"credential_id": VAULT_TRANSFER_ID, "enrollment_token": token}), + ) + .await; + assert_eq!( + reply["result"]["error"]["code"], "not_found", + "an enrollment token must not reach a fenced key: {reply}" + ); + } }