//! The relay name an origin reaches, and the flavor rule over it: a relay //! name is one household's, and Cotton Dev only ever reaches a name ending in //! `-dev` while CottonDB never does (`cotton_core::flavor`). Asked of the //! ORIGIN, so every spelling of one household's address is the same refusal. //! The signed transport asks it before it signs anything to an origin //! (`SignedHttp::confirm_app`). use cotton_core::device::pairing::{is_relay_host, relay_name_of_host}; use cotton_core::flavor::Flavor; /// Why `origin` is a household the app `flavor` must never reach, or `None`. /// /// Only an origin under the relay domain names a household; a LAN address or /// any other host is no business of the flavor rule (a server ANSWERING there /// says which app it is — [`super::server_flavor_of`]). `None` for an /// undeclared process. pub fn origin_refusal(origin: &str, flavor: Option) -> Option { let flavor = flavor?; let name = relay_name_reached(origin)?; flavor.refusal_for_relay_name(&name) } /// The relay name an origin reaches, or `None` when its host is not under the /// relay domain (or is the relay's own apex). /// /// Deliberately looser than `cotton_core::device::pairing::relay_name_of`, /// which answers only for the exact canonical origin: a guard must not be /// talked out of by a port, a trailing dot, upper case or a deeper label — /// `https://x.studio.relay.cottondb.com:443` still reaches a name, and that /// name is the label nearest the relay domain. pub fn relay_name_reached(origin: &str) -> Option { let rest = origin .trim() .split_once("://") .map_or(origin.trim(), |(_, rest)| rest); let authority = rest.split(['/', '?', '#']).next()?; let host = authority.rsplit_once('@').map_or(authority, |(_, host)| host); let host = match host.rsplit_once(':') { Some((bare, port)) if port.bytes().all(|b| b.is_ascii_digit()) => bare, _ => host, }; let host = host.trim_end_matches('.').to_ascii_lowercase(); if !is_relay_host(&host) { return None; } // The NEAREST label that, with the relay domain after it, is exactly a // relay host — the one grammar `cotton_core` keeps, never a second one. let labels: Vec<&str> = host.split('.').collect(); (0..labels.len()).rev().find_map(|from| { relay_name_of_host(&labels[from..].join(".")).map(str::to_string) }) } #[cfg(test)] #[path = "address_tests.rs"] mod tests;