Repository navigation
Add WebAssembly support: browser keyrings, an in-memory backend and WASI - #8
Merged
Merged
Conversation
added 8 commits
November 20, 2026 14:30
TursoKeyring can now adopt an already-open connection (from_connection) or open through a caller-supplied turso IO (with_io), so a browser worker can keep the keyring's secrets table next to its own tables in one OPFS database. Rekey needs cryptex to own the storage, so it reports a clear error for adopted keyrings. turso_common::block_on no longer parks when built for wasm: it polls a bounded number of times and fails instead of spinning forever. The memory backend keeps secrets encrypted in RAM and seals them into a single AEAD blob for export/import, which is the base for browser storage and the JS bindings. CipherAlgorithm moved to its own module so it no longer drags in the page cipher. cargo check --target wasm32-unknown-unknown --no-default-features --features turso passes; a wasm CI job covers it.
acquire_single_opener_lock treats an Unsupported lock as success, which is what WASI reports, and the default keyring directory there is ./.cryptex instead of a home directory that does not exist. Criterion is native-only (Rayon does not build for wasm) and the CLI is split into src/native.rs so a wasm build of the binary compiles and prints that it is unsupported. cargo test --target wasm32-wasip1 --no-default-features --features turso,memory passes under wasmtime.
Turso's file traffic now goes through a PageStore trait, so the page cipher works over a native file, an in-memory buffer or (next) an OPFS handle. On top of it, a Turso IO keeps every database and WAL file in memory, reads and writes complete in place, and the clock comes from the JavaScript runtime: the default clock calls Instant::now, which panics on wasm. That gives TursoKeyring::in_memory, from_bytes and export, which is how a browser persists a keyring without a filesystem.
IndexedDB keeps one encrypted image per keyring and takes a Web Lock named after it, so a second tab fails instead of overwriting the first. localStorage holds the same image for synchronous code that cannot await, and OPFS gives a worker a real file, opened through sync access handles so Turso never waits on a promise it cannot park for.
Keyring.open picks a backend (a Turso database or a plain memory image) and a store (IndexedDB, localStorage, OPFS or none), so a page can keep secrets without knowing which of them it needs. set and delete resolve once the image reached storage; export copies it out. Also add a browser example: IndexedDB from the page, OPFS from a worker.
…lose An in-memory keyring opened its database under one fixed path, so two of them shared a page cache and an encryption key. Give each its own path, reopen an imported image under the path it was written as, and let a rekey copy into a fresh database instead of replacing the one in place. Closing a JavaScript keyring now drops the underlying database, which is what releases an OPFS sync access handle or an IndexedDB lock, and the flush no longer tries to export an OPFS keyring that has no image.
The memory backend writes its sealed image to IndexedDB too, so the only difference from the default is that there is no database underneath. That also makes the localStorage path the same code, and asking for OPFS with a memory keyring is refused instead of silently losing the store. Closing a keyring now waits for the Web Lock to actually be released: the release is asynchronous, so reopening the same name in the same page could otherwise be refused. Browser tests for the bindings join the repository as tests/wasm.rs, run with .
The README gains the JavaScript API, the backend/store combinations, what each store persists, the Web Lock that keeps two tabs from fighting over one keyring, and the costs: any script on the origin can read browser storage, the browser may evict it, and Argon2 blocks the thread it runs on. CI builds the wasm package and runs tests/wasm.rs in headless Chrome, and the version moves to 2.5.0.
dougcooper
marked this pull request as draft
November 20, 2026 20:51
added 2 commits
November 20, 2026 16:02
The example pre-filled a password with spaces, which the whitespace-separated connection string cannot carry, so Open failed with a parser byte offset. Use a password without spaces and say why if one is typed. Reading a missing secret then failed loudly too: the kind is in error.message, not in String(error), which prepends "Error: ".
The page could read a secret but not ask a keyring what it holds. Add TursoKeyring::keys and MemoryKeyring::keys, and SnapshotStore::list_names, which reads IndexedDB without taking the lock, then expose them to JavaScript as keyring.keys() and storedKeyrings(). The example page shows both lists and opens a keyring when its name is clicked.
mikelodder7
approved these changes
Nov 20, 2026
added 4 commits
November 20, 2026 16:46
storedKeyrings now takes a store: IndexedDB is still the default, and localStorage and OPFS each have their own listing. Neither reads an image, so both are safe while a tab holds the keyring open, and listing OPFS works from a page even though opening one needs a worker. The example page shows all three stores, tags each row with the store it came from, and can open a keyring from either store the page can reach.
Turso's process-wide registry is keyed by the IO's file_id, so two IOs holding a same-named file resolved to one open database: a keyring opened from an image could read and write another keyring's secrets under the other's key. Give every IO an id of its own, and open a restored image under a fresh path instead of the one it recorded. The snapshot reader also trusted a file count from an unauthenticated header to size a map; a fifteen-byte input aborted the process. Refuse a count that is not the database and its WAL, and check the stored value is an image before converting it.
An imported blob carried its Argon2 costs in an unauthenticated header, so a crafted image could demand terabytes and minutes of work before the AEAD noticed. Bound them before deriving, and check the salt length with checked arithmetic: on wasm32 the old check wrapped and a debug build panicked slicing the blob. On the browser side, a closed keyring now refuses every call instead of silently accepting writes that go nowhere, a localStorage keyring takes the same single-opener Web Lock as IndexedDB, an aborted transaction rejects instead of hanging, and a repeated connection-string keyword is an error rather than a silent override. Secret buffers and the AEAD key schedules are zeroized.
The explicit feature lists replaced --all-features, which quietly stopped linting, testing and documenting the YubiHSM backends; list them too. Give the workflow a read-only token and build wasm-pack from a pinned version instead of piping an unpinned installer into a shell. The README now says what the encryption does not cover: a stale image is not detected, and the connection string is a small DSL, so a password with whitespace or quotes in it is not passed through unchanged.
dougcooper
marked this pull request as ready for review
November 20, 2026 22:38
TursoKeyring carried the store and an Option<Arc<WebIo>> that was Some exactly when cryptex made the database itself. Two fields, one meaning: export and copy_under branched on the flag, each constructor patched it afterwards, and on a native build the field was always None. Make it one enum: Stored holds a file or a caller's connection, InMemory holds the database cryptex made plus the IO that can copy it out. export is now a match arm rather than a runtime check, and the two modes cannot drift. Also rename the opfs feature to turso-opfs. It is a Turso IO, not a store that layers over export(), so it belongs beside turso-encrypted-io rather than beside indexeddb and web-storage. The JS store name is unchanged.
mikelodder7
approved these changes
Nov 21, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KiuMTUBySDSj38NVDFgn2b
argon2 0.6, dirs 7, rusqlite 0.43, sqlite-plugin 0.11, turso 0.9.0-pre.6. bindgen stays at 0.72 to match sqlite-plugin, so cargo-outdated ignores it. The argon2 example test now builds into its own target dir; sharing the unhashed rlib dropped memory and turso from the one doctests link. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KiuMTUBySDSj38NVDFgn2b
…river windows.rs uses as_chunks for the UTF-16 decode (chunks_exact_to_as_chunks). file_common doctests import file_common::ConnectionParams, which exists without the file feature. The browser tests use the runner's ChromeDriver, which matches its Chrome; setup-chrome installed a newer driver. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KiuMTUBySDSj38NVDFgn2b
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
cryptexhad no backend that could run in a browser: the OS keyrings need an OS,fileandencrypted-vfsare SQLCipher plus a bundled C VFS, and YubiHSM needs USB. This adds WASM support for bothwasm32-unknown-unknownand WASI, with the browser as the primary target and persistence built in rather than left to the caller.This addresses #7: a
TursoKeyringcan now adopt a caller's connection or run through a caller'sIO, which is what a consumer that already has an OPFS-backedturso_core::IOin a worker needs.What is new
wasm32-unknown-unknownmemoryexport/importtursoturso-encrypted-ioweb-storageMemoryKeyringinlocalStorageindexeddbopfsjswasm-bindgenKeyringclassfile,encrypted-vfs, OS keyrings,yubihsm-*Rust API
MemoryKeyring:with_params,import,export,rekey. The blob carries the salt, cipher and Argon2 parameters, the header is the AAD, and it is hand-encoded rather than serde.TursoKeyring:from_connectionandwith_io(addresses turso keyring cannot target wasm32-unknown-unknown: expose an IO/connection seam #7),in_memory,from_bytes,export,copy_under(non-destructive rekey),open_opfs.cryptex::indexeddb::SnapshotStore,cryptex::web_storage,cryptex::opfs.PageStoretrait underpage_cipher, so the encrypted page layout[ct(4096)|nonce(12)|tag(16)]is shared by files, memory and OPFS and the bytes stay compatible across them.JavaScript
Options are
{ backend: "turso"|"memory", store: "indexeddb"|"opfs"|"localstorage"|"memory" }, defaulting to an encrypted Turso image in IndexedDB that is written after every resolvedset/deleteand guarded by a Web Lock, so a second tab is refused withAccessDeniedinstead of overwriting.rekeyis available except on OPFS, where the database cannot be rewritten in place. Errors areErrorwhose message starts with the kind.examples/web/has an IndexedDB page and an OPFS worker.Also
block_ongains a bounded-poll wasm path (Turso's futures must complete in place; there is no thread to park), and a JavaScript-backedClock, because Turso'sDefaultClockcallsInstant::now(), which panics onwasm32-unknown-unknown.try_lockreportingUnsupportedis treated as success and the default directory is./.cryptex; the CLI is native-only and says so on wasm.getrandomuses thewasm_jsbackend (set in.cargo/config.toml), with CI coverage forwasm32-unknown-unknownand both WASI targets.Verification
-D warningsoverfile,serde,memory,turso,turso-encrypted-iowasm-pack test --headless --chrome: 3 binding tests intests/wasm.rscargo fmt --checkcleanNotes for the reviewer
2.5.0.--all-featurescan no longer build (the browser features are wasm-only by design), so CI and docs.rs name their features explicitly.createSyncAccessHandle(), which browsers expose only in a worker, sostore: "opfs"is refused on the main thread.localStorage, so the encryption protects against other origins and users, not a compromised page, and the browser may evict the data unlessnavigator.storage.persist()is granted.