Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Improve vault init efficiency in browser extension (#2159)
Leendert de Borstb1d2b8ef7781
diff --git a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.tsindex 79a2d1888..369475e0c 100644--- a/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts+++ b/apps/browser-extension/src/entrypoints/background/VaultMessageHandler.ts@@ -1,6 +1,7 @@/* eslint-disable @typescript-eslint/no-explicit-any */import { storage } from 'wxt/utils/storage';+import { base64ToBytes } from '@/utils/Base64';import { AUTH_STORAGE_KEYS, dirtyScopeStorageKey, SESSION_STORAGE_KEYS, StorageKeys, vaultDataStorageKeys, VAULT_LOCK_STORAGE_KEYS } from '@/utils/constants/storageKeys';import { TRASH_RETENTION_DAYS } from '@/utils/constants/vault';import type { ItemUsageAction } from '@/utils/db';@@ -1920,12 +1921,7 @@ export async function handleSaveLoginCredential(// First try direct base64 if providedif (message.logoBase64) {try {- const binaryString = atob(message.logoBase64);- const bytes = new Uint8Array(binaryString.length);- for (let i = 0; i < binaryString.length; i++) {- bytes[i] = binaryString.charCodeAt(i);- }- logo = bytes;+ logo = base64ToBytes(message.logoBase64);} catch {// Logo decode failed, continue without logo}diff --git a/apps/browser-extension/src/utils/Base64.ts b/apps/browser-extension/src/utils/Base64.tsnew file mode 100644index 000000000..0b0064a63--- /dev/null+++ b/apps/browser-extension/src/utils/Base64.ts@@ -0,0 +1,35 @@+/**+ * Base64 conversion helpers for raw byte buffers to efficiently encode and decode large buffers.+ */++/**+ * Chunk size for encoding and decoding.+ */+const CHUNK_SIZE = 0x8000;++/**+ * Encode raw bytes as base64 via latin-1 character codes.+ * @param bytes - the bytes to encode+ * @returns The base64 representation of the bytes+ */+export function bytesToBase64(bytes: Uint8Array): string {+ const parts: string[] = [];+ for (let i = 0; i < bytes.length; i += CHUNK_SIZE) {+ parts.push(String.fromCharCode(...bytes.subarray(i, i + CHUNK_SIZE)));+ }+ return btoa(parts.join(''));+}++/**+ * Decode a base64 string back into raw bytes. Counterpart of {@link bytesToBase64}.+ * @param base64 - the base64-encoded bytes+ * @returns The decoded bytes+ */+export function base64ToBytes(base64: string): Uint8Array {+ const binaryString = atob(base64);+ const bytes = new Uint8Array(binaryString.length);+ for (let i = 0; i < binaryString.length; i++) {+ bytes[i] = binaryString.charCodeAt(i);+ }+ return bytes;+}diff --git a/apps/browser-extension/src/utils/EncryptionUtility.ts b/apps/browser-extension/src/utils/EncryptionUtility.tsindex c9758ceb1..2e4038947 100644--- a/apps/browser-extension/src/utils/EncryptionUtility.ts+++ b/apps/browser-extension/src/utils/EncryptionUtility.ts@@ -2,6 +2,7 @@ import { Buffer } from 'buffer';import argon2 from 'argon2-browser/dist/argon2-bundled.min.js';+import { base64ToBytes, bytesToBase64 } from '@/utils/Base64';import { devWarn } from '@/utils/devLogger/DevLogger';import type { EncryptionKey } from '@/utils/dist/core/models/vault';import type { Email, EmailDecryptionKey, MailboxEmail } from '@/utils/dist/core/models/webapi';@@ -77,7 +78,7 @@ export class EncryptionUtility {const key = await crypto.subtle.importKey("raw",- Uint8Array.from(atob(base64Key), c => c.charCodeAt(0)),+ base64ToBytes(base64Key),{name: "AES-GCM",length: 256,@@ -100,11 +101,7 @@ export class EncryptionUtility {combined.set(iv, 0);combined.set(new Uint8Array(ciphertext), iv.length);- return btoa(- Array.from(combined)- .map(byte => String.fromCharCode(byte))- .join('')- );+ return bytesToBase64(combined);}/**@@ -113,7 +110,7 @@ export class EncryptionUtility {public static async symmetricEncryptBytes(plaintextBytes: Uint8Array, base64Key: string): Promise<string> {const key = await crypto.subtle.importKey("raw",- Uint8Array.from(atob(base64Key), c => c.charCodeAt(0)),+ base64ToBytes(base64Key),{name: "AES-GCM",length: 256,@@ -134,11 +131,7 @@ export class EncryptionUtility {combined.set(iv, 0);combined.set(new Uint8Array(ciphertext), iv.length);- return btoa(- Array.from(combined)- .map(byte => String.fromCharCode(byte))- .join('')- );+ return bytesToBase64(combined);}/**@@ -151,7 +144,7 @@ export class EncryptionUtility {const key = await crypto.subtle.importKey("raw",- Uint8Array.from(atob(base64Key), c => c.charCodeAt(0)),+ base64ToBytes(base64Key),{name: "AES-GCM",length: 256,@@ -160,7 +153,7 @@ export class EncryptionUtility {["decrypt"]);- const ivAndCiphertext = Uint8Array.from(atob(base64Ciphertext), c => c.charCodeAt(0));+ const ivAndCiphertext = base64ToBytes(base64Ciphertext);const iv = ivAndCiphertext.slice(0, 12);const ciphertext = ivAndCiphertext.slice(12);@@ -184,7 +177,7 @@ export class EncryptionUtility {const key = await crypto.subtle.importKey("raw",- Uint8Array.from(atob(base64Key), c => c.charCodeAt(0)),+ base64ToBytes(base64Key),{name: "AES-GCM",length: 256,@@ -211,14 +204,14 @@ export class EncryptionUtility {*/public static generateVaultEncryptionKey(): string {const vek = crypto.getRandomValues(new Uint8Array(32));- return btoa(String.fromCharCode(...vek));+ return bytesToBase64(vek);}/*** Encrypts a VEK with a KEK using AES-256-GCM. Returns base64(IV | ciphertext | authTag).*/public static async encryptVaultEncryptionKey(vekBase64: string, kekBase64: string): Promise<string> {- const vekBytes = Uint8Array.from(atob(vekBase64), c => c.charCodeAt(0));+ const vekBytes = base64ToBytes(vekBase64);return this.symmetricEncryptBytes(vekBytes, kekBase64);}@@ -226,9 +219,9 @@ export class EncryptionUtility {* Decrypts an encrypted VEK with a KEK. Returns the VEK as base64.*/public static async decryptVaultEncryptionKey(encryptedVekBase64: string, kekBase64: string): Promise<string> {- const encryptedBytes = Uint8Array.from(atob(encryptedVekBase64), c => c.charCodeAt(0));+ const encryptedBytes = base64ToBytes(encryptedVekBase64);const vekBytes = await this.symmetricDecryptBytes(encryptedBytes, kekBase64);- return btoa(String.fromCharCode(...vekBytes));+ return bytesToBase64(vekBytes);}/**@@ -303,7 +296,7 @@ export class EncryptionUtility {encodedPlaintext);- return btoa(String.fromCharCode.apply(null, Array.from(new Uint8Array(cipherBuffer))));+ return bytesToBase64(new Uint8Array(cipherBuffer));}/**@@ -324,7 +317,7 @@ export class EncryptionUtility {* Decrypts data using RSA-OAEP asymmetric encryption with a CryptoKey private key.*/public static async decryptWithPrivateKeyObject(ciphertext: string, privateKey: CryptoKey): Promise<Uint8Array> {- const cipherBuffer = Uint8Array.from(atob(ciphertext), c => c.charCodeAt(0));+ const cipherBuffer = base64ToBytes(ciphertext);const plaintextBuffer = await crypto.subtle.decrypt({name: "RSA-OAEP",@@ -423,7 +416,7 @@ export class EncryptionUtility {decryptedEmail.fromDomain = await EncryptionUtility.symmetricDecrypt(email.fromDomain, symmetricKeyBase64);decryptedEmail.fromLocal = await EncryptionUtility.symmetricDecrypt(email.fromLocal, symmetricKeyBase64);- const sourceBytes = email.messageSource ? await EncryptionUtility.symmetricDecryptBytes(Uint8Array.from(atob(email.messageSource), c => c.charCodeAt(0)), symmetricKeyBase64) : null;+ const sourceBytes = email.messageSource ? await EncryptionUtility.symmetricDecryptBytes(base64ToBytes(email.messageSource), symmetricKeyBase64) : null;decryptedEmail.messageSource = '';let htmlBody: string | null = null;diff --git a/apps/browser-extension/src/utils/PinUnlockService.ts b/apps/browser-extension/src/utils/PinUnlockService.tsindex a9bd2807a..c5b5dea05 100644--- a/apps/browser-extension/src/utils/PinUnlockService.ts+++ b/apps/browser-extension/src/utils/PinUnlockService.ts@@ -1,6 +1,7 @@import argon2 from 'argon2-browser/dist/argon2-bundled.min.js';import { browser } from 'wxt/browser';+import { base64ToBytes, bytesToBase64 } from '@/utils/Base64';import { StorageKeys } from '@/utils/constants/storageKeys';import { storage } from '#imports';@@ -159,7 +160,7 @@ export async function setupPin(pin: string, vaultEncryptionKey: string): Promisetry {// Generate random saltconst salt = crypto.getRandomValues(new Uint8Array(16));- const saltBase64 = arrayBufferToBase64(salt.buffer);+ const saltBase64 = bytesToBase64(salt);// Derive key from PIN using Argon2idconst combinedSalt = await assembleSaltWithPepper(salt);@@ -177,7 +178,7 @@ export async function setupPin(pin: string, vaultEncryptionKey: string): Promiseconst combined = new Uint8Array(iv.length + encryptedKey.byteLength);combined.set(iv, 0);combined.set(new Uint8Array(encryptedKey), iv.length);- const encryptedKeyBase64 = arrayBufferToBase64(combined.buffer);+ const encryptedKeyBase64 = bytesToBase64(combined);/* Store encrypted key, salt, PIN length, and enable flag */await Promise.all([@@ -228,12 +229,12 @@ export async function unlockWithPin(pin: string): Promise<string> {}// Decode encrypted package- const combined = new Uint8Array(base64ToArrayBuffer(encryptedKeyBase64));+ const combined = base64ToBytes(encryptedKeyBase64);const iv = combined.slice(0, 12);const encryptedData = combined.slice(12);// Derive key from PIN with extension ID pepper- const salt = new Uint8Array(base64ToArrayBuffer(saltBase64));+ const salt = base64ToBytes(saltBase64);const combinedSalt = await assembleSaltWithPepper(salt);const pinKey = await derivePinKey(pin, combinedSalt);@@ -358,7 +359,7 @@ async function assembleSaltWithPepper(randomSalt: Uint8Array): Promise<Uint8Arra*/async function derivePinKey(pin: string, salt: Uint8Array): Promise<CryptoKey> {// Convert salt to base64 string (required by argon2-browser)- const saltBase64 = arrayBufferToBase64(salt.buffer as ArrayBuffer);+ const saltBase64 = bytesToBase64(salt);// Derive key using Argon2idconst hash = await argon2.hash({@@ -382,27 +383,3 @@ async function derivePinKey(pin: string, salt: Uint8Array): Promise<CryptoKey> {return pinKey;}--/**- * Convert ArrayBuffer to base64 string- */-function arrayBufferToBase64(buffer: ArrayBuffer): string {- const bytes = new Uint8Array(buffer);- let binary = '';- for (let i = 0; i < bytes.byteLength; i++) {- binary += String.fromCharCode(bytes[i]);- }- return btoa(binary);-}--/**- * Convert base64 string to ArrayBuffer- */-function base64ToArrayBuffer(base64: string): ArrayBuffer {- const binary = atob(base64);- const bytes = new Uint8Array(binary.length);- for (let i = 0; i < binary.length; i++) {- bytes[i] = binary.charCodeAt(i);- }- return bytes.buffer;-}diff --git a/apps/browser-extension/src/utils/SqliteClient.ts b/apps/browser-extension/src/utils/SqliteClient.tsindex 2154161a1..c07a7f2b6 100644--- a/apps/browser-extension/src/utils/SqliteClient.ts+++ b/apps/browser-extension/src/utils/SqliteClient.ts@@ -1,5 +1,6 @@import initSqlJs from 'sql.js';+import { base64ToBytes, bytesToBase64 } from '@/utils/Base64';import { StorageKeys } from '@/utils/constants/storageKeys';import type { VaultVersion } from '@/utils/dist/core/vault';import { VaultSqlGenerator, checkVersionCompatibility, extractVersionFromMigrationId } from '@/utils/dist/core/vault';@@ -23,6 +24,12 @@ import type { Database } from 'sql.js';import { storage } from '#imports';+/** Minimum number of free pages before a VACUUM is worth the full database rewrite on export. */+const VACUUM_MIN_FREE_PAGES = 64;++/** A VACUUM runs when the freelist holds at least this fraction (1/n) of the database's pages. */+const VACUUM_FREE_PAGE_RATIO = 10;+/*** Core SQLite database client.* Provides low-level database operations and exposes repositories for domain-specific operations.@@ -164,12 +171,7 @@ export class SqliteClient implements IDatabaseClient {*/public async initializeFromBase64(base64String: string): Promise<void> {try {- // Convert base64 to Uint8Array- const binaryString = atob(base64String);- const bytes = new Uint8Array(binaryString.length);- for (let i = 0; i < binaryString.length; i++) {- bytes[i] = binaryString.charCodeAt(i);- }+ const bytes = base64ToBytes(base64String);// Initialize SQL.js with the WASM fileconst SQL = await initSqlJs({@@ -272,22 +274,26 @@ export class SqliteClient implements IDatabaseClient {}try {- // Vacuum to free up space before exporting- this.db.run('VACUUM');-- const binaryArray = this.db.export();-- let binaryString = '';- for (let i = 0; i < binaryArray.length; i++) {- binaryString += String.fromCharCode(binaryArray[i]);- }- return btoa(binaryString);+ this.vacuumIfFragmented();+ return bytesToBase64(this.db.export());} catch (error) {console.error('Error exporting SQLite database:', error);throw error;}}+ /**+ * Rebuild the database to reclaim free pages, but only when enough of them have accumulated to be worth it.+ */+ private vacuumIfFragmented(): void {+ const freePages = this.executeQuery<{ freelist_count: number }>('PRAGMA freelist_count')[0]?.freelist_count ?? 0;+ const totalPages = this.executeQuery<{ page_count: number }>('PRAGMA page_count')[0]?.page_count ?? 0;++ if (freePages >= VACUUM_MIN_FREE_PAGES && freePages * VACUUM_FREE_PAGE_RATIO >= totalPages) {+ this.executeRaw('VACUUM');+ }+ }+/*** Execute a SELECT query.* @param query - SQL query string@@ -612,8 +618,7 @@ export class SqliteClient implements IDatabaseClient {*/private static base64Encode(buffer: Uint8Array | number[] | { [key: number]: number }): string | null {try {- const arr = Array.from(this.toUint8Array(buffer));- return btoa(arr.reduce((data, byte) => data + String.fromCharCode(byte), ''));+ return bytesToBase64(this.toUint8Array(buffer));} catch (error) {console.error('Error encoding to base64:', error);return null;diff --git a/apps/browser-extension/src/utils/VaultCodec.ts b/apps/browser-extension/src/utils/VaultCodec.tsindex 5ca96dd0a..e8ceb2125 100644--- a/apps/browser-extension/src/utils/VaultCodec.ts+++ b/apps/browser-extension/src/utils/VaultCodec.ts@@ -2,6 +2,8 @@* VaultCodec: SQLite materialization methods for the manifest-v1 storage format.*/+import { base64ToBytes as decodeBase64, bytesToBase64 as encodeBase64 } from '@/utils/Base64';+import type {CodecManifest,CodecDataBucket,@@ -138,6 +140,9 @@ export class VaultCodec {db.run('PRAGMA foreign_keys = OFF');db.run('BEGIN TRANSACTION');+ // Compiled INSERT statements, keyed by table name plus the row's column set.+ const statements = new Map<string, import('sql.js').Statement>();+// Tables present in the freshly-created schema; rows for tables outside it cannot be inserted.const schemaTables = new Set<string>(db.exec("SELECT name FROM sqlite_master WHERE type='table'")[0]?.values.map(v => String(v[0])) ?? []);@@ -156,7 +161,6 @@ export class VaultCodec {console.info(`[VaultCodec] Inserting ${rows.length} rows into "${tableName}"...`);for (const row of rows) {const cols = Object.keys(row);- const placeholders = cols.map(() => '?').join(', ');const values: unknown[] = cols.map(c => {const v = row[c];if (this.isBlobRef(v)) {@@ -169,15 +173,28 @@ export class VaultCodec {return (v === undefined ? null : v) as unknown;});- const quotedCols = cols.map(c => `"${c}"`).join(', ');+ const statementKey = `${tableName}\u0000${cols.join('\u0000')}`;+ let statement = statements.get(statementKey);+ if (!statement) {+ const quotedCols = cols.map(c => `"${c}"`).join(', ');+ const placeholders = cols.map(() => '?').join(', ');+ statement = db.prepare(`INSERT INTO "${tableName}" (${quotedCols}) VALUES (${placeholders})`);+ statements.set(statementKey, statement);+ }+try {- db.run(`INSERT INTO "${tableName}" (${quotedCols}) VALUES (${placeholders})`, values as never);+ statement.run(values as never);} catch (e) {throw new Error(`VaultCodec: failed to insert row into "${tableName}" (columns: ${cols.join(', ')}): ${e instanceof Error ? e.message : String(e)}`);}}}+ for (const statement of statements.values()) {+ statement.free();+ }+ statements.clear();+db.run('COMMIT');// 3) Verify referential integrity of the fully-assembled database.@@ -191,11 +208,7 @@ export class VaultCodec {// 4) Export.const bytes = db.export();console.info(`[VaultCodec] Reassembly complete: exported SQLite of ${bytes.length} bytes.`);- let binaryString = '';- for (let i = 0; i < bytes.length; i++) {- binaryString += String.fromCharCode(bytes[i]);- }- return btoa(binaryString);+ return encodeBase64(bytes);} finally {db.close();}@@ -308,7 +321,7 @@ export class VaultCodec {* @param base64 - base64-encoded bytes*/public static base64ToBytes(base64: string): Uint8Array {- return Uint8Array.from(atob(base64), c => c.charCodeAt(0));+ return decodeBase64(base64);}/**@@ -316,11 +329,7 @@ export class VaultCodec {* @param bytes - bytes to encode*/private static bytesToBase64(bytes: Uint8Array): string {- let s = '';- for (let i = 0; i < bytes.length; i++) {- s += String.fromCharCode(bytes[i]);- }- return btoa(s);+ return encodeBase64(bytes);}}diff --git a/apps/browser-extension/src/utils/VaultMergeService.ts b/apps/browser-extension/src/utils/VaultMergeService.tsindex bd3dd904c..1d08dd910 100644--- a/apps/browser-extension/src/utils/VaultMergeService.ts+++ b/apps/browser-extension/src/utils/VaultMergeService.ts@@ -1,6 +1,7 @@import initSqlJs from 'sql.js';import { browser } from 'wxt/browser';+import { base64ToBytes, bytesToBase64 } from '@/utils/Base64';import { TRASH_RETENTION_DAYS } from '@/utils/constants/vault';import { devLog } from '@/utils/devLogger/DevLogger';import type { SqliteClient } from '@/utils/SqliteClient';@@ -275,12 +276,7 @@ export class VaultMergeService {* @returns The loaded Database instance*/private loadDatabase(SQL: SqlJsStatic, base64String: string): Database {- const binaryString = atob(base64String);- const bytes = new Uint8Array(binaryString.length);- for (let i = 0; i < binaryString.length; i++) {- bytes[i] = binaryString.charCodeAt(i);- }- return new SQL.Database(bytes);+ return new SQL.Database(base64ToBytes(base64String));}/**@@ -290,14 +286,7 @@ export class VaultMergeService {*/private exportDatabase(db: Database): string {db.run('VACUUM');- const binaryArray = db.export();- // Convert to string in chunks to avoid O(n²) byte-by-byte concatenation- const chunkSize = 0x8000;- const parts: string[] = [];- for (let i = 0; i < binaryArray.length; i += chunkSize) {- parts.push(String.fromCharCode(...binaryArray.subarray(i, i + chunkSize)));- }- return btoa(parts.join(''));+ return bytesToBase64(db.export());}/**diff --git a/apps/browser-extension/src/utils/VaultSyncService.ts b/apps/browser-extension/src/utils/VaultSyncService.tsindex e22905f19..62ddf6a54 100644--- a/apps/browser-extension/src/utils/VaultSyncService.ts+++ b/apps/browser-extension/src/utils/VaultSyncService.ts@@ -6,6 +6,7 @@import { storage } from 'wxt/utils/storage';+import { base64ToBytes } from '@/utils/Base64';import { bucketRevisionKey, StorageKeys } from '@/utils/constants/storageKeys';import { devError, devLog, devWarn } from '@/utils/devLogger/DevLogger';import { ManifestKeyType, VaultKeyAlgorithm, type VaultResponse } from '@/utils/dist/core/models/webapi';@@ -80,7 +81,7 @@ async function verifyDecryptUnpack(base64Ciphertext: string, vek: string, expectif (expectedCiphertextHash && await vaultCodecComputeCiphertextHash(base64Ciphertext) !== expectedCiphertextHash) {throw new Error(`VaultSyncService: ${label} ciphertext hash mismatch, refusing to load. Possible storage corruption.`);}- const encryptedBytes = Uint8Array.from(atob(base64Ciphertext), c => c.charCodeAt(0));+ const encryptedBytes = base64ToBytes(base64Ciphertext);const plainBytes = await EncryptionUtility.symmetricDecryptBytes(encryptedBytes, vek);return vaultCodecUnpackPayload(plainBytes);}