Secure storage
A secret the operating system protects, as opposed to one a file protects. Registered on macOS,
Windows and Linux under keychain, so a page can call it.
await window.crust.invoke("keychain", "set", {
service: "land.jyz.notes",
account: "sync-token",
secret: "…",
});
const { found, secret } = await window.crust.invoke("keychain", "get", {
service: "land.jyz.notes",
account: "sync-token",
});
stronghold is the other half of this story: one AES-256-GCM file under a password the app supplies.
That protects a file, and a lost password loses the data — a leaked one leaks it. A keychain entry is
unlocked by the login session, has no app-held password to lose, and appears in Keychain Access or
Credential Manager, where a user can revoke it. Reach for keychain when the OS owns the machine, and
stronghold when the secret must travel with a data file.
Upgrading stronghold: its vault moved from the machine-wide ~/Library/Application Support/crust/stronghold.json to one file per app under that app's data directory. The legacy shared file is not migrated because its entries carry no app identity, so they cannot be attributed to one app without preserving the leak. Any secret stored before this version must be set again. When the first launch creates a fresh per-app vault while the legacy file still exists, crust writes one warn line naming both paths.
Commands
| command | arguments | answer |
|---|---|---|
set | service, account, secret | { ok: true, stored: true } |
get | service, account | { ok: true, found: true, secret } or { ok: true, found: false } |
delete | service, account | { ok: true, deleted: boolean } |
list | service | { ok: true, accounts: string[] } |
The list answer above is what a platform that binds enumeration returns. macOS binds
SecItemCopyMatching and Windows binds CredEnumerateW, so list works on both. Linux binds no
enumerator, so list refuses with not-implemented naming secret_service_search_sync; set, get
and delete work on all three platforms.
A missing credential is { found: false }, never an error. "You have not stored this" is a normal
answer, and a refusal a caller must catch would make a first launch look broken.
delete answers deleted: false when nothing was stored, which is the same success: the entry the
caller named is gone. An empty secret is a real secret, so only a non-string is refused.
Both service and account must be non-empty and may not contain a NUL — a NUL would truncate the
name inside the C string the OS is handed, which is how two credentials could collapse onto one entry.
On Windows neither may contain a backslash either: CredWriteW keys a generic credential on one
service\account target, so ("a", "b\c") and ("a\b", "c") would otherwise be the same entry.
A secret may be empty but may not contain a NUL on any platform — libsecret takes a C string on
Linux, so a NUL-bearing secret would come back as only its prefix there. Refuse it uniformly.
Ad-hoc builds cannot use the keychain
crust build ad-hoc signs a macOS app; that signature carries no application-identifier
entitlement, so the Keychain driver refuses the write. keychain.set then answers
{ ok: false, code: "handler-error", message: … } naming the OS refusal, and nothing is stored. A
later keychain.get answers { found: false } — do not read that as "nothing stored yet". Check
the answer of set: found: false after a refused write means the credential store is unavailable,
not that a first launch simply has no entry.
There is no framework-level plaintext fallback. crust never writes a 0600 file and calls it a
keychain. If a secret must work in an ad-hoc build, stronghold is the right tool — an AES-256-GCM
file under a password the app supplies, which is a different contract from an OS-managed credential.
To use the macOS keychain, ship a bundle signed with a Developer ID and the entitlement. Windows and
Linux have no application-identifier entitlement; both work from an ad-hoc build.
What each platform uses
- macOS —
SecItemUpdatethenSecItemAdd, read withSecItemCopyMatching. Neversecurity add-generic-password: a shell argument puts the secret in the process list for the life of the write, which is the shape this plugin exists to avoid. - Windows —
CredWriteW,CredReadW,CredDeleteWinadvapi32, resolved at runtime like every other entry point.listenumerates accounts throughCredEnumerateWfrom the same unit, returning a sortedstring[]and an empty array for an empty store; aNULLfilter keeps*and?literal. - Linux — libsecret's synchronous simple API over the same dlopen'd C shim GTK and WebKitGTK use.
The
*_synccalls make their ownGMainContext/GMainLoop, so crust needs no running loop — onlylibsecret-1.so.0and a Secret Service provider (gnome-keyring, ksecretservice or KWallet) on the session bus. No GLib allocation crosses the FFI boundary: the C half copies the secret into a static buffer and frees theGErroritself, so a GLib string is never handed to Bun's allocator. The library is opened on the first command, not at import; a missing library or symbol becomeshandler-errornaming libsecret and the install line — Debian/Ubuntulibsecret-1-0 gnome-keyring, Fedoralibsecret gnome-keyring.listis not bound, so it answersnot-implementednamingsecret_service_search_sync.
Failure codes
| code | when |
|---|---|
bad-request | service or account empty or NUL-bearing, a backslash in either on Windows, a NUL in secret, or a non-string secret |
not-implemented | no adapter on this platform, or list on Linux; requires names what is missing |
handler-error | the OS refused the read, write or delete — a locked keychain, a denied prompt, or Linux's missing libsecret/provider |
On macOS and Windows a secret is read into a caller-sized buffer and re-read at its exact length when
the first buffer was too small; if it keeps growing across three reads the command refuses rather
than decoding a clamped prefix. Linux uses a fixed 64 KiB static buffer in the C shim and refuses a
larger secret with handler-error, and a NUL-bearing secret is refused on every platform; no
platform silently truncates a credential.