Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
feat(security): replace safeStorage trust anchor with OS keychain + AES-256-GCM
Gabriel Stein25b19245e0e1
diff --git a/electron-builder.json b/electron-builder.jsonindex 3c974479d..80d70ad87 100644--- a/electron-builder.json+++ b/electron-builder.json@@ -87,7 +87,8 @@"asarUnpack": ["**/node_modules/ffmpeg-static/**/*","**/node_modules/better-sqlite3/**/*",- "**/node_modules/onnxruntime-node/**/*"+ "**/node_modules/onnxruntime-node/**/*",+ "**/node_modules/@napi-rs/keyring/**/*"],"extraResources": [".env",diff --git a/package-lock.json b/package-lock.jsonindex ccf4374a2..50a99e598 100644--- a/package-lock.json+++ b/package-lock.json@@ -18,6 +18,7 @@"@ai-sdk/groq": "^3.0.29","@ai-sdk/openai": "^3.0.41","@aws-sdk/credential-providers": "^3.1029.0",+ "@napi-rs/keyring": "^1.3.0","@qdrant/js-client-rest": "^1.12.0","@radix-ui/react-accordion": "^1.1.2","@radix-ui/react-dialog": "^1.1.14",@@ -2048,6 +2049,240 @@"node": ">= 10.0.0"}},+ "node_modules/@napi-rs/keyring": {+ "version": "1.3.0",+ "resolved": "https://registry.npmjs.org/@napi-rs/keyring/-/keyring-1.3.0.tgz",+ "integrity": "sha512-WrOw/bcXm0f9qHkumlT1QlArXSTWqaY9sunsDpOk+yCCorCKMxvWT/a3xko4EYHVdeZoh00yI2TydXn6eyICDA==",+ "license": "MIT",+ "engines": {+ "node": ">= 10"+ },+ "funding": {+ "type": "github",+ "url": "https://github.com/sponsors/Brooooooklyn"+ },+ "optionalDependencies": {+ "@napi-rs/keyring-darwin-arm64": "1.3.0",+ "@napi-rs/keyring-darwin-x64": "1.3.0",+ "@napi-rs/keyring-freebsd-x64": "1.3.0",+ "@napi-rs/keyring-linux-arm-gnueabihf": "1.3.0",+ "@napi-rs/keyring-linux-arm64-gnu": "1.3.0",+ "@napi-rs/keyring-linux-arm64-musl": "1.3.0",+ "@napi-rs/keyring-linux-riscv64-gnu": "1.3.0",+ "@napi-rs/keyring-linux-x64-gnu": "1.3.0",+ "@napi-rs/keyring-linux-x64-musl": "1.3.0",+ "@napi-rs/keyring-win32-arm64-msvc": "1.3.0",+ "@napi-rs/keyring-win32-ia32-msvc": "1.3.0",+ "@napi-rs/keyring-win32-x64-msvc": "1.3.0"+ }+ },+ "node_modules/@napi-rs/keyring-darwin-arm64": {+ "version": "1.3.0",+ "resolved": "https://registry.npmjs.org/@napi-rs/keyring-darwin-arm64/-/keyring-darwin-arm64-1.3.0.tgz",+ "integrity": "sha512-pl76hJvdYUBn6I24bXiOBMA9nbDapo3I5B+f3OorjDU4dUMSypXeKbOVehJe8fhgTiH24flMyTS3aAIy43xegQ==",+ "cpu": [+ "arm64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "darwin"+ ],+ "engines": {+ "node": ">= 10"+ }+ },+ "node_modules/@napi-rs/keyring-darwin-x64": {+ "version": "1.3.0",+ "resolved": "https://registry.npmjs.org/@napi-rs/keyring-darwin-x64/-/keyring-darwin-x64-1.3.0.tgz",+ "integrity": "sha512-YcJtEV5LA3cvA4z3BurgxH5IhTsW1JfIvcAAcqcecwk06Si9F9NqkxbZVIfDwQ8oRHgaBmT3zZJnLAotCrVahw==",+ "cpu": [+ "x64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "darwin"+ ],+ "engines": {+ "node": ">= 10"+ }+ },+ "node_modules/@napi-rs/keyring-freebsd-x64": {+ "version": "1.3.0",+ "resolved": "https://registry.npmjs.org/@napi-rs/keyring-freebsd-x64/-/keyring-freebsd-x64-1.3.0.tgz",+ "integrity": "sha512-vlLf31TGhfRAaxLDBhg8b89ss0HHD/lyNmL5F3UjSaz5CUXElsJmKYq9fqA/B+cZKUEUcLHHGhF0I/CqcFdaVw==",+ "cpu": [+ "x64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "freebsd"+ ],+ "engines": {+ "node": ">= 10"+ }+ },+ "node_modules/@napi-rs/keyring-linux-arm-gnueabihf": {+ "version": "1.3.0",+ "resolved": "https://registry.npmjs.org/@napi-rs/keyring-linux-arm-gnueabihf/-/keyring-linux-arm-gnueabihf-1.3.0.tgz",+ "integrity": "sha512-KiWdMMu/Inz/bHHIAGrnF7r54FZDYXuHO6UFF/rhIrshUsxbMG1Rl9lEymNtqqsVo927G0VYcb02FzWQ3iBQRQ==",+ "cpu": [+ "arm"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">= 10"+ }+ },+ "node_modules/@napi-rs/keyring-linux-arm64-gnu": {+ "version": "1.3.0",+ "resolved": "https://registry.npmjs.org/@napi-rs/keyring-linux-arm64-gnu/-/keyring-linux-arm64-gnu-1.3.0.tgz",+ "integrity": "sha512-eyKGpY40lm9Jvs1aD294XRH4y7+TlJM0YVAryZeXA6TX0mb4gMkxVXwSQv7MCwgah7raeUd0dKUb4BPAYIgcMg==",+ "cpu": [+ "arm64"+ ],+ "libc": [+ "glibc"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">= 10"+ }+ },+ "node_modules/@napi-rs/keyring-linux-arm64-musl": {+ "version": "1.3.0",+ "resolved": "https://registry.npmjs.org/@napi-rs/keyring-linux-arm64-musl/-/keyring-linux-arm64-musl-1.3.0.tgz",+ "integrity": "sha512-iIK6JWHXAJqDrEyLY3TmswwloVyt2vj+04TZnew+uSJ9gnDO8EwRbp3/iw3LpWaXiDO7VomGO6y8I0Id8uBZSw==",+ "cpu": [+ "arm64"+ ],+ "libc": [+ "musl"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">= 10"+ }+ },+ "node_modules/@napi-rs/keyring-linux-riscv64-gnu": {+ "version": "1.3.0",+ "resolved": "https://registry.npmjs.org/@napi-rs/keyring-linux-riscv64-gnu/-/keyring-linux-riscv64-gnu-1.3.0.tgz",+ "integrity": "sha512-/PGqrwn6EwgtK6vccASSXJRfOSP4vN1F4ASsIQ+7MdrK6hNvAJ1FZPrIuD5gGGdxezo3F++To2Wq7DbuGIeuNQ==",+ "cpu": [+ "riscv64"+ ],+ "libc": [+ "glibc"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">= 10"+ }+ },+ "node_modules/@napi-rs/keyring-linux-x64-gnu": {+ "version": "1.3.0",+ "resolved": "https://registry.npmjs.org/@napi-rs/keyring-linux-x64-gnu/-/keyring-linux-x64-gnu-1.3.0.tgz",+ "integrity": "sha512-2PDK1WKWTu9lBGq9VvNEkSlQD3O7YwVpmnyN2M3cy4v7NJ/8gDMd9GXv3G+FVXN13uhp4gnnPBS+ScefmEeD2A==",+ "cpu": [+ "x64"+ ],+ "libc": [+ "glibc"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">= 10"+ }+ },+ "node_modules/@napi-rs/keyring-linux-x64-musl": {+ "version": "1.3.0",+ "resolved": "https://registry.npmjs.org/@napi-rs/keyring-linux-x64-musl/-/keyring-linux-x64-musl-1.3.0.tgz",+ "integrity": "sha512-oJ2HkX8YUo46QBkn0pG+HuIKQNqr523q6vBobCn+P95s4C4K6/kLBqHY/1bg5J4ap31DzsznhnFKcfBNBsjCnw==",+ "cpu": [+ "x64"+ ],+ "libc": [+ "musl"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">= 10"+ }+ },+ "node_modules/@napi-rs/keyring-win32-arm64-msvc": {+ "version": "1.3.0",+ "resolved": "https://registry.npmjs.org/@napi-rs/keyring-win32-arm64-msvc/-/keyring-win32-arm64-msvc-1.3.0.tgz",+ "integrity": "sha512-tOd3c/uAaeoE4ycVlmAdSvygz0Zt3zdca6Y7gokBeIbaRDWpjDIUOpU3MvML59XAaqyuKGsVVu0F/DZb1lHPmw==",+ "cpu": [+ "arm64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "win32"+ ],+ "engines": {+ "node": ">= 10"+ }+ },+ "node_modules/@napi-rs/keyring-win32-ia32-msvc": {+ "version": "1.3.0",+ "resolved": "https://registry.npmjs.org/@napi-rs/keyring-win32-ia32-msvc/-/keyring-win32-ia32-msvc-1.3.0.tgz",+ "integrity": "sha512-sPSqeAFZMGqP1R++M2JTza7GQJJ/TpCo6JU6Vcd4jnebvOaEDs9b7eipakU1PJdSvhpC2yXMCNRk9gXfrhuwHQ==",+ "cpu": [+ "ia32"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "win32"+ ],+ "engines": {+ "node": ">= 10"+ }+ },+ "node_modules/@napi-rs/keyring-win32-x64-msvc": {+ "version": "1.3.0",+ "resolved": "https://registry.npmjs.org/@napi-rs/keyring-win32-x64-msvc/-/keyring-win32-x64-msvc-1.3.0.tgz",+ "integrity": "sha512-4DnCWXwDc0HRKwyRlG5y0VhKZW2tNRQfKKfyj6IX/KWfDNyq9hn4n+GL1auyDcOO/v8PwnhmYo2+rOOqCkvvOg==",+ "cpu": [+ "x64"+ ],+ "license": "MIT",+ "optional": true,+ "os": [+ "win32"+ ],+ "engines": {+ "node": ">= 10"+ }+ },"node_modules/@napi-rs/wasm-runtime": {"version": "1.1.3","dev": true,@@ -12337,6 +12572,7 @@},"node_modules/usocket": {"version": "0.3.0",+ "hasInstallScript": true,"license": "ISC","optional": true,"dependencies": {diff --git a/package.json b/package.jsonindex efd0b31b4..90f3ebd80 100644--- a/package.json+++ b/package.json@@ -119,6 +119,7 @@"@ai-sdk/groq": "^3.0.29","@ai-sdk/openai": "^3.0.41","@aws-sdk/credential-providers": "^3.1029.0",+ "@napi-rs/keyring": "^1.3.0","@qdrant/js-client-rest": "^1.12.0","@radix-ui/react-accordion": "^1.1.2","@radix-ui/react-dialog": "^1.1.14",diff --git a/src/helpers/environment.js b/src/helpers/environment.jsindex ed9b5369f..0629cab1b 100644--- a/src/helpers/environment.js+++ b/src/helpers/environment.js@@ -1,9 +1,10 @@const path = require("path");const fs = require("fs");const fsPromises = require("fs/promises");-const { app, safeStorage } = require("electron");+const { app } = require("electron");const debugLogger = require("./debugLogger");const { normalizeUiLanguage } = require("./i18nMain");+const secretCrypto = require("./secretCrypto");const SECRET_KEYS = ["OPENAI_API_KEY",@@ -86,11 +87,11 @@ class EnvironmentManager {}}- // safeStorage requires app.whenReady(), so init runs after construction.+ // Encryption init runs after construction; safeStorage fallback needs app.whenReady().async init() {if (!this._encryptionAvailable()) {debugLogger.warn(- "safeStorage unavailable — secrets remain in plaintext .env",+ "Secret encryption unavailable — secrets remain in plaintext .env",{ platform: process.platform },"environment");@@ -109,7 +110,7 @@ class EnvironmentManager {_encryptionAvailable() {try {- return safeStorage.isEncryptionAvailable();+ return secretCrypto.isAvailable();} catch {return false;}@@ -131,7 +132,9 @@ class EnvironmentManager {const filePath = this._getSecretFilePath(envVarName);try {const buffer = await fsPromises.readFile(filePath);- process.env[envVarName] = safeStorage.decryptString(buffer);+ const { value, needsReencrypt } = secretCrypto.decrypt(buffer);+ process.env[envVarName] = value;+ if (needsReencrypt) await this._saveSecretKey(envVarName, value);} catch (error) {if (error.code === "ENOENT") return;debugLogger.error(@@ -155,7 +158,7 @@ class EnvironmentManager {const filePath = this._getSecretFilePath(envVarName);const tmpPath = `${filePath}.tmp`;- const encrypted = safeStorage.encryptString(value);+ const encrypted = secretCrypto.encrypt(value);await fsPromises.writeFile(tmpPath, encrypted);await fsPromises.rename(tmpPath, filePath);@@ -189,7 +192,7 @@ class EnvironmentManager {await this._saveSecretKey(name, value);// Round-trip verify before stripping plaintext .env.const buffer = await fsPromises.readFile(this._getSecretFilePath(name));- if (safeStorage.decryptString(buffer) !== value) {+ if (secretCrypto.decrypt(buffer).value !== value) {throw new Error(`round-trip verification failed for ${name}`);}migrated.push(name);diff --git a/src/helpers/secretCrypto.js b/src/helpers/secretCrypto.jsnew file mode 100644index 000000000..95b597917--- /dev/null+++ b/src/helpers/secretCrypto.js@@ -0,0 +1,99 @@+const crypto = require("crypto");+const { safeStorage } = require("electron");+const debugLogger = require("./debugLogger");++const SERVICE = "OpenWhispr";+const ACCOUNT = "secrets-master-key";+const ALGO = "aes-256-gcm";+const IV_LEN = 12;+const TAG_LEN = 16;+const KEY_LEN = 32;++let mode = null;+let masterKey = null;++function _initKeychain() {+ let Entry;+ try {+ ({ Entry } = require("@napi-rs/keyring"));+ } catch (error) {+ debugLogger.warn("@napi-rs/keyring failed to load", { error: error?.message }, "secretCrypto");+ return false;+ }++ try {+ const entry = new Entry(SERVICE, ACCOUNT);+ let stored = null;+ try {+ stored = entry.getPassword();+ } catch {}+ if (stored) {+ const key = Buffer.from(stored, "base64");+ if (key.length !== KEY_LEN) throw new Error("stored key length invalid");+ masterKey = key;+ } else {+ masterKey = crypto.randomBytes(KEY_LEN);+ entry.setPassword(masterKey.toString("base64"));+ }+ return true;+ } catch (error) {+ debugLogger.warn(+ "OS keychain unavailable — falling back to safeStorage",+ { error: error?.message, platform: process.platform },+ "secretCrypto"+ );+ return false;+ }+}++function _ensureInit() {+ if (mode) return;+ if (_initKeychain()) {+ mode = "keychain";+ return;+ }+ mode = safeStorage.isEncryptionAvailable() ? "safeStorage" : "unavailable";+}++function isAvailable() {+ _ensureInit();+ return mode !== "unavailable";+}++function encrypt(plaintext) {+ _ensureInit();+ if (mode === "keychain") {+ const iv = crypto.randomBytes(IV_LEN);+ const cipher = crypto.createCipheriv(ALGO, masterKey, iv);+ const ct = Buffer.concat([cipher.update(plaintext, "utf8"), cipher.final()]);+ return Buffer.concat([iv, cipher.getAuthTag(), ct]);+ }+ if (mode === "safeStorage") return safeStorage.encryptString(plaintext);+ throw new Error("no encryption backend available");+}++function decrypt(blob) {+ _ensureInit();+ if (mode === "keychain" && blob.length > IV_LEN + TAG_LEN) {+ try {+ const iv = blob.subarray(0, IV_LEN);+ const tag = blob.subarray(IV_LEN, IV_LEN + TAG_LEN);+ const ct = blob.subarray(IV_LEN + TAG_LEN);+ const decipher = crypto.createDecipheriv(ALGO, masterKey, iv);+ decipher.setAuthTag(tag);+ const value = Buffer.concat([decipher.update(ct), decipher.final()]).toString("utf8");+ return { value, needsReencrypt: false };+ } catch {+ // Legacy safeStorage blob — handled by the fallback below.+ }+ }+ if (safeStorage.isEncryptionAvailable()) {+ return {+ value: safeStorage.decryptString(blob),+ needsReencrypt: mode === "keychain",+ };+ }+ throw new Error("decryption failed: no backend available");+}++module.exports = { encrypt, decrypt, isAvailable };diff --git a/src/helpers/tokenStore.js b/src/helpers/tokenStore.jsindex dd723c210..2d9a97114 100644--- a/src/helpers/tokenStore.js+++ b/src/helpers/tokenStore.js@@ -1,7 +1,8 @@-const { safeStorage, app } = require("electron");+const { app } = require("electron");const fs = require("fs");const path = require("path");const debugLogger = require("./debugLogger");+const secretCrypto = require("./secretCrypto");const tokenFile = () => path.join(app.getPath("userData"), "auth-token.bin");@@ -13,9 +14,13 @@ function get() {const file = tokenFile();if (!fs.existsSync(file)) return (cached = "");const buf = fs.readFileSync(file);- cached = safeStorage.isEncryptionAvailable()- ? safeStorage.decryptString(buf)- : buf.toString("utf8");+ if (!secretCrypto.isAvailable()) {+ cached = buf.toString("utf8");+ return cached || null;+ }+ const { value, needsReencrypt } = secretCrypto.decrypt(buf);+ cached = value;+ if (needsReencrypt) set(value);return cached || null;} catch (err) {debugLogger.error("tokenStore.get failed", { error: err?.message });@@ -27,8 +32,8 @@ function get() {function set(token) {try {const file = tokenFile();- const data = safeStorage.isEncryptionAvailable()- ? safeStorage.encryptString(token)+ const data = secretCrypto.isAvailable()+ ? secretCrypto.encrypt(token): Buffer.from(token, "utf8");fs.writeFileSync(file, data, { mode: 0o600 });cached = token;diff --git a/src/stores/settingsStore.ts b/src/stores/settingsStore.tsindex d0723ebad..0cb7561ce 100644--- a/src/stores/settingsStore.ts+++ b/src/stores/settingsStore.ts@@ -577,7 +577,7 @@ function debouncedSaveSecret(provider: SecretProvider, key: string) {| undefined;save?.(key)?.catch((err) => {logger.warn(- "Failed to persist secret to safeStorage",+ "Failed to persist secret",{ provider, error: (err as Error).message },"settings");