0017 · Browser extension: thin client of klavd over native messaging, klav-wasm for the pure parts and the no-daemon mode postponed
- Status: accepted
- Date: 2026-11-02
Context
Phase 5 brings the browser extension (design §8, roadmap): autofill for logins, cards and addresses, TOTP, saving logins, password generator, fill_login with the extension target for agents and approvals from the browser. The roadmap says "native messaging with klavd (Touch ID); password if there is no daemon", and ADR 0001 says the extension uses klav-core through WASM.
We need to decide:
- where the unlocked account lives and who decrypts;
- what the WASM does;
- how the extension talks to
klavdand how to check who is on the other end; - how an agent's request reaches the tab;
- what the content script (the code that runs inside the page) receives;
- what to do with the "no daemon" mode.
Decision
1. The extension is a thin client of klavd
The unlocked account, Touch ID, sync, the encrypted cache, rollback detection and approval signatures stay in klavd (ADR 0012). The extension stores no key and no value: it asks klavd, over native messaging, only for what it needs at each moment.
- Unlock: the popup sends
unlockwithout a password (Touch ID, ADR 0013). If Touch ID isn't available, fails or isn't set up, the popup asks for the master password and sends it inunlock(password unlock through the daemon). The end-to-end tests use this path. - Lock:
klavddecides it (idle, sleep, screen locked). The extension has no timer of its own: it asks for the state and shows "locked" whenklavdanswerslocked. The popup's lock button sendslock. - The extension stores nothing in
chrome.storageexcept preferences with no secrets (shortcut, showing the icon in fields). Neither values nor vault metadata.
2. The no-daemon mode is postponed
A standalone mode with the password inside the extension is not an "alternative": it is a second klav-client in WASM. It would need:
- enrolling the extension as a device (OPAQUE login with the emergency kit);
- delta sync with the anti-rollback marks (T7) and its own encrypted cache in browser storage;
- unwrapping the vault keys with HPKE, renewing the session with single-use refresh tokens shared with nobody;
- signing approvals with the account identity;
- a 256 MiB Argon2id inside a service worker that the browser can kill after 30 s.
It is a new surface to audit (the account key would live in the browser process, next to every page) for a case that doesn't exist today: klav and klavd are installed together, and the extension is useless without the CLI to create the account. That is why phase 5 ships only the daemon mode. The roadmap checkbox is split: "native messaging with klavd (Touch ID or password)" is done; "password without a daemon" stays open, for when there is a reason (for example, the extension on a machine without the CLI). If it is done, it will be its own ADR and will use klav-client compiled to WASM, not a new TypeScript client.
3. klav-wasm carries the pure parts that have to match Rust
The WASM is loaded in the background and in the extension pages (never in the content script). It exports, with wasm-bindgen, klav-core functions with no state and no vault secrets:
- site matching:
fillable_host(url)andhost_matches(host, urls), the anti-phishing rule (§5) that used to live inklav-sdk. It moves toklav-core::siteand is used by the SDK,klavdand the extension. That way the rule is a single implementation in three places; - the password and passphrase generator (
klav-core::generator), for new-password fields even whenklavdis locked.
There is no vault cryptography in the WASM or in TypeScript. The generator uses getrandom with crypto.getRandomValues (feature wasm_js, only on wasm32). The extension's CSP allows 'wasm-unsafe-eval' to instantiate that bundled WASM; there is no remote code.
klav-core gains the url dependency (WHATWG, MIT/Apache-2.0), which is not cryptographic and was already in the tree through reqwest.
4. Native messaging: its own binary, klav-native-host
The browser launches the native messaging host as one process per connection, with the fixed path from the manifest, with no arguments of its own, and kills it when the port closes. klavd has to outlive the browser and must not mix that lifecycle with its own. That is why the host is a small binary, klav-native-host, in the klav-daemon crate, which only acts as a bridge:
extensión ──(stdin/stdout, 4 bytes de longitud + JSON)── klav-native-host ──(socket Unix, JSON por líneas)── klavd- Starting
klavd: if it isn't running, the host starts it the same wayklav unlockdoes (the code moves toklav-daemon::client, shared). KLAV_HOME: the browser doesn't pass environment variables.klav extension install-hostwrites, inside$KLAV_HOME, an executable wrapper that setsKLAV_HOMEand the allowed extensions, and calls the binary. The host manifest points to that wrapper.- Origin check on both sides:
- the browser only lets the extensions listed in the manifest connect (
allowed_originsin Chromium,allowed_extensionsin Firefox); - the host checks again the origin the browser passes it (
chrome-extension://<id>/inargv[1]in Chromium; the ID inargv[2]in Firefox) against its list, and exits without answering if it isn't there; klavdstill checks the peer's UID (T32);- the extension only accepts internal messages from its own ID (
sender.id) and, from a content script, uses the origin the browser gives (sender.origin/sender.url), never one the page claims.
- the browser only lets the extensions listed in the manifest connect (
- Allowlist of operations: the host only forwards to
klavdthe operations the extension needs (status,unlock,lock,sync,vaults,projects,list,view,suggest,autofill,totp,create,edit,approvals,decide_approval,extension_reply). Neverresolve,attach,create_many, agents, revocation, audit log orshutdown.createandeditfrom the extension never carryallow_duplicate. (ADR 0024 §5 addsimport_add,import_preview,import_applyandimport_cancelfor the import assistant: they return no values and never duplicate titles.create_manywas removed. ADR 0032 §6 addsenvironments,environment,environment_saveandenvironment_trash, andresolveonly withvariables_onlyand one reference, for «Show» and «Copy» of an environment's variable, whichklavdchecks and protects like a value.) - Size: from the extension to the host, at most 1 MiB per message; from the host to the extension, 1 MiB (Chromium's limit). List responses only carry metadata.
- Versioned protocol (
v: 1), documented inextension.md, with Rust tests for the wire format, the 4-byte frame, the operation list and the origin check. - Browsers:
klav extension install-hostwrites the manifest for Chrome, Chromium, Brave, Arc, Edge, Dia and Firefox in their user directories on macOS and Linux (Dia:~/Library/Application Support/Dia/User Data/NativeMessagingHosts, the same layout as Arc). It accepts--user-data-dirfor a specific profile (the tests use throwaway profiles). - Extension IDs: the development manifest carries a public key (
key) that pins the Chromium ID; Firefox usesbrowser_specific_settings.gecko.id. The store IDs are added with--extension-iduntil they exist and are pinned in the code.
5. Autofill: the content script only receives what it is going to type
- Matching (anti-phishing, T21): a login is only offered and filled if the frame's host matches the host of one of the item's URLs or is a subdomain of it (
www.is ignored), withhttps(orhttpon the same machine). It is the SDK's rule, now inklav-core::site. The Public Suffix List is not used (as in ADR 0006): ahttps://ejemplo.comURL doesn't fill onotro.ejemplo.com.evil.ioor onejemplo.co. Cards and identities have no URL: they are filled wherever the person picks them, only onhttps(or localhttp). klavdchecks the match, not just the extension:autofill { item, url }refuses a login whose site doesn't match. The URL is set by the background from what the browser says about the frame, not by the page.- It never fills on its own when the page loads. Filling requires a gesture from the person: the icon in the field, the suggestions menu or the keyboard shortcut.
- The suggestions menu doesn't run in the page: it is an extension page inside an
iframe(in a closed shadow root). The page can't read its content; the content script doesn't see the list of items. Only when one is picked does the background askklavdfor the values and send them to the specific frame that asked for the menu (frameIdand, in Chromium,documentId), and the content script checks its origin again before typing. - TOTP: if the login has TOTP, after the password it fills the one-time code field if it is on the page, or the one on the next page of the same site for 2 minutes. The code is computed in
klavdright before it is typed. - Save on submit: the content script detects the submission of a form with a password and sends the username and password to the background, which offers to save them or update the matching login, with the unique-titles rule.
6. Agents: fill_login with the extension target goes through klavd
The agent runtime is still the one that decrypts, with its grant, and the server still records the access with the domain, as with the SDK (ADR 0014 §9). What changes is the transport to the tab:
klav mcp(orklav agent fill --target extension) connects toklavd(the one for itsKLAV_HOME, orKLAV_BROWSER_HOME) and asks forbrowser_tab: the origin of the active tab in the focused window, without the path.- It checks
fillable_hostandhost_matchesagainst the item's URLs. If they don't match, it fails without revealing the host. - It asks the server for access (
fill, with that host asdomain): the policy, theaskapprovals and the audit log are the usual ones. - It sends the values in
browser_filltoklavd, which hands them to that browser's extension over the host connection. The extension checks that it is the same tab, that its origin didn't change and that it matches the item's URLs; it fills, shows on the page "Klav filled for agent name: reason" and answers only with the field names (and, withsubmit, whether another page loaded and its origin). - The values never go back to the agent or the LLM.
Event channel: the klavd protocol was request-response only. A connection that sends extension_attach (only the host sends it) is registered as a browser, and klavd can write event lines to it ({"v":1,"event":{…}}), which the extension answers with extension_reply. It is additive: clients that don't register see no events, so the version stays at 1. browser_tab and browser_fill don't need the account unlocked: the agent brings the values.
Approvals in the browser: the popup lists the pending approvals (approvals) and decides them (decide_approval) with Touch ID through klavd, or with the password if Touch ID isn't available, the same as the CLI.
7. Extension permissions, minimal
nativeMessagingto talk toklav-native-host;storageonly for preferences with no secrets;clipboardWriteandalarmsto copy and clear the clipboard after 30 s (in Chromium, plusoffscreen, because a service worker has no clipboard);- a content script declared for
http://*/*andhttps://*/*: detecting forms and offering to save requires seeing the page without a prior click, like any password manager. It runs in an isolated world and receives nothing from the vault until the person picks what to fill.
No webRequest, no history, no cookies, no remote code. The justification for each permission is in extension.md.
8. Stack
Strict TypeScript, esbuild for bundling (no plugins and no runtime code), vitest + jsdom for the form detection tests, eslint and Playwright for the end-to-end test with Chromium. A single codebase produces the Chromium package (MV3 with a service worker) and the Firefox one (MV3 with background.scripts).
Consequences
- Without
klavd, the extension only shows how to install it. The no-daemon mode stays as an open roadmap checkbox and a question for Omar. klavdgains the event channel and new operations (suggest,autofill,extension_attach,extension_reply,browser_tab,browser_fill);docs/klavd.mdandextension.mddocument them.- Any process of the same user can ask for
browser_fillwith its own values and a made-up agent label: it would type its own text into the active tab, as long as the origin matches the URLs it sends. It isn't new power (that process can already talk to the socket and the browser); it stays in the threat model. klav-coregainssiteand theurldependency;klav-sdkuses those functions instead of its own.- Building the extension needs the
wasm32-unknown-unknowntarget andwasm-bindgen-cliwith the exact version of thewasm-bindgencrate.
Implementation notes (phase 5)
- No
storage: there was no need to store preferences. What the extension remembers (open menus, a login waiting to be saved, the last login with a code) lives in the background's memory. Changed with the redesign, below. - No
activeTab: the popup asks the background for the active tab's address, and the background gets it from the browser withsender.url(the samewhere/hereexchange as the agents). - Firefox 140 or newer (the current ESR): it is the version that supports
data_collection_permissions, whichaddons.mozilla.orgrequires; the extension declares that it collects no data. Firefox for Android has no native messaging: it isn't offered there. - Clipboard: without
clipboardRead, it is cleared after 30 s even if something else was copied outside Klav (threat model, residual risk 15).
Redesign notes (2026-11-03)
With the new interface (diseno-visual/README.md):
storage, for a single setting: "Only fill, don't press" (storage.local, keyonlyFill, a boolean). Nothing from the vault, no hosts, no history. Everything else stays in the background's memory.*.localhostis the same machine for the §5 rule (RFC 6761):fillable_hostacceptshttp://idmsa.localhost, and an item forhttp://localhostmatches its subdomains, like any host. This makes it possible to test sign-ins with a frame from another subdomain without a real domain.is_loopback, which guards the SDK's CDP connections, doesn't change: onlylocalhostand the loopback IPs.- New surfaces in the page: the "Sign in as …" card, the central search and the agent notice are extension pages in an
iframe, like the menu (§5). Filling still requires a real click in a visible frame. A login from another site is only filled from the search, after a question with both hosts in view (threat model T69). - Multi-step sign-ins: after a login is picked, Klav follows its steps (email, password, code) and presses the buttons, with the state in the background per tab and a 60 s deadline. Each step goes through
suggestandautofillagain (§5). There are no newklavdoperations (threat model T68).