use super::*; use ed25519_dalek::{Signer, SigningKey}; use rusqlite::types::Value; fn file(owner: &SigningKey) -> Driver { let db = Driver::open_in_memory().unwrap(); db.exec("CREATE TABLE _meta(workspace_id TEXT); INSERT INTO _meta VALUES('synthetic-database'); CREATE TABLE cotton_workspaces(id TEXT, namespace TEXT); INSERT INTO cotton_workspaces VALUES('synthetic-database',NULL); CREATE TABLE cotton_admissions(id TEXT,workspace_id TEXT,person_key TEXT,rights TEXT,admitted_by_person TEXT,invite_id TEXT,p_sig TEXT,admitted_at TEXT,_hlc TEXT)").unwrap(); let key = URL_SAFE_NO_PAD.encode(owner.verifying_key().as_bytes()); db.run("INSERT INTO cotton_admissions VALUES('birth','synthetic-database',?,'owner',?,NULL,NULL,'2026-10-20T00:00:00.000Z','2026-10-20T00:00:00:00001:synthetic')", &[Value::Text(key.clone()),Value::Text(key)]).unwrap(); db } fn grant(owner: &SigningKey, target: &SigningKey) -> ServingGrant { let mut grant = ServingGrant { kind: GrantKind::ServingGrant, database_id: "synthetic-database".into(), source_host: "https://source-dev.relay.cottondb.com".into(), source_epoch: 3, target_host: "https://target-dev.relay.cottondb.com".into(), target_key_fingerprint: fingerprint( &URL_SAFE_NO_PAD.encode(target.verifying_key().as_bytes()), ) .unwrap(), new_epoch: 4, grant_id: "synthetic-grant".into(), issued_at: "2026-10-20T01:00:00.000Z".into(), owner_signature: String::new(), }; grant.owner_signature = URL_SAFE_NO_PAD.encode(owner.sign(&grant.signing_bytes()).to_bytes()); grant } // RULE: Only the owner key already resolved from this file verifies its // serving grant. Supplying an external signing key cannot grant authority. #[test] fn the_grant_verifies_only_against_the_files_owner() { let owner = SigningKey::from_bytes(&[51; 32]); let other = SigningKey::from_bytes(&[52; 32]); let target = SigningKey::from_bytes(&[53; 32]); let db = file(&owner); let good = grant(&owner, &target); good.verify_copy(&db).unwrap(); let unowned = file(&owner); unowned.run("DELETE FROM cotton_admissions", &[]).unwrap(); assert_eq!( good.verify_copy(&unowned).unwrap_err(), Refused::OwnerMissing ); assert_eq!( grant(&other, &target).verify_copy(&db).unwrap_err(), Refused::OwnerSignatureInvalid ); assert_eq!( good.verify_copy(&file(&other)).unwrap_err(), Refused::OwnerSignatureInvalid ); db.run("UPDATE _meta SET workspace_id='another-database'", &[]) .unwrap(); assert_eq!(good.verify_copy(&db).unwrap_err(), Refused::WrongDatabase); } // RULE: A grant for another host or key cannot be adopted by this target, // even when the owner's signature is valid and the database id matches. #[test] fn a_target_verifies_its_host_and_its_own_key_fingerprint() { let owner = SigningKey::from_bytes(&[61; 32]); let target = SigningKey::from_bytes(&[62; 32]); let other = SigningKey::from_bytes(&[63; 32]); let db = file(&owner); let grant = grant(&owner, &target); let key = URL_SAFE_NO_PAD.encode(target.verifying_key().as_bytes()); grant.verify_target(&db, &grant.target_host, &key).unwrap(); assert_eq!( grant .verify_target(&db, "https://another-dev.relay.cottondb.com", &key) .unwrap_err(), Refused::WrongTargetHost ); assert_eq!( grant .verify_target( &db, &grant.target_host, &URL_SAFE_NO_PAD.encode(other.verifying_key().as_bytes()) ) .unwrap_err(), Refused::WrongTargetKey ); assert_eq!( fingerprint("not a public key").unwrap_err(), Refused::InvalidTargetKey ); } // RULE: The owner signs every routing and epoch field. An archive journal // cannot rewrite even one field of the in-file grant or skip an epoch. #[test] fn every_grant_field_is_covered_by_the_owner_signature() { let owner = SigningKey::from_bytes(&[71; 32]); let target = SigningKey::from_bytes(&[72; 32]); let db = file(&owner); let good = grant(&owner, &target); for field in [ "databaseId", "sourceHost", "sourceEpoch", "targetHost", "targetKeyFingerprint", "newEpoch", "grantId", "issuedAt", ] { let mut value = serde_json::to_value(&good).unwrap(); value[field] = match field { "sourceEpoch" => 2.into(), "newEpoch" => 5.into(), "issuedAt" => "2026-10-20T01:00:01.000Z".into(), _ => "changed".into(), }; let changed: ServingGrant = serde_json::from_value(value).unwrap(); assert_ne!(changed.signing_bytes(), good.signing_bytes(), "{field}"); assert!( !crate::signed::verifies( &URL_SAFE_NO_PAD.encode(owner.verifying_key().as_bytes()), &changed.signing_bytes(), &good.owner_signature ), "{field}" ); assert!(changed.verify_copy(&db).is_err(), "{field}"); } let mut overflow = good.clone(); overflow.source_epoch = i64::MAX; overflow.new_epoch = i64::MIN; overflow.owner_signature = URL_SAFE_NO_PAD.encode(owner.sign(&overflow.signing_bytes()).to_bytes()); assert_eq!( overflow.verify_copy(&db).unwrap_err(), Refused::InvalidEpoch ); let mut wrong_kind = serde_json::to_value(good).unwrap(); wrong_kind["kind"] = "another_kind".into(); assert!(serde_json::from_value::(wrong_kind).is_err()); }