# Custos in the browser Version 2 of the browser extension (core 0.20.0): Firefox, Edge and Chrome on Windows, from one source; logins and their 2FA codes, and saving a new login. Passkeys come later. The extension, its native host and the rule that decides which login belongs on which page are all in this public repository, so anyone can read what the browser side does. The desktop app (the private `Custos` repo) holds the vault and answers the host; its side is `docs/13_browser.md` there. ``` page ─(content script)─► background ─(native messaging)─► custos-native.exe ─(\\.\pipe\custos-domi)─► custos.exe extension/content.js extension/background.js crates/custos-native the desktop app ``` ## The pieces | Piece | What it does | What it never does | |:--|:--|:--| | `extension/content.js` | Finds login fields (password, username next to one, one-time code); on focus shows a small Custos button in a **closed** shadow root; on a (trusted) click asks for the logins of *its own frame*, and fills the one the person picks: username and password, or only the 2FA code on a page that asks only for that. When a form is sent with a password the person typed (not one Custos filled), offers to save it. | Fill without a click; read anything from Custos before the list is opened; put a secret anywhere but its field; trust what the page says about its own address. | | `extension/popup.*` | The toolbar popup: the logins for the active tab, a 2FA code on request, a login offered for saving, and this connection's match code. A click asks the top frame's content script to fill. | Receive a password: the content script asks for the secrets itself. | | `extension/background.js` | Owns the one connection to the native host; turns each request into a `browser_*` op, with the frame's origin from **`sender.url`** and its page's from **`sender.tab.url`** (as the browser reports them). Holds a login offered for saving in memory, for its tab, at most 3 minutes. | Accept messages from pages or other extensions (`sender.id` must be its own); ask for a frame whose real origin is not its URL's (`sender.origin`, e.g. a sandboxed frame); write anything to `storage`. | | `crates/custos-native` | The native messaging host. Checks who started it (`custos_browser::caller`); reads frames from stdin, **rebuilds** each request from its known fields, adds its connection's `session`, forwards it to the Custos pipe and returns the answer. | Answer an extension that is not Custos's (`not_allowed`, nothing reaches Custos); forward any other Domi op (`get`, `list`, `sign`, …); talk to a pipe served by anything but `custos.exe`; keep a secret after the answer is written (frames and parsed messages are zeroized). | | `crates/custos-browser` | `Origin::parse`, `login_matches`, `best_match`, `fill_allowed` (which login fits which frame of which page), `caller`, and the native messaging framing. The desktop uses the same crate, so both sides apply one rule. | Fuzzy matching of any kind. | | `tools/extension.mjs` | Builds `target/extension/firefox/` and `…/chromium/` from the one source with each browser's manifest (`extension/manifest..json`) and the workspace version; `--zip` writes reproducible packages for AMO and the stores. | Differ per browser: the scripts are the same files. | ## Which login goes where (`custos-browser`) A page's origin is `scheme://host[:port]`, host lowercased, trailing dot removed. A login's stored `url` fields (an item may have several) match a page when: 1. the page is **fillable**: `https`, or plain `http` only on this machine (`localhost`, `*.localhost`, `127.0.0.1`, `[::1]`). Plain http on the internet is never filled, even for a login saved with `http://`; 2. the schemes are equal (a stored URL without a scheme means `https`); 3. the ports are equal (the stored URL's port, or the scheme's default when it names none); 4. the page's host is the login's host (**exact**; `www.` counts as the host) or a subdomain of it (**subdomain**: `accounts.github.com` for `github.com`). A login for a subdomain never goes to its parent or to a sibling. A login for an IP address, a bare name (`cz`) or a **public suffix** (`co.uk`, `github.io`, `herokuapp.com`: Mozilla's Public Suffix List, the `psl` crate) matches only that exact host, because the subdomains of a public suffix belong to different people; 5. **in a frame** (`fill_allowed`): the frame matches, and when the frame is not the top page, the top page matches too. `evil.net` framing `github.com/login` gets nothing, and neither does an ad frame on `github.com`. URLs with userinfo (`https://bank.cz@evil.net`), a port that is not only digits (`:+443`) or a host that is not ASCII (the browser reports punycode, and that is what is compared) are refused outright. Look-alikes (`github.com.evil.net`, `githuh.com`, punycode twins) never match, because nothing is compared but labels. Exact matches are offered before subdomain matches, then by title; at most 50 logins per page. ## The messages Native messaging frames: a 4-byte little-endian length, then UTF-8 JSON (the same in Firefox and Chromium). The host accepts at most 64 KiB per message (the desktop pipe's limit). Every request may carry `seq` (a number); the answer carries it back. The host adds `app`, `browser` (`firefox` from its arguments; `edge`, `chrome` or `chromium` as the extension says, a label) and `session` (64 hex digits, random per connection) to each. | Extension → host | Answer | |:--|:--| | `{"op":"browser_status"}` | `{"ok":true,"code":"123456"}`: the session's match code | | `{"op":"browser_match","origin","top"}` | `{"ok":true,"logins":[{"id","title","username","totp"}]}` (`totp`: it has a 2FA code); never a secret | | `{"op":"browser_fill","id","origin","top"}` | `{"ok":true,"username","password","totp"}` only if that login may go into that frame of that page, else `no_match` | | `{"op":"browser_totp","id","origin","top"}` | `{"ok":true,"totp","remaining"}` (seconds left) under the same rule; `no_totp` without one | | `{"op":"browser_save","origin","top","username","password"[,"title"]}` | after Custos's card: `{"ok":true,"saved":"new"/"updated"/"unchanged"}`, or `refused` | Limits: `origin`, `top` ≤ 2 KiB, `id` ≤ 128 bytes, `username` ≤ 1 KiB, `password` 1 byte to 4 KiB, `title` ≤ 200 bytes. Errors the extension turns into a sentence: `not_connected` (no host registered), `not_allowed` (not the Custos extension), `not_running`, `not_custos` (the pipe's server is not `custos.exe`: nothing was sent), `locked`, `denied`, `refused`, `not_found`, `no_match`, `no_totp`, `no_answer`, `bad_request`. ## What Custos checks (the desktop's side, summarised) - The host is a Domi client like any other: Custos identifies it by its executable and asks the person once whether it may use **`web-logins/`**. That grant is the browser's only; no `domi://` ref may use the `web-logins` namespace and the browser ops reach nothing else. - Custos parses both origins again with `custos-browser` and checks the login with `fill_allowed` on **every** `browser_fill` and `browser_totp`, whatever the extension asked for before. - The grant card shows the session's match code, the one the popup shows. Every fill, code and save goes in the vault's audit with that code. - `browser_save` always shows a card (*Save* / *Don't save*): a new *Web login* for the origin, or, when a login of that site has the same username, its new password. An identical login is `unchanged`, without a card. - Only items of type *Web login* are offered. A locked Custos waits for the unlock (up to a minute), then answers `locked`. ## Registration and the extension ids | Browser | Extension id | Host manifest | Registry (HKCU, per user) | |:--|:--|:--|:--| | Firefox | `custos@marek-novak.cz` (`browser_specific_settings.gecko.id`) | `"allowed_extensions": ["custos@marek-novak.cz"]` | `Software\Mozilla\NativeMessagingHosts\cz.marek_novak.custos` | | Edge | `jchkapmceepdnlhagfppeckggaecdcmn` (the `key` in `manifest.chromium.json`) | `"allowed_origins": ["chrome-extension://jchkapmceepdnlhagfppeckggaecdcmn/"]` | `Software\Microsoft\Edge\NativeMessagingHosts\cz.marek_novak.custos` | | Chrome | the same | the same | `Software\Google\Chrome\NativeMessagingHosts\cz.marek_novak.custos` | The desktop writes both host manifests and registers them. The Chromium `key` fixes the id however the extension is loaded; the private key that matches it is not in this repository (it is held in Custos) and is needed only to pack a `.crx`, or for the first upload to the Chrome Web Store, to keep the id. Release Firefox installs only add-ons signed by Mozilla: the Firefox zip is signed through AMO as an *unlisted* (self-distributed) add-on. ## Threat model, briefly - A malicious **page** cannot ask for a login: content scripts run in an isolated world, the button and list are in a closed shadow root, the origin comes from the browser, and filling needs a click in Custos's own UI. A page *can* read what was filled into its own fields: that is what filling is. - A malicious **frame** on a good site is a different origin: it gets only its own origin's logins, and those only if the page around it matches them too. A good site framed by a malicious page gets nothing. - Another **extension** cannot message this one (`sender.id`), cannot start the host (`allowed_origins`, `allowed_extensions`), and if a stray manifest let it, the host answers it only `not_allowed` (`caller`). - A page can make the extension *offer* to save a login for **its own** origin, with values of its choosing; it is saved only on Custos's card, which names the site. - A program that **impersonates Custos** by creating the pipe first gets nothing: the host checks the pipe server's image path ends in `\custos.exe` before writing the request. (A process of the same user that can replace `custos.exe` is out of scope, as everywhere in Custos: docs/THREAT_MODEL.md.) - The extension holds no secret at rest. Its only state is the open native port and, for a login offered for saving, that login in the background's memory for at most 3 minutes (JavaScript strings cannot be wiped; they are dropped). ## Not yet Passkeys (WebAuthn through the extension), filling cards, other operating systems. ## Tests ``` cargo test -p custos-browser -p custos-native node tools/extension-test.mjs ``` `custos-browser` covers origin parsing, look-alikes (punycode twins, extra labels, userinfo, encoded dots, backslashes, odd ports), public suffixes, frames and their pages, ports and schemes, bare names and IPs, best-of-several, the callers and the frames. `custos-native` covers that only the five ops pass, rebuilt from known fields, and that another extension gets `not_allowed` without reaching Custos. `tools/extension-test.mjs` runs the background script against a fake browser: origins from the browser and never from the message, frames, sandboxed and blank frames, other extensions, a locked Custos, and a login offered for saving that never comes back out. The desktop repo has the pipe tests and the end-to-end runs in each browser.