Skip to content

A local-first personal finance app

[AI] fix(sync): receive only the latest timestamp to stop counter overflow (#8706)

Stephen Brown II5f60f2971ca0
diff --git a/packages/loot-core/src/server/sync/index.ts b/packages/loot-core/src/server/sync/index.ts
index ce928f16c2..b98efa1b44 100644
--- a/packages/loot-core/src/server/sync/index.ts
+++ b/packages/loot-core/src/server/sync/index.ts
@@ -524,9 +524,22 @@ export const applyMessages = sequential(_applyMessages);
export function receiveMessages(messages: Message[]): Promise<Message[]> {
try {
- messages.forEach(msg => {
- Timestamp.recv(msg.timestamp);
- });
+ // Receiving the latest timestamp preserves the clock and drift check while
+ // advancing the counter once per batch.
+ let latest = null;
+ for (const { timestamp } of messages) {
+ if (
+ latest === null ||
+ timestamp.millis() > latest.millis() ||
+ (timestamp.millis() === latest.millis() &&
+ timestamp.counter() > latest.counter())
+ ) {
+ latest = timestamp;
+ }
+ }
+ if (latest !== null) {
+ Timestamp.recv(latest);
+ }
} catch (e) {
if (e instanceof Timestamp.ClockDriftError) {
throw new SyncError('clock-drift');
diff --git a/packages/loot-core/src/server/sync/sync.test.ts b/packages/loot-core/src/server/sync/sync.test.ts
index 5b859f253d..21322577df 100644
--- a/packages/loot-core/src/server/sync/sync.test.ts
+++ b/packages/loot-core/src/server/sync/sync.test.ts
@@ -9,7 +9,13 @@ import * as mockSyncServer from '#server/tests/mockSyncServer';
import * as encoder from './encoder';
import { isError } from './utils';
-import { applyMessages, fullSync, sendMessages, setSyncingMode } from './index';
+import {
+ applyMessages,
+ fullSync,
+ receiveMessages,
+ sendMessages,
+ setSyncingMode,
+} from './index';
beforeEach(() => {
mockSyncServer.reset();
@@ -150,6 +156,58 @@ describe('Sync', () => {
expect(result.messages.length).toBe(2);
expect(mockSyncServer.getMessages().length).toBe(3);
});
+
+ it('should advance the clock once per received batch', async () => {
+ // `Date.now()` is frozen here, standing in for the 100ms rounding Firefox
+ // applies under resistFingerprinting. The counter only resets when
+ // `Date.now()` advances, so one tick per message overflowed its 16 bits
+ // once a sync carried more than 65535 of them.
+ const node = '0000testinguuid2';
+ const base = Date.parse('1970-01-02T00:00:00.000Z');
+
+ const messages = [];
+ for (let i = 0; i < 100; i++) {
+ messages.push({
+ dataset: 'transactions',
+ row: 'foo',
+ column: 'amount',
+ value: 'N:1',
+ timestamp: Timestamp.parse(
+ `${new Date(base + i).toISOString()}-0000-${node}`,
+ ),
+ });
+ }
+
+ await receiveMessages(messages);
+
+ expect(getClock().timestamp.counter()).toBe(0);
+
+ // The clock must still sort above every timestamp it received.
+ const latest = messages[messages.length - 1].timestamp.toString();
+ expect(getClock().timestamp.toString() > latest).toBe(true);
+ });
+
+ it('should reject a batch holding a far-future timestamp', () => {
+ // `toISOString` writes years above 9999 as `+010000-…`, which sorts below
+ // every normal timestamp. The batch maximum must not rank it last, which
+ // would let it skip the clock drift check.
+ const message = (millis: number) => ({
+ dataset: 'transactions',
+ row: 'foo',
+ column: 'amount',
+ value: 'N:1',
+ timestamp: Timestamp.parse(
+ `${new Date(millis).toISOString()}-0000-0000testinguuid2`,
+ ),
+ });
+
+ expect(() =>
+ receiveMessages([
+ message(Date.parse('1970-01-02T00:00:00.000Z')),
+ message(253402300800000),
+ ]),
+ ).toThrow(expect.objectContaining({ reason: 'clock-drift' }));
+ });
});
function registerBudgetMonths(months) {
diff --git a/upcoming-release-notes/sync-timestamp-counter-overflow.md b/upcoming-release-notes/sync-timestamp-counter-overflow.md
new file mode 100644
index 0000000000..efbad4aada
--- /dev/null
+++ b/upcoming-release-notes/sync-timestamp-counter-overflow.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [StephenBrown2]
+---
+
+Prevent timestamp counter overflow when downloading a large sync.