Vault
A vault store keeps its state device-encrypted, server-blind: sealed on the user's device, and kept on the app's server only as ciphertext the server cannot open. Its key comes from the user's passkey, and a 12-word recovery code brings it back. If every passkey is lost and the recovery code is lost too, the data is lost: nobody can open the vault then, not the app and not its server. Show the code once, and ask the user to write it down.
// src/notes.js
import { store } from '@qatjs/store'
/** @type {{ id: string, text: string }[]} */
const items = []
export const notes = store({
name: 'notes',
state: { items },
actions: {
add: (s, /** @type {string} */ text) => s.items.push({ id: crypto.randomUUID(), text }),
remove: (s, /** @type {string} */ id) => s.items.splice(s.items.findIndex((n) => n.id === id), 1),
},
persist: 'vault',
sync: true,
})notes state// src/app.qx
import { vault } from '@qatjs/vault'
import { VaultUnlock } from './ui/vault/vault.qx'
import { notes } from './notes.js'
export default function App() {
let text = state('')
return (
<VaultUnlock vault={vault}>
<form on:submit|prevent={() => notes.add(text)}>
<textarea aria-label="New note" bind:value={text}></textarea>
<button>Add note</button>
</form>
<ul>
{for (const n of notes.items)}<li key={n.id}>{n.text}</li>{/for}
</ul>
</VaultUnlock>
)
}text stateThat is examples/vault-notes, which runs this with a passkey in Chromium (examples/vault-notes/e2e/vault.spec.js).
persist: 'vault' compiles to vaultStore from @qatjs/vault (the store from @qatjs/store is unchanged).
Add sync: true and it compiles to syncStore from @qatjs/vault/sync, shared by the user's devices.
<VaultUnlock> and <RecoveryCode> are @qatjs/ui components: qatjs add vault copies both
(src/ui/vault/vault.qx).
The keys
Everything is WebCrypto: AES-256-GCM, HKDF-SHA-256, PBKDF2-SHA-256 and WebAuthn's PRF extension. Qat has no cryptographic primitive of its own.
- The root.
vault.create()draws 128 random bits. Every key is derived from it with HKDF: a store's data key from the store's name, its sync key the same way. The root is shown once, as the recovery code (12 words of the BIP-39 English list, the last word carrying a checksum). Where there is a vault already — on this device, or the server's copy of its keys —create()throws (code: 'exists'): a second vault took the first one's place, and what the first had sealed could not be opened again. Starting over, for someone who lost every key and the code, is the operator's: the user's records deleted on the server. - A passkey. The passkey's PRF output becomes a key-encryption key, and that key wraps the root.
The wrapped root is kept on the server (
keys), so the same passkey on another device (a synced passkey) opens the same vault. Without the passkey, the wrapped root is ciphertext. - This device's session. Once unlocked, the root is kept in IndexedDB as a non-extractable key,
for 12 hours by default (
configure({ session })). A reload does not ask for the passkey again.vault.lock()ends the session; one that ran out is deleted the next time the page opens, not left on the device.session: 0keeps it for the tab: across a reload, not past the tab. - A save that fails — the disk full, IndexedDB gone — goes to
onError, and is made again with the next write: the store keeps working. - The recovery code.
vault.recover(code, { passkey: true })opens the vault on a new device, or when every passkey is lost. It then wraps the root under a new passkey. A mistyped code fails its checksum, and nothing is derived from it. - A passphrase, when the browser has no PRF:
vault.create({ passphrase }), with PBKDF2 at 600,000 rounds.vault.weak()says so, and<VaultUnlock>shows it: a passphrase can be guessed, a passkey cannot. The passphrase-wrapped root stays on its device; the server never gets it. - Escrow, opt-in, only with the user's consent:
vault.create({ escrow: { consent: true } }). Any two of three open the vault: this device, the recovery code, the app's operator. The code alone already holds the root; escrow adds the pair that does not, this device and the operator. The root is sealed under the device's share, then under the operator's share, so either share alone opens nothing.release()from@qatjs/vault/escrowopens the vault once the operator'sreleaseallows it. Escrow is AES-GCM sealing twice, not Shamir's polynomial sharing: WebCrypto has no field arithmetic, and Qat writes none.
Keys are non-extractable CryptoKeys. The root's bytes are in JavaScript's memory only on the
recovery path: when the vault is made (to show its words), and when the words are typed back (or
the escrow shares are joined). A blob is a nonce and its ciphertext. The nonce is 96 random bits,
checked against every nonce this page issued under that key. The additional data is the store's id
and the blob's version, so a blob cannot pass for another store's, or for an older version of
itself. A changed byte, a wrong key or a relabeled blob does not open, and the vault stays locked:
it fails closed.
The API
@qatjs/vault |
|
|---|---|
vault.status |
'opening', 'new', 'locked' or 'unlocked' (a signal) |
vault.weak |
opened with a passphrase (a signal) |
vault.ready() |
reads this device's keys and session (the stores start it themselves) |
vault.passkeys() |
the browser can derive a key from a passkey (passkeys() too) |
vault.create({ passphrase?, name?, escrow? }) |
makes the vault; returns the 12 words |
vault.unlock({ passphrase? }) |
the passkey (or the passphrase) |
vault.recover(code, { passkey?, passphrase? }) |
the recovery code, then a new passkey or passphrase |
vault.lock() |
ends the session; every vault store goes back to its initial state |
configure({ url?, session?, timeout? }) |
the server copy's endpoint (/__qat/vault), the session's length (ms), how long the server is waited for (8,000 ms: after it the vault opens with what the device has, as offline) |
vaultStore(def) |
what persist: 'vault' compiles to |
@qatjs/vault/sync has syncStore(def), which sync: true compiles to, and sealed(key, room), the
connection that seals each operation. @qatjs/vault/escrow has release(). @qatjs/vault/words has
A code is read however it was kept — with commas, numbered, one word a line: only its letters are
its own, and its checksum says whether the twelve are the code.
toWords, fromWords and isWord: the recovery code's encoding, loaded only when a code is shown or
typed. The crypto helpers the entries share are exported too, for tests and audits: seal, open,
nonce, hkdf, derive, wrap, b64, bytes and ROUNDS.
The server's copy
qatjs dev keeps one developer's records in node_modules/.qat/vault. A deployed app serves its own
records from server/vault.js, which qatjs dev and qatjs preview serve at /__qat/vault/:
// server/vault.js
import { file, vault } from '@qatjs/server/vault'
export default vault({
store: file('./vaults'),
// whose vault a request is: the app's own auth (null: 401)
owner: (req) => req.headers.get('x-user'),
})A record is { id, blob, version, updatedAt }, kept per owner. owner has no default, so one user's
records are never another's. A PUT names the version its writer last saw, as base. When another
device saved since then, the answer is 409 with the current record. The device then merges: it takes
the other device's fields, and keeps the ones it changed itself. memory() keeps records for tests.
The server never reads, logs or indexes a blob. It never gets a key that opens one: the passkeys'
wrapped roots are ciphertext to it. Escrow's operator share is the exception, and only when the
user consented to escrow.
Sync
With sync: true, the store's state is a @qatjs/core/sync document (lists and maps merge, as in
Collaboration). Each operation is sealed before it leaves the device, and the
relay (qatjs dev, @qatjs/sync-server) keeps and passes ciphertext. The merge runs on each device, on
the operations it opened. Offline, writes queue, sealed in IndexedDB, and merge on reconnect. What
the relay still sees: the room's name (vault: and the store's name), how many operations there
are, their sizes and their times.
What the compiler holds a vault to
- Every field is a secret (Secrets). A field's flow to the server, a URL, a log,
storage,
shared()or a scenario is a compile error with its path. That includes<Compare>:scoped(notes.items)isQX-SECRET-IN-SCENARIO, because a vault cannot be branched. Devtools,snapshot()andreport()get no value of it: every field is redacted, and a trace lists the field's id amongredacted, without its value (as it does a password field's). QX-VAULT-IN-SERVER-RENDER. A server or static route that reads a vault store on the server is an error. The server holds no key, so it would render the locked state. Show the store under a test onvault.status. The server renders the other branch as a skeleton, and the browser decrypts:
// src/pages/notes.qx
import { vault } from '@qatjs/vault'
import { notes } from '../notes.js'
export default function Notes() {
return (
<main>
{if (vault.status() === 'unlocked')}
<p>{notes.items.length} notes</p>
{else}
<p class="skeleton">Your notes are locked</p>
{/if}
</main>
)
}QX-VAULT-NO-CSP. A vault is only as safe as the page that opens it: a script injected there reads what the vault decrypts. So for an app with a vault store,qatjs checkandqatjs compressrefuse an index.html that weakens the policy. That covers'unsafe-inline','unsafe-eval', a wildcard or a scheme inscript-src; inline handlers;javascript:URLs; and a script from another origin withoutintegrity. The build writes its strict policy into the page as a<meta>: hashes of what the page inlines,'self', and SRI on what it loads. The page has that policy wherever it is served (dist/csp.jsonstill has the header form, withframe-ancestors).
Not yet
A vault store has no version/migrate: its blob's version is the vault's own. Sharing a vault
with another user, and a client-side encrypted search, are not built yet. What a vault does and does
not protect against: Threat model.