//! Owner calls for a held database carry its query scope. use cotton_client::transport::{ClientAnswer, ClientRequest, ServerFetch, ServerRefusal}; use serde_json::Value; use crate::handle::CottonHandle; use crate::methods::MethodError; /// The code a server-bound call answers on a database no server serves /// ([`not_served`]). pub const NOT_SERVED: &str = "not_served"; /// The typed answer of a server-bound call made on a database no server /// serves: nothing was sent. The app should not have called it; when it does, /// Debug lists it under the calls not used on this device, never as a /// failure (`shell::diagnostics::not_used`; architect's ruling C1, 22 Oct /// 2026). The message is for that log line alone. pub fn not_served(method: &str) -> MethodError { MethodError::new(NOT_SERVED, format!("{method} needs a server and no server serves this database")) } /// An owner call about the request's database, its every request naming that /// database in its signed query. pub fn with_fetch( handle: &CottonHandle, request: &Value, work: impl FnOnce(&dyn ServerFetch) -> Result, ) -> Result { // Dispatch strips the argument after selecting its held replica; the // scope it named is carried on every request. let database = super::owner::optional_text(request, "database").or_else(crate::shell::client::named_database); super::owner::with_fetch(handle, |fetch| match database.as_deref() { Some(database) => work(&ScopedFetch { fetch, database }), None => work(fetch), }) } #[derive(Debug)] pub(crate) struct ScopedFetch<'a> { fetch: &'a dyn ServerFetch, database: &'a str, } impl ServerFetch for ScopedFetch<'_> { fn remove_device(&self, device: &str, erase: bool, _database: Option<&str>) -> Result { self.fetch.remove_device(device, erase, Some(self.database)) } fn call(&self, mut request: ClientRequest) -> Result { request.path = cotton_core::device::database::naming_database(&request.path, self.database); self.fetch.call(request) } } #[cfg(test)] mod tests { use super::*; #[derive(Debug)] struct Capturing(std::sync::Mutex>); impl ServerFetch for Capturing { fn call(&self, request: ClientRequest) -> Result { self.0.lock().unwrap().push(request); Ok(ClientAnswer { status: 200, body: b"{}".to_vec(), }) } } // RULE: A phone's database owner calls name that database in the signed request, including pending claims, admission and removal; they never use the personal database's default scope. #[test] fn owner_requests_carry_the_database_before_the_transport_signs() { let fetch = Capturing(std::sync::Mutex::new(Vec::new())); let scoped = ScopedFetch { fetch: &fetch, database: "garden", }; scoped .call(ClientRequest::get("/v1/devices/pending")) .unwrap(); scoped .call(super::super::owner::post( "/v1/invites".into(), &serde_json::json!({}), )) .unwrap(); let membership_paths = [ "/v1/databases/members/prepare", "/v1/databases/members/apply", "/v1/databases/members/claims", "/v1/databases/members/claims/synthetic-claim/decline", ]; for path in membership_paths { scoped.call(ClientRequest::get(path)).unwrap(); } let seen = fetch.0.lock().unwrap(); assert_eq!(seen[0].path, "/v1/devices/pending?database=garden"); assert_eq!(seen[1].path, "/v1/invites?database=garden"); for (request, path) in seen[2..].iter().zip(membership_paths) { assert_eq!(request.path, format!("{path}?database=garden")); } } } #[cfg(test)] mod removal_scope_guard { use super::*; use std::sync::Mutex; #[derive(Debug, Default)] struct Wire(Mutex>); impl ServerFetch for Wire { fn call(&self, request: ClientRequest) -> Result { self.0.lock().unwrap().push(request); Ok(ClientAnswer { status: 200, body: b"{}".to_vec() }) } } /// RULE: Device removal names the shown database before signing, so removing a shared database's device cannot revoke a Personal approval instead. #[test] fn device_removal_uses_the_shown_database() { let wire = Wire::default(); let fetch = ScopedFetch { fetch: &wire, database: "invented-shared-database" }; crate::phone::devices::over(&fetch, "devices.revoke", &serde_json::json!({"deviceId":"synthetic-device"})).unwrap(); let sent = wire.0.lock().unwrap(); assert_eq!(sent[0].path, "/v1/devices/approved/synthetic-device?database=invented-shared-database"); } }