fix(mail-index): close handle on every pragma failure, reconcile contact/file deletes
QA pass on the encrypted mail index found two real gaps beyond what the
prior end-to-end fix pass caught:
1. store.ts's MailIndex.open() only wrapped SOME of the post-key pragma
calls in a try/catch before this: the first attempt's `key` pragma and
assertEncrypted() ran outside any try at all, and the wrong-key retry
repeated the same gap. Any pragma throwing there (SQLITE_BUSY, a full
disk on the first WAL write) leaked the native SQLite handle instead of
closing it. Factored the open+key+verify+pragma sequence into openKeyed(),
which guarantees a close before rethrowing on any failure, and reused it
for both the first attempt and the retry.
2. reindex.ts never removed a deleted contact or file from the index. The
`removed` field exists in the API and is fully tested at the store layer,
but nothing in the renderer populates it, so a deleted contact/file stayed
searchable - and retrievable by the AI feature - indefinitely. Mail and
calendar can't use the same fix (their queries are date-windowed, so an id
missing from one fetch may just be outside the window), but contacts/files
have no date filter - a catch-up fetch that comes back under its cap IS
the complete set, so anything locally indexed but absent from it is safely
known to be deleted. Added strayIdsAfterCatchUp() and wired it into the
catch-up path for those two types only.
Also read binding.ts, key.ts, paths.ts, jmap.ts, extract.ts, the FTS5
query builder, and both /api/offline/{search,reindex} routes end to end;
no other concrete bugs found there. Full findings reported separately.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
bde9f14832
commit
bde8455df5
+54
-31
@@ -15,7 +15,7 @@
|
||||
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { loadSqlcipher, type SqlcipherDatabase } from './binding';
|
||||
import { loadSqlcipher, type SqlcipherConstructor, type SqlcipherDatabase } from './binding';
|
||||
import { dbSiblings, indexDbPath } from './paths';
|
||||
|
||||
export const SCHEMA_VERSION = 1;
|
||||
@@ -137,6 +137,51 @@ export interface OpenOptions {
|
||||
key: Buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the connection, sets the SQLCipher key, verifies real encryption, and
|
||||
* applies the fixed pragmas - the sequence both the first attempt and the
|
||||
* wrong-key retry in `open()` need identically.
|
||||
*
|
||||
* On ANY failure the just-opened connection is closed before the error
|
||||
* propagates. This matters beyond `assertEncrypted`'s own failure (which
|
||||
* already closes): a bare `db.pragma(...)` throwing - SQLITE_BUSY, a full disk
|
||||
* on the first WAL write, anything - must not leak the native handle either,
|
||||
* which a `try` wrapped around only some of these calls previously missed.
|
||||
*
|
||||
* Exported so the cleanup guarantee can be unit-tested against a fake
|
||||
* `SqlcipherDatabase` - a real double failure (wrong key, THEN a pragma
|
||||
* failure on the freshly rebuilt file) is not practically reproducible
|
||||
* against the real binding.
|
||||
*/
|
||||
export function openKeyed(
|
||||
Database: SqlcipherConstructor,
|
||||
dbPath: string,
|
||||
key: Buffer,
|
||||
): { db: SqlcipherDatabase; version: number | null } {
|
||||
const db = new Database(dbPath);
|
||||
try {
|
||||
// The key pragma must be the FIRST statement on the connection. Hex form
|
||||
// means SQLCipher uses these 32 bytes as the raw key with no KDF, which is
|
||||
// right for a random key (a passphrase would want the KDF).
|
||||
db.pragma(`key = "x'${key.toString('hex')}'"`);
|
||||
assertEncrypted(db, dbPath);
|
||||
db.pragma('journal_mode = WAL');
|
||||
db.pragma('synchronous = NORMAL');
|
||||
// The offline replica (lib/offline-replica/**) is a SECOND connection to
|
||||
// this same file, writing disjoint tables. WAL lets a writer and readers
|
||||
// coexist, but two WRITERS get SQLITE_BUSY immediately without this - and
|
||||
// both subsystems are driven by the same renderer push handler, so they
|
||||
// genuinely do overlap.
|
||||
db.pragma('busy_timeout = 8000');
|
||||
return { db, version: readSchemaVersion(db) };
|
||||
} catch (error) {
|
||||
// Idempotent: assertEncrypted already closed on its own failure, so this
|
||||
// is a harmless no-op in that case.
|
||||
try { db.close(); } catch { /* already closed */ }
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export class MailIndex {
|
||||
private constructor(
|
||||
private readonly db: SqlcipherDatabase,
|
||||
@@ -163,46 +208,24 @@ export class MailIndex {
|
||||
const dbPath = indexDbPath(storeDir, accountId);
|
||||
fs.mkdirSync(path.dirname(dbPath), { recursive: true, mode: 0o700 });
|
||||
|
||||
let db = new Database(dbPath);
|
||||
// The key pragma must be the FIRST statement on the connection. Hex form
|
||||
// means SQLCipher uses these 32 bytes as the raw key with no KDF, which is
|
||||
// right for a random key (a passphrase would want the KDF).
|
||||
db.pragma(`key = "x'${key.toString('hex')}'"`);
|
||||
assertEncrypted(db, dbPath);
|
||||
|
||||
// A wrong key surfaces here rather than at open: SQLCipher only reads the
|
||||
// header lazily. Treat it as "unreadable" and rebuild from scratch - the
|
||||
// index is derived data, so there is nothing to recover and never anything
|
||||
// to prompt the user for (the key was never a user secret).
|
||||
let version: number | null;
|
||||
let opened: { db: SqlcipherDatabase; version: number | null };
|
||||
try {
|
||||
db.pragma('journal_mode = WAL');
|
||||
db.pragma('synchronous = NORMAL');
|
||||
// The offline replica (lib/offline-replica/**) is a SECOND connection to
|
||||
// this same file, writing disjoint tables. WAL lets a writer and readers
|
||||
// coexist, but two WRITERS get SQLITE_BUSY immediately without this - and
|
||||
// both subsystems are driven by the same renderer push handler, so they
|
||||
// genuinely do overlap.
|
||||
db.pragma('busy_timeout = 8000');
|
||||
version = readSchemaVersion(db);
|
||||
opened = openKeyed(Database, dbPath, key);
|
||||
} catch {
|
||||
db.close();
|
||||
// `openKeyed` guarantees the failed connection above is already closed,
|
||||
// so there is nothing to clean up here before retrying on a fresh file.
|
||||
for (const f of dbSiblings(dbPath)) {
|
||||
try { fs.rmSync(f, { force: true }); } catch { /* best effort */ }
|
||||
}
|
||||
db = new Database(dbPath);
|
||||
db.pragma(`key = "x'${key.toString('hex')}'"`);
|
||||
assertEncrypted(db, dbPath);
|
||||
db.pragma('journal_mode = WAL');
|
||||
db.pragma('synchronous = NORMAL');
|
||||
// The offline replica (lib/offline-replica/**) is a SECOND connection to
|
||||
// this same file, writing disjoint tables. WAL lets a writer and readers
|
||||
// coexist, but two WRITERS get SQLITE_BUSY immediately without this - and
|
||||
// both subsystems are driven by the same renderer push handler, so they
|
||||
// genuinely do overlap.
|
||||
db.pragma('busy_timeout = 8000');
|
||||
version = null;
|
||||
opened = openKeyed(Database, dbPath, key);
|
||||
opened.version = null; // fresh file - nothing to read
|
||||
}
|
||||
const db = opened.db;
|
||||
let version = opened.version;
|
||||
|
||||
if (version !== null && version !== SCHEMA_VERSION) {
|
||||
// Rebuildable derived data: drop, don't migrate.
|
||||
|
||||
Reference in New Issue
Block a user