Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: authenticate every Signal snapshot page before ingestion (#11148)

Adam Eivy4cf207bbc15c
diff --git a/server/services/signalSync.js b/server/services/signalSync.js
index e21d354cad..342c6beb05 100644
--- a/server/services/signalSync.js
+++ b/server/services/signalSync.js
@@ -338,13 +338,13 @@ async function openDecryptedSnapshot(key) {
await rm(tmp, { recursive: true, force: true });
return { ok: false, error: 'Signal database snapshot is empty', remediation: 'The Signal database copy was empty — try again with Signal closed.' };
}
- const decrypted = decryptSqlcipherDatabase(encBuf, key, { verify: 'first' });
+ const decrypted = decryptSqlcipherDatabase(encBuf, key, { verify: 'all' });
if (!decrypted.ok) {
await rm(tmp, { recursive: true, force: true });
return {
ok: false,
error: `Signal database decryption failed: ${decrypted.error}`,
- remediation: 'Signal database decryption failed — the key or SQLCipher format may have changed in this Signal version (not yet supported).',
+ remediation: 'Signal database authentication failed — the key or SQLCipher format may have changed, or the snapshot is damaged. Close Signal Desktop and retry; if the failure persists, check or restore the source database.',
};
}
await writeFile(plainPath, decrypted.plaintext);
diff --git a/server/services/signalSync.runSync.test.js b/server/services/signalSync.runSync.test.js
new file mode 100644
index 0000000000..3a233f95a1
--- /dev/null
+++ b/server/services/signalSync.runSync.test.js
@@ -0,0 +1,160 @@
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
+import { DatabaseSync } from 'node:sqlite';
+import crypto from 'node:crypto';
+import { existsSync, mkdirSync, readFileSync, writeFileSync, rmSync } from 'node:fs';
+import { join } from 'node:path';
+import { createTempDataRoot } from '../lib/mockPathsDataRoot.js';
+import { decryptSqlcipherDatabase, deriveSqlcipherKeys, sqlcipherPageHmac,
+ SQLCIPHER_PAGE_SIZE, SQLCIPHER_RESERVE_BYTES, SQLCIPHER_SALT_BYTES,
+ SQLCIPHER_IV_BYTES } from '../lib/signalCrypto.js';
+
+const mocks = vi.hoisted(() => ({
+ root: '', snapshots: [], recordEvents: vi.fn(), autoLogTouchpoints: vi.fn(),
+}));
+vi.mock('../lib/fileUtils.js', async (importOriginal) => ({
+ ...await importOriginal(),
+ dataPath: (...parts) => join(mocks.root, ...parts),
+}));
+vi.mock('node:fs/promises', async (importOriginal) => {
+ const actual = await importOriginal();
+ return { ...actual, mkdtemp: async (...args) => {
+ const path = await actual.mkdtemp(...args);
+ mocks.snapshots.push(path);
+ return path;
+ } };
+});
+vi.mock('./settings.js', () => ({ getSettings: vi.fn(async () => ({})) }));
+vi.mock('./userTimezone.js', () => ({ getUserTimezone: vi.fn(async () => 'UTC') }));
+vi.mock('./humanActivity.js', async (importOriginal) => ({
+ ...await importOriginal(), recordEvents: mocks.recordEvents,
+}));
+vi.mock('./tribe.js', () => ({ autoLogTouchpoints: mocks.autoLogTouchpoints }));
+
+import { checkSetup, runSync, readSyncState } from './signalSync.js';
+
+const KEY = 'ab'.repeat(32); // invented fixture key; never reads the keychain
+const BODY = 'x'.repeat(512);
+let encrypted;
+let plaintext;
+
+// Build a genuinely queryable SQLite database with SQLCipher's 80-byte reserve.
+// An empty SQLite page has no cells to relocate: changing its reserve and empty
+// content-area pointer before creating tables makes SQLite allocate all later
+// pages with the correct usable size. No external SQLCipher/Python dependency.
+function createFixture() {
+ const path = join(mocks.root, 'fixture.sqlite');
+ let db = new DatabaseSync(path);
+ db.exec('PRAGMA page_size=4096; VACUUM');
+ db.close();
+ const empty = readFileSync(path);
+ empty[20] = SQLCIPHER_RESERVE_BYTES;
+ empty.writeUInt16BE(SQLCIPHER_PAGE_SIZE - SQLCIPHER_RESERVE_BYTES, 105);
+ writeFileSync(path, empty);
+ db = new DatabaseSync(path);
+ db.exec(`
+ CREATE TABLE conversations(id TEXT, e164 TEXT, type TEXT, name TEXT, profileName TEXT);
+ CREATE TABLE messages(id TEXT, conversationId TEXT, type TEXT, sent_at INTEGER, received_at INTEGER, body TEXT);
+ INSERT INTO conversations VALUES ('fixture-chat', '+15550000000', 'private', 'Example Contact', '');
+ `);
+ db.prepare('INSERT INTO messages(rowid, id, conversationId, type, sent_at, body) VALUES (?,?,?,?,?,?)')
+ .run(8, 'fixture-message', 'fixture-chat', 'incoming', Date.UTC(2024, 5, 1, 12), BODY);
+ expect(db.prepare('PRAGMA integrity_check').get().integrity_check).toBe('ok');
+ db.close();
+ return readFileSync(path);
+}
+
+// Encryption uses the production KDF pinned by signalCrypto's independent
+// known-answer vectors; this fixture exercises service wiring, not KDF accuracy.
+function encrypt(plain) {
+ const salt = Buffer.alloc(SQLCIPHER_SALT_BYTES, 7);
+ const { encKey, hmacKey } = deriveSqlcipherKeys(KEY, salt);
+ const out = Buffer.alloc(plain.length);
+ for (let page = 1; page <= plain.length / SQLCIPHER_PAGE_SIZE; page += 1) {
+ const start = (page - 1) * SQLCIPHER_PAGE_SIZE;
+ const bodyStart = start + (page === 1 ? SQLCIPHER_SALT_BYTES : 0);
+ const end = start + SQLCIPHER_PAGE_SIZE - SQLCIPHER_RESERVE_BYTES;
+ const iv = Buffer.alloc(SQLCIPHER_IV_BYTES, page);
+ const cipher = crypto.createCipheriv('aes-256-cbc', encKey, iv);
+ cipher.setAutoPadding(false);
+ const ciphertext = Buffer.concat([cipher.update(plain.subarray(bodyStart, end)), cipher.final()]);
+ if (page === 1) salt.copy(out, start);
+ ciphertext.copy(out, bodyStart);
+ iv.copy(out, end);
+ sqlcipherPageHmac(hmacKey, Buffer.concat([ciphertext, iv]), page).copy(out, end + SQLCIPHER_IV_BYTES);
+ }
+ return out;
+}
+
+beforeEach(() => {
+ mocks.root = createTempDataRoot('signal-auth-test-');
+ mocks.snapshots = [];
+ mkdirSync(join(mocks.root, 'sql'));
+ writeFileSync(join(mocks.root, 'config.json'), JSON.stringify({ key: KEY }));
+ vi.stubEnv('SIGNAL_DIR', mocks.root);
+ vi.stubEnv('SIGNAL_CONFIG_PATH', join(mocks.root, 'config.json'));
+ vi.stubEnv('SIGNAL_DB_PATH', join(mocks.root, 'sql', 'db.sqlite'));
+ plaintext = createFixture();
+ encrypted = encrypt(plaintext);
+ writeFileSync(process.env.SIGNAL_DB_PATH, encrypted);
+ mocks.recordEvents.mockReset().mockImplementation(async (events) => ({ recorded: events.length, skipped: 0 }));
+ mocks.autoLogTouchpoints.mockReset().mockResolvedValue({ created: 1, matched: 1 });
+});
+afterEach(() => {
+ vi.unstubAllEnvs();
+ rmSync(mocks.root, { recursive: true, force: true });
+ vi.restoreAllMocks();
+});
+
+function expectSnapshotsRemoved() {
+ expect(mocks.snapshots.length).toBeGreaterThan(0);
+ expect(mocks.snapshots.every((path) => !existsSync(path))).toBe(true);
+}
+
+describe('Signal authenticated snapshot ingestion', () => {
+ it('accepts a multipage database and does not reingest messages on replay', async () => {
+ expect(plaintext.length).toBeGreaterThan(SQLCIPHER_PAGE_SIZE);
+ expect(await checkSetup()).toMatchObject({ ok: true, messageCount: 1 });
+ expect(await runSync()).toMatchObject({ ok: true, recorded: 1, cursorRowid: 8 });
+ expect(mocks.recordEvents.mock.calls[0][0]).toMatchObject([
+ { dedupeKey: 'signal:fixture-message', kind: 'message.received' },
+ ]);
+ expect(mocks.autoLogTouchpoints.mock.calls[0][0]).toHaveLength(1);
+ expect(await runSync()).toMatchObject({ ok: true, recorded: 0, scanned: 0, cursorRowid: 8 });
+ expect(mocks.recordEvents.mock.calls[1][0]).toEqual([]);
+ expect((await readSyncState()).cursorRowid).toBe(8);
+ expectSnapshotsRemoved();
+ });
+
+ it('refuses queryable later-page body corruption before writes and preserves the prior cursor', async () => {
+ const statePath = join(mocks.root, 'signal-sync-state.json');
+ const state = JSON.stringify({ cursorRowid: 7, lastRunAt: '2024-01-01T00:00:00Z', lastResult: { ok: true } });
+ writeFileSync(statePath, state);
+ // Flip a ciphertext bit well inside the long body, beyond page 1. CBC
+ // damages text bytes while leaving the cell header and B-tree readable.
+ const bodyOffset = plaintext.indexOf(Buffer.from(BODY));
+ expect(bodyOffset).toBeGreaterThan(SQLCIPHER_PAGE_SIZE);
+ encrypted[bodyOffset + 128] ^= 1;
+ writeFileSync(process.env.SIGNAL_DB_PATH, encrypted);
+ const partial = decryptSqlcipherDatabase(encrypted, KEY, { verify: 'first' });
+ expect(partial.ok).toBe(true);
+ const readablePath = join(mocks.root, 'damaged-readable.sqlite');
+ writeFileSync(readablePath, partial.plaintext);
+ const readable = new DatabaseSync(readablePath, { readOnly: true });
+ try {
+ expect(readable.prepare('PRAGMA integrity_check').get().integrity_check).toBe('ok');
+ expect(readable.prepare('SELECT body FROM messages').get().body).not.toBe(BODY);
+ } finally { readable.close(); }
+
+ vi.spyOn(console, 'error').mockImplementation(() => {});
+ for (const result of [await checkSetup(), await runSync()]) {
+ expect(result.ok).toBe(false);
+ expect(result.error).toMatch(/Page-[2-9]\d* HMAC mismatch/);
+ expect(result.remediation).toMatch(/Close Signal Desktop and retry/);
+ }
+ expect(mocks.recordEvents).not.toHaveBeenCalled();
+ expect(mocks.autoLogTouchpoints).not.toHaveBeenCalled();
+ expect(readFileSync(statePath, 'utf8')).toBe(state);
+ expect((await readSyncState()).cursorRowid).toBe(7);
+ expectSnapshotsRemoved();
+ });
+});