Cotton code
130 lines · 4.1 KB · RustRaw
//! Whose server is this? The rule from the decision note: nobody at Cotton//! and no third party is ever in the path. A connection talks to the//! service's own server, or to a server on the person's Mac. A hosted MCP//! gateway — a middleman that reads every row the connection reads — is//! refused when it is known, and warned about when it is merely unknown.use url::Url;use super::refusal::ConnectionRefusal;/// First-party MCP hosts: the service's own, by registrable domain. A/// subdomain of one of these is the service's (`bindings.mcp.cloudflare.com`).const OWN: &[(&str, &str)] = &[    ("cloudflare.com", "Cloudflare"),    ("github.com", "GitHub"),    ("githubcopilot.com", "GitHub"),    ("atlassian.com", "Atlassian"),    ("linear.app", "Linear"),    ("notion.com", "Notion"),    ("sentry.io", "Sentry"),    ("stripe.com", "Stripe"),    ("paypal.com", "PayPal"),    ("hubspot.com", "HubSpot"),    ("intercom.com", "Intercom"),    ("asana.com", "Asana"),    ("canva.com", "Canva"),    ("figma.com", "Figma"),    ("vercel.com", "Vercel"),    ("supabase.com", "Supabase"),    ("neon.tech", "Neon"),    ("square.com", "Square"),    ("squareup.com", "Square"),    ("webflow.com", "Webflow"),    ("zapier.com", "Zapier"),    ("make.com", "Make"),    ("google.com", "Google"),    ("googleapis.com", "Google"),    ("microsoft.com", "Microsoft"),    ("slack.com", "Slack"),];/// Hosted gateways: a server that proxies OTHER services' data through its/// own account. Refused outright — a known middleman is not a warning.const GATEWAYS: &[&str] = &[    "smithery.ai",    "composio.dev",    "glama.ai",    "pipedream.net",    "mcp.run",    "klavis.ai",    "mcpmarket.com",    "pulsemcp.com",    "nango.dev",    "metorial.com",];#[derive(Debug, Clone, PartialEq, Eq)]pub enum Verdict {    /// The service's own server.    Own { service: &'static str },    /// Loopback or a `.local` name: a server on the person's Mac.    OnThisMac,    /// Neither: allowed with the warning said    /// ([`super::refusal::third_party_warning`]), if the person insists.    ThirdParty { host: String },    /// A known middleman, or a URL no connection may use (plain http to the    /// internet, no host, not a URL).    Refused(ConnectionRefusal),}impl Verdict {    pub fn is_refused(&self) -> bool {        matches!(self, Verdict::Refused(_))    }}/// Judge one MCP server URL.pub fn judge(url: &str) -> Verdict {    let parsed = match Url::parse(url.trim()) {        Ok(parsed) => parsed,        Err(_) => {            return Verdict::Refused(ConnectionRefusal::NotAUrl { url: url.to_string() });        }    };    let host = match parsed.host_str() {        Some(host) => host.trim_end_matches('.').to_ascii_lowercase(),        None => {            return Verdict::Refused(ConnectionRefusal::NoHost { url: url.to_string() });        }    };    let local = is_on_this_mac(&host);    match parsed.scheme() {        "https" => {}        "http" if local => {}        "http" => {            return Verdict::Refused(ConnectionRefusal::PlainHttp { url: url.to_string() });        }        other => {            return Verdict::Refused(ConnectionRefusal::NotAWebScheme { url: url.to_string(), scheme: other.to_string() });        }    }    if local {        return Verdict::OnThisMac;    }    if let Some(gateway) = GATEWAYS.iter().find(|domain| under(&host, domain)) {        return Verdict::Refused(ConnectionRefusal::Gateway { host, gateway: (*gateway).to_string() });    }    if let Some((_, service)) = OWN.iter().find(|(domain, _)| under(&host, domain)) {        return Verdict::Own { service };    }    Verdict::ThirdParty { host }}fn under(host: &str, domain: &str) -> bool {    host == domain || host.ends_with(&format!(".{domain}"))}fn is_on_this_mac(host: &str) -> bool {    if host == "localhost" || host == "::1" || host == "[::1]" || host.ends_with(".local") {        return true;    }    if let Ok(ip) = host.trim_matches(|c| c == '[' || c == ']').parse::<std::net::IpAddr>() {        return ip.is_loopback();    }    false}#[cfg(test)]#[path = "judge_tests.rs"]mod judge_tests;