//! Which extension started the native host. Browsers start `custos-native.exe` only for the extensions its //! host manifest names (`allowed_origins` for Chromium, `allowed_extensions` for Firefox), and they say which //! one it was in the host's arguments: //! //! - **Chromium** (Edge, Chrome): the first argument is the caller's origin, `chrome-extension:///` //! (then `--parent-window=` on Windows); //! - **Firefox**: the first argument is the host manifest's path, the second the add-on's id. //! //! The host checks them again ([`caller`]) and answers every request from anything else with `not_allowed`, //! without reaching Custos: a second line behind the browser's own check, for a manifest someone else wrote. /// The Chromium extension's id, fixed by the public key in its manifest (`extension/manifest.chromium.json`). pub const CHROMIUM_ID: &str = "jchkapmceepdnlhagfppeckggaecdcmn"; /// The Firefox add-on's id (`browser_specific_settings.gecko.id` in `extension/manifest.firefox.json`). pub const FIREFOX_ID: &str = "custos@marek-novak.cz"; /// The kind of browser the Custos extension runs in, as its host's arguments say. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Browser { Chromium, Firefox, } /// The caller of the host, from its arguments without the program name; `None` unless it is the Custos /// extension. pub fn caller>(args: &[S]) -> Option { let first = args.first()?.as_ref(); if first == format!("chrome-extension://{CHROMIUM_ID}/") { return Some(Browser::Chromium); } // Firefox: . The path is the browser's business; the id must be exactly ours. if first.to_ascii_lowercase().ends_with(".json") && args.get(1).map(AsRef::as_ref) == Some(FIREFOX_ID) { return Some(Browser::Firefox); } None } #[cfg(test)] mod tests { use super::*; #[test] fn only_the_custos_extension_may_use_the_host() { let edge = ["chrome-extension://jchkapmceepdnlhagfppeckggaecdcmn/", "--parent-window=0"]; assert_eq!(caller(&edge), Some(Browser::Chromium)); let firefox = [r"F:\Apps\Custos\cz.marek_novak.custos.firefox.json", "custos@marek-novak.cz"]; assert_eq!(caller(&firefox), Some(Browser::Firefox)); // Any other extension, a look-alike id, or no arguments at all (started by hand): refused. let refused: [&[&str]; 11] = [ &["chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/"], &["chrome-extension://jchkapmceepdnlhagfppeckggaecdcmnx/"], &["chrome-extension://jchkapmceepdnlhagfppeckggaecdcmn"], &["CHROME-EXTENSION://JCHKAPMCEEPDNLHAGFPPECKGGAECDCMN/"], &["moz-extension://jchkapmceepdnlhagfppeckggaecdcmn/"], &[r"C:\x\manifest.json", "evil@example.com"], &[r"C:\x\manifest.json", "custos@marek-novak.cz.evil"], &[r"C:\x\manifest.json", "Custos@marek-novak.cz"], &["custos@marek-novak.cz"], &[r"C:\x\manifest.txt", "custos@marek-novak.cz"], &[], ]; for other in refused { assert_eq!(caller(other), None, "{other:?}"); } } }