From f67add3660256da74b66cbf18fec5eed6e6e8f50 Mon Sep 17 00:00:00 2001 From: agent Date: Fri, 9 Oct 2026 19:59:16 +0000 Subject: [PATCH] Server: count only newly stored events toward the group quota Re-uploading existing events (retries, backup restore) is idempotent and must not eat into MAX_GROUP_BYTES. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013FYo6JSV1fDU1fm3LU77iF --- apps/server/src/app.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/apps/server/src/app.ts b/apps/server/src/app.ts index d58137d..428be2c 100644 --- a/apps/server/src/app.ts +++ b/apps/server/src/app.ts @@ -174,17 +174,18 @@ export function createApp(cfg: Config, store: Store) { const body = await readJson(req, 200 * (cfg.maxEventBytes * 1.4 + 512)); const events = body?.events; if (!Array.isArray(events) || events.length === 0 || events.length > 200) throw new HttpError(400, 'events must be 1..200'); - let bytes = 0; + let incoming = 0; for (const e of events) { if (!EVENT_ID_RE.test(e?.id) || !DEVICE_RE.test(e?.deviceId) || !B64U_RE.test(e?.iv) || !B64U_RE.test(e?.ct) || e.iv.length !== 16) throw new HttpError(400, 'bad event'); const size = Math.floor((e.ct.length * 3) / 4); if (size > cfg.maxEventBytes) throw new HttpError(413, 'event too large'); - bytes += size; + incoming += size; } - if (g.bytes + bytes > cfg.maxGroupBytes) throw new HttpError(507, 'group storage full'); + if (g.bytes + incoming > cfg.maxGroupBytes) throw new HttpError(507, 'group storage full'); const seqs = store.tx(() => { let next = store.latestSeq(groupId); + let bytes = 0; const out: number[] = []; for (const e of events) { const existing = store.seqOf(groupId, e.id); @@ -194,6 +195,7 @@ export function createApp(cfg: Config, store: Store) { } next += 1; store.insertEvent(groupId, next, { id: e.id, deviceId: e.deviceId, iv: e.iv, ct: e.ct }); + bytes += Math.floor((e.ct.length * 3) / 4); out.push(next); } store.addBytes(groupId, bytes);