Skip to content

A local-first personal finance app

[AI] Reject sync messages too far in the future (#8905)

Joshua Soberg0b1eec8cced8
diff --git a/packages/sync-server/src/app-sync.test.ts b/packages/sync-server/src/app-sync.test.ts
index 76c2162534..96b9b78451 100644
--- a/packages/sync-server/src/app-sync.test.ts
+++ b/packages/sync-server/src/app-sync.test.ts
@@ -3,7 +3,12 @@ import crypto from 'node:crypto';
import fs from 'node:fs';
import fsPromises from 'node:fs/promises';
-import { create, SyncRequestSchema, toBinary } from '@actual-app/crdt';
+import {
+ create,
+ MessageEnvelopeSchema,
+ SyncRequestSchema,
+ toBinary,
+} from '@actual-app/crdt';
import request from 'supertest';
import { getAccountDb } from './account-db';
@@ -1608,6 +1613,75 @@ describe('/sync', () => {
expect(res.text).toEqual('file-access-not-allowed');
});
+ it.each([
+ { reason: 'clock-drift', timestamp: futureTimestamp() },
+ { reason: 'invalid-timestamp', timestamp: 'invalid' },
+ ])(
+ 'returns 400 $reason for a new message without storing it',
+ async ({ reason, timestamp }) => {
+ const fileId = crypto.randomBytes(16).toString('hex');
+ const keyId = 'key-id';
+ addMockFile(fileId, 'group-id', keyId, JSON.stringify({ keyId }), 2);
+
+ const syncRequest = createMinimalSyncRequest(fileId, 'group-id', keyId);
+ syncRequest.messages = [createMessage(timestamp)];
+ const res = await sendSyncRequest(syncRequest);
+ // A stored message would be accepted as a duplicate on retry
+ const retryRes = await sendSyncRequest(syncRequest);
+
+ expect(res.statusCode).toEqual(400);
+ expect(res.text).toEqual(reason);
+ expect(retryRes.statusCode).toEqual(400);
+ expect(retryRes.text).toEqual(reason);
+ },
+ );
+
+ it.each([
+ { offset: 5 * 60 * 1000, status: 200 },
+ { offset: 5 * 60 * 1000 + 1, status: 400 },
+ ])(
+ 'returns $status for a message $offset ms ahead',
+ async ({ offset, status }) => {
+ const fileId = crypto.randomBytes(16).toString('hex');
+ const groupId = crypto.randomBytes(16).toString('hex');
+ const keyId = 'key-id';
+ addMockFile(fileId, groupId, keyId, JSON.stringify({ keyId }), 2);
+
+ const syncRequest = createMinimalSyncRequest(fileId, groupId, keyId);
+
+ vi.useFakeTimers({ toFake: ['Date'], now: Date.now() });
+ try {
+ syncRequest.messages = [createMessage(futureTimestamp(offset))];
+ const res = await sendSyncRequest(syncRequest);
+
+ expect(res.statusCode).toEqual(status);
+ } finally {
+ vi.useRealTimers();
+ }
+ },
+ );
+
+ it('accepts a resent far-future message the server already has', async () => {
+ const fileId = crypto.randomBytes(16).toString('hex');
+ const groupId = crypto.randomBytes(16).toString('hex');
+ const keyId = 'key-id';
+ addMockFile(fileId, groupId, keyId, JSON.stringify({ keyId }), 2);
+
+ const syncRequest = createMinimalSyncRequest(fileId, groupId, keyId);
+ syncRequest.messages = [createMessage(futureTimestamp())];
+
+ vi.useFakeTimers({ toFake: ['Date'], now: Date.now() + 2 * DAY_MS });
+ try {
+ await sendSyncRequest(syncRequest);
+ } finally {
+ vi.useRealTimers();
+ }
+
+ const res = await sendSyncRequest(syncRequest);
+
+ expect(res.statusCode).toEqual(200);
+ });
+
it("allows an admin to sync another user's file", async () => {
const fileId = crypto.randomBytes(16).toString('hex');
const groupId = 'group-id';
@@ -1655,6 +1729,20 @@ function createMinimalSyncRequest(fileId, groupId, keyId) {
});
}
+const DAY_MS = 24 * 60 * 60 * 1000;
+
+function futureTimestamp(offset = 2 * DAY_MS) {
+ return `${new Date(Date.now() + offset).toISOString()}-0000-0000000000000000`;
+}
+
+function createMessage(timestamp: string) {
+ return create(MessageEnvelopeSchema, {
+ timestamp,
+ isEncrypted: false,
+ content: new Uint8Array(),
+ });
+}
+
async function sendSyncRequest(syncRequest, token = 'valid-token') {
const serializedRequest = toBinary(SyncRequestSchema, syncRequest);
// Convert Uint8Array to Buffer
diff --git a/packages/sync-server/src/app-sync.ts b/packages/sync-server/src/app-sync.ts
index e170482b72..c727d3341a 100644
--- a/packages/sync-server/src/app-sync.ts
+++ b/packages/sync-server/src/app-sync.ts
@@ -190,7 +190,12 @@ app.post('/sync', async (req, res): Promise<void> => {
return;
}
- const { trie, newMessages } = simpleSync.sync(messages, since, groupId);
+ const result = simpleSync.sync(messages, since, groupId);
+ if (result.error) {
+ res.status(400).send(result.error);
+ return;
+ }
+ const { trie, newMessages } = result;
const responsePb = create(SyncResponseSchema, {
merkle: JSON.stringify(trie),
diff --git a/packages/sync-server/src/sync-simple.js b/packages/sync-server/src/sync-simple.js
index c907ed94f6..f8063cfd56 100644
--- a/packages/sync-server/src/sync-simple.js
+++ b/packages/sync-server/src/sync-simple.js
@@ -11,6 +11,9 @@ import { openDatabase } from './db';
import messagesSql from './sql/messages.sql?raw';
import { getPathForGroupFile } from './util/paths';
+// Matches the client's Timestamp maxDrift; later messages break sync on other devices
+const MAX_FUTURE_DRIFT_MS = 5 * 60 * 1000;
+
function getGroupDb(groupId) {
const path = getPathForGroupFile(groupId);
const needsInit = !existsSync(path);
@@ -38,7 +41,13 @@ function addMessages(db, messages) {
);
if (info.changes > 0) {
- trie = merkle.insert(trie, Timestamp.parse(msg.timestamp));
+ const timestamp = Timestamp.parse(msg.timestamp);
+ const error = getTimestampError(timestamp);
+ if (error) {
+ // Throwing rolls back the transaction
+ throw Object.assign(new Error(error), { rejectedReason: error });
+ }
+ trie = merkle.insert(trie, timestamp);
}
}
}
@@ -56,6 +65,16 @@ function addMessages(db, messages) {
return returnValue;
}
+function getTimestampError(timestamp) {
+ if (!timestamp) {
+ return 'invalid-timestamp';
+ }
+ if (timestamp.millis() - Date.now() > MAX_FUTURE_DRIFT_MS) {
+ return 'clock-drift';
+ }
+ return null;
+}
+
function getMerkle(db) {
const rows = db.all('SELECT * FROM messages_merkles');
@@ -70,25 +89,32 @@ function getMerkle(db) {
export function sync(messages, since, groupId) {
const db = getGroupDb(groupId);
- const newMessages = db.all(
- `SELECT * FROM messages_binary
- WHERE timestamp > ?
- ORDER BY timestamp`,
- [since],
- );
-
- const trie = addMessages(db, messages);
-
- db.close();
-
- return {
- trie,
- newMessages: newMessages.map(msg =>
- create(MessageEnvelopeSchema, {
- timestamp: msg.timestamp,
- isEncrypted: msg.is_encrypted === 1,
- content: msg.content,
- }),
- ),
- };
+ try {
+ const newMessages = db.all(
+ `SELECT * FROM messages_binary
+ WHERE timestamp > ?
+ ORDER BY timestamp`,
+ [since],
+ );
+
+ const trie = addMessages(db, messages);
+
+ return {
+ trie,
+ newMessages: newMessages.map(msg =>
+ create(MessageEnvelopeSchema, {
+ timestamp: msg.timestamp,
+ isEncrypted: msg.is_encrypted === 1,
+ content: msg.content,
+ }),
+ ),
+ };
+ } catch (e) {
+ if (e.rejectedReason) {
+ return { error: e.rejectedReason };
+ }
+ throw e;
+ } finally {
+ db.close();
+ }
}
diff --git a/upcoming-release-notes/reject-sync-messages-too-far-in-future.md b/upcoming-release-notes/reject-sync-messages-too-far-in-future.md
new file mode 100644
index 0000000000..b5de9ee337
--- /dev/null
+++ b/upcoming-release-notes/reject-sync-messages-too-far-in-future.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [jsoberg]
+---
+
+Prevent a device with an incorrect clock from breaking sync for everyone sharing the budget.