[browser] Firefox: one extension source for Firefox, Edge and Chrome; frames, public suffixes, the caller check, 2FA codes and saving logins; v0.20.0
- custos-browser: fill_allowed (a frame gets a login only when its page would too), public suffixes match exactly only (psl), ports digits only, caller (which extension started the host); spoof tests. - custos-native: answers not_allowed to anything but the Custos extension, browser_totp and browser_save, the frame's and the page's origins, a random session per connection (its match code on Custos's cards). - extension/: manifest.firefox.json (gecko id custos@marek-novak.cz) and manifest.chromium.json, one set of scripts; the popup shows 2FA codes, the save offer and the connection's code; tools/extension.mjs builds each browser and reproducible zips; tools/extension-test.mjs runs the background against a fake browser.
This commit is contained in:
@@ -7,4 +7,6 @@ license.workspace = true
|
||||
repository.workspace = true
|
||||
|
||||
[dependencies]
|
||||
# Mozilla's Public Suffix List, compiled in: a login for `github.io` or `co.uk` never goes to their subdomains.
|
||||
psl = "2"
|
||||
zeroize.workspace = true
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
//! 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://<id>/`
|
||||
//! (then `--parent-window=<n>` 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<S: AsRef<str>>(args: &[S]) -> Option<Browser> {
|
||||
let first = args.first()?.as_ref();
|
||||
if first == format!("chrome-extension://{CHROMIUM_ID}/") {
|
||||
return Some(Browser::Chromium);
|
||||
}
|
||||
// Firefox: <manifest path> <add-on id>. 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:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,10 @@
|
||||
//! - [`login_matches`]: does a login's stored URL belong to that origin? Its host or a subdomain of it,
|
||||
//! same scheme, same port when the login names one. Never a "similar" name, so look-alike domains
|
||||
//! (`paypa1.com`, `paypal.com.evil.net`) never match.
|
||||
//! - [`framing`]: Chrome's native messaging frames (a 4-byte little-endian length, then JSON).
|
||||
//! - [`fill_allowed`]: the same for a login field in a frame: the frame's origin, and the top page's too when
|
||||
//! the frame is not the top page, so a login never goes into a site embedded by another one.
|
||||
//! - [`caller`]: which extension started the native host (the browser's arguments), refused unless it is Custos's.
|
||||
//! - [`framing`]: native messaging frames (a 4-byte little-endian length, then JSON), the same in every browser.
|
||||
//!
|
||||
//! ```
|
||||
//! use custos_browser::{login_matches, Origin};
|
||||
@@ -16,6 +19,7 @@
|
||||
//! # Ok::<(), custos_browser::OriginError>(())
|
||||
//! ```
|
||||
|
||||
pub mod caller;
|
||||
pub mod framing;
|
||||
|
||||
use std::fmt;
|
||||
@@ -95,6 +99,7 @@ fn split(url: &str, default: Option<Scheme>) -> Result<(Scheme, String, Option<u
|
||||
}
|
||||
let port = match port {
|
||||
None | Some("") => None,
|
||||
Some(p) if !p.bytes().all(|b| b.is_ascii_digit()) => return Err(OriginError::BadPort),
|
||||
Some(p) => Some(
|
||||
p.parse::<u16>()
|
||||
.map_err(|_| OriginError::BadPort)
|
||||
@@ -157,7 +162,8 @@ pub enum Match {
|
||||
/// A stored URL without a scheme (`github.com/login`) means https. The page must be fillable, the schemes
|
||||
/// equal, the port equal when the stored URL names one (else the page's must be the scheme's default), and
|
||||
/// the page's host the login's host or a subdomain of it. A login for a bare top-level name (`com`) or for
|
||||
/// an IP address matches only that exact host.
|
||||
/// an IP address matches only that exact host, and so does a login for a public suffix (`co.uk`, `github.io`,
|
||||
/// Mozilla's Public Suffix List): its subdomains belong to different people.
|
||||
pub fn login_matches(item_url: &str, page: &Origin) -> Option<Match> {
|
||||
if !page.fillable() {
|
||||
return None;
|
||||
@@ -171,7 +177,7 @@ pub fn login_matches(item_url: &str, page: &Origin) -> Option<Match> {
|
||||
if item == at {
|
||||
return Some(Match::Exact);
|
||||
}
|
||||
if page.is_ip() || !item.contains('.') || item.starts_with('[') {
|
||||
if page.is_ip() || !item.contains('.') || item.starts_with('[') || psl::domain_str(&item).is_none() {
|
||||
return None;
|
||||
}
|
||||
at.strip_suffix(&item).filter(|rest| rest.ends_with('.')).map(|_| Match::Subdomain)
|
||||
@@ -182,6 +188,23 @@ pub fn best_match<'a>(urls: impl IntoIterator<Item = &'a str>, page: &Origin) ->
|
||||
urls.into_iter().filter_map(|u| login_matches(u, page)).min()
|
||||
}
|
||||
|
||||
/// May the login with `urls` be filled into a field of the frame at `frame`, on the tab whose top page is `top`?
|
||||
///
|
||||
/// The frame must match (it is where the password goes). A frame that is not the top page (another origin) must
|
||||
/// match **and** the top page must match too: `evil.net` embedding `github.com/login` in an iframe gets nothing,
|
||||
/// and neither does a `github.com` page that embeds an advertiser's frame. The answer is the frame's match.
|
||||
pub fn fill_allowed<'a>(
|
||||
urls: impl IntoIterator<Item = &'a str> + Clone,
|
||||
frame: &Origin,
|
||||
top: &Origin,
|
||||
) -> Option<Match> {
|
||||
let m = best_match(urls.clone(), frame)?;
|
||||
if frame != top && best_match(urls, top).is_none() {
|
||||
return None;
|
||||
}
|
||||
Some(m)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -278,4 +301,92 @@ mod tests {
|
||||
Some(Match::Subdomain)
|
||||
);
|
||||
}
|
||||
|
||||
/// Hostile pages, one trick each: none of them gets the `github.com` login.
|
||||
#[test]
|
||||
fn spoofed_addresses_never_get_a_login() {
|
||||
let gh = "https://github.com";
|
||||
// Look-alike hosts: punycode twins (Cyrillic and Greek letters), extra labels, hyphens, typos.
|
||||
for evil in [
|
||||
"https://xn--gthub-n4a.com/",
|
||||
"https://xn--80ak6aa92e.com/",
|
||||
"https://xn--github-xyz.com/",
|
||||
"https://github.com.evil.net/",
|
||||
"https://www.github.com.evil.net/",
|
||||
"https://github.com-login.evil.net/",
|
||||
"https://wwwgithub.com/",
|
||||
"https://github.co.evil.net/",
|
||||
"https://evil.net/github.com",
|
||||
"https://evil.net/?next=https://github.com",
|
||||
"https://evil.net/#@github.com",
|
||||
] {
|
||||
assert_eq!(login_matches(gh, &page(evil)), None, "{evil}");
|
||||
}
|
||||
// Addresses that hide their host: userinfo, encoded dots, backslashes, unicode, odd ports. Refused outright.
|
||||
for bad in [
|
||||
"https://github.com@evil.net/",
|
||||
"https://github.com:443@evil.net/",
|
||||
r"https://evil.net\@github.com/",
|
||||
"https://github%2ecom/",
|
||||
"https://github.com%2eevil.net/",
|
||||
r"https://github.com\.evil.net/",
|
||||
"https://g\u{0456}thub.com/",
|
||||
"https://github.com:+443/",
|
||||
"https://github.com:-1/",
|
||||
"https://github.com: 443/",
|
||||
] {
|
||||
assert!(Origin::parse(bad).is_err(), "{bad}");
|
||||
}
|
||||
// A stored URL that hides its host never matches either.
|
||||
assert_eq!(login_matches("https://evil.net@github.com", &page("https://github.com/")), None);
|
||||
assert_eq!(
|
||||
login_matches("https://bücher.de", &page("https://xn--bcher-kva.de/")),
|
||||
None,
|
||||
"only what the browser reports"
|
||||
);
|
||||
// Plain http, even of the right host, and https logins on http pages: never.
|
||||
assert_eq!(login_matches(gh, &page("http://github.com/")), None);
|
||||
assert_eq!(login_matches("http://github.com", &page("http://github.com/")), None);
|
||||
assert_eq!(login_matches("http://github.com", &page("https://github.com/")), None);
|
||||
// The case and a trailing dot are the same host; that is not a trick.
|
||||
assert_eq!(login_matches(gh, &page("https://GITHUB.com./")), Some(Match::Exact));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_public_suffix_has_no_subdomains_to_fill() {
|
||||
// Anyone can own a subdomain of these: a login saved for the suffix itself fits only the suffix.
|
||||
for (login, other) in [
|
||||
("https://github.io", "https://attacker.github.io/"),
|
||||
("https://co.uk", "https://bank.co.uk/"),
|
||||
("https://blogspot.com", "https://evil.blogspot.com/"),
|
||||
("https://herokuapp.com", "https://evil.herokuapp.com/"),
|
||||
] {
|
||||
assert_eq!(login_matches(login, &page(other)), None, "{login} on {other}");
|
||||
assert_eq!(login_matches(login, &page(&format!("{login}/"))), Some(Match::Exact), "{login}");
|
||||
}
|
||||
// A site on such a suffix keeps its own subdomains.
|
||||
assert_eq!(
|
||||
login_matches("https://mark.github.io", &page("https://docs.mark.github.io/")),
|
||||
Some(Match::Subdomain)
|
||||
);
|
||||
assert_eq!(login_matches("https://mark.github.io", &page("https://eve.github.io/")), None);
|
||||
assert_eq!(login_matches("https://bank.co.uk", &page("https://login.bank.co.uk/")), Some(Match::Subdomain));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_frame_gets_a_login_only_when_its_page_would_too() {
|
||||
let gh = ["https://github.com"];
|
||||
let (login, top_gh, evil) =
|
||||
(page("https://github.com/login"), page("https://github.com/"), page("https://evil.net/"));
|
||||
// The top page itself.
|
||||
assert_eq!(fill_allowed(gh, &login, &login), Some(Match::Exact));
|
||||
// A frame of the same site, on a page of the same site.
|
||||
assert_eq!(fill_allowed(gh, &page("https://gist.github.com/"), &top_gh), Some(Match::Subdomain));
|
||||
// github.com framed by evil.net: the frame's own origin matches, the page does not. Nothing.
|
||||
assert_eq!(fill_allowed(gh, &login, &evil), None);
|
||||
// An evil frame on github.com: its own origin does not match. Nothing.
|
||||
assert_eq!(fill_allowed(gh, &evil, &top_gh), None);
|
||||
// A plain-http top page around an https frame of the site: nothing either.
|
||||
assert_eq!(fill_allowed(gh, &login, &page("http://github.com/")), None);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user