Expose sync reset API

This commit is contained in:
2026-05-08 19:10:04 -04:00
parent e9f8b1eb43
commit ed44db3034
9 changed files with 597 additions and 1 deletions
+3
View File
@@ -290,6 +290,9 @@ function testR2Bucket(): Env["ELY_STORAGE"] {
},
});
},
delete() {
return Promise.resolve();
},
};
}
+13 -1
View File
@@ -14,6 +14,7 @@ export interface TestEnvOptions {
d1?: RecordedD1Database;
kvEntries?: [string, string][];
kvReads?: string[];
r2Deletes?: string[];
r2Gets?: string[];
r2Objects?: [string, ArrayBuffer][];
r2Puts?: RecordedR2Put[];
@@ -47,7 +48,12 @@ export function testEnv(options: TestEnvOptions): Env {
return Promise.resolve(values.get(key) ?? null);
},
},
ELY_STORAGE: testR2Bucket(options.r2Puts, options.r2Objects, options.r2Gets),
ELY_STORAGE: testR2Bucket(
options.r2Puts,
options.r2Objects,
options.r2Gets,
options.r2Deletes,
),
ELY_RATE_LIMITER: {
limit(): Promise<{ success: boolean }> {
return Promise.resolve({ success: true });
@@ -125,6 +131,7 @@ function testR2Bucket(
puts: RecordedR2Put[] = [],
objects: [string, ArrayBuffer][] = [],
gets: string[] = [],
deletes: string[] = [],
): Env["ELY_STORAGE"] {
const values = new Map(objects);
return {
@@ -149,5 +156,10 @@ function testR2Bucket(
},
});
},
delete(key: string) {
deletes.push(key);
values.delete(key);
return Promise.resolve();
},
};
}
+3
View File
@@ -371,6 +371,9 @@ function testR2Bucket(): Env["ELY_STORAGE"] {
},
});
},
delete() {
return Promise.resolve();
},
};
}
+26
View File
@@ -6,6 +6,7 @@ import type { ElyR2Bucket, ElyR2PutOptions } from "../src/bindings.js";
import {
StorageObjectError,
crashAttachmentKey,
deleteKnownObject,
exportObjectKey,
getVerifiedObject,
pluginAssetKey,
@@ -168,6 +169,24 @@ describe("R2 storage contracts", () => {
assert.deepEqual(new Uint8Array(downloaded ?? new ArrayBuffer(0)), new Uint8Array(payload));
});
it("deletes only known object key shapes", async () => {
const bucket = recordedR2Bucket();
const key = syncSnapshotKey({
region: "us-east",
userHash: USER_HASH,
snapshotId: "snapshot-01",
});
await deleteKnownObject(bucket, key);
assert.deepEqual(bucket.deletes, [key]);
await assert.rejects(
() => deleteKnownObject(bucket, "sync-snapshots/../bad.bin"),
StorageObjectError,
);
assert.deepEqual(bucket.deletes, [key]);
});
});
interface RecordedPut {
@@ -177,12 +196,15 @@ interface RecordedPut {
}
interface RecordedR2Bucket extends ElyR2Bucket {
deletes: string[];
puts: RecordedPut[];
}
function recordedR2Bucket(payload?: ArrayBuffer): RecordedR2Bucket {
const deletes: string[] = [];
const puts: RecordedPut[] = [];
return {
deletes,
puts,
get() {
if (payload === undefined) {
@@ -202,6 +224,10 @@ function recordedR2Bucket(payload?: ArrayBuffer): RecordedR2Bucket {
},
});
},
delete(key: string) {
deletes.push(key);
return Promise.resolve();
},
};
}
+226
View File
@@ -0,0 +1,226 @@
import assert from "node:assert/strict";
import { createHash } from "node:crypto";
import { describe, it } from "node:test";
import { authSessionCacheKvKey, authTokenHash } from "../src/auth.js";
import { handleRequest } from "../src/index.js";
import { ACCESS_TOKEN, sessionDocument, testD1Database, testEnv } from "./devices_test_support.js";
const USER_ID = "user-01";
const DEVICE_ID = "device-01";
const IDEMPOTENCY_KEY = "sync-reset-000001";
const PAYLOAD_HASH = "a".repeat(64);
const USER_HASH = sha256(bytes(USER_ID));
const PAYLOAD_KEY = `sync-payloads/us-east/${USER_HASH}/tabs/tab-01/${PAYLOAD_HASH}.bin`;
const SNAPSHOT_KEY = `sync-snapshots/us-east/${USER_HASH}/snapshot-01.bin`;
describe("sync reset routes", () => {
it("deletes cloud sync data and records an audit event", async () => {
const r2Deletes: string[] = [];
const tokenHash = await authTokenHash(ACCESS_TOKEN);
const d1 = testD1Database({
firstRows: [{ device_id: DEVICE_ID }, null, resetCountsRow()],
allRows: [{ r2_key: PAYLOAD_KEY }, { r2_key: SNAPSHOT_KEY }],
});
const response = await handleRequest(
syncResetRequest(syncResetBody()),
testEnv({
d1,
r2Deletes,
kvEntries: [[authSessionCacheKvKey("local", tokenHash), sessionDocument(DEVICE_ID)]],
}),
);
const body = (await response.json()) as {
version: number;
user_id: string;
device_id: string;
idempotency_key: string;
reset_at: number;
deleted: Record<string, number>;
};
assert.equal(response.status, 200);
assert.equal(response.headers.get("cache-control"), "no-store");
assert.equal(body.version, 1);
assert.equal(body.user_id, USER_ID);
assert.equal(body.device_id, DEVICE_ID);
assert.equal(body.idempotency_key, IDEMPOTENCY_KEY);
assert.ok(Number.isSafeInteger(body.reset_at));
assert.deepEqual(body.deleted, {
objects: 4,
changes: 9,
snapshots: 2,
tombstones: 1,
r2_objects: 2,
});
assert.deepEqual(r2Deletes, [PAYLOAD_KEY, SNAPSHOT_KEY]);
assert.equal(d1.batches[0], 5);
assert.ok(d1.queries[1]?.includes("FROM audit_events"));
assert.ok(d1.queries[2]?.includes("FROM sync_objects"));
assert.ok(d1.queries[3]?.includes("UNION"));
assert.ok(d1.queries[4]?.includes("DELETE FROM sync_change_log"));
assert.ok(d1.queries[8]?.includes("INSERT INTO audit_events"));
assert.deepEqual(d1.binds[1], [USER_ID, syncResetEventId()]);
assert.deepEqual(d1.binds[2], [USER_ID, USER_ID, USER_ID, USER_ID]);
assert.deepEqual(d1.binds[3], [USER_ID, USER_ID]);
assert.deepEqual(d1.binds[8]?.slice(0, 4), [syncResetEventId(), USER_ID, DEVICE_ID, USER_ID]);
});
it("returns an idempotent reset document for existing audit events", async () => {
const r2Deletes: string[] = [];
const tokenHash = await authTokenHash(ACCESS_TOKEN);
const d1 = testD1Database({
firstRows: [
{ device_id: DEVICE_ID },
{ actor_device_id: DEVICE_ID, outcome: "success", created_at: 1_780_001_000 },
],
allRows: [{ r2_key: PAYLOAD_KEY }],
});
const response = await handleRequest(
syncResetRequest(syncResetBody()),
testEnv({
d1,
r2Deletes,
kvEntries: [[authSessionCacheKvKey("local", tokenHash), sessionDocument(DEVICE_ID)]],
}),
);
assert.equal(response.status, 200);
assert.deepEqual(await response.json(), {
version: 1,
user_id: USER_ID,
device_id: DEVICE_ID,
idempotency_key: IDEMPOTENCY_KEY,
reset_at: 1_780_001_000,
deleted: { objects: 0, changes: 0, snapshots: 0, tombstones: 0, r2_objects: 0 },
});
assert.deepEqual(r2Deletes, []);
assert.equal(d1.queries.length, 2);
assert.deepEqual(d1.batches, []);
});
it("rejects replay mismatches before deleting sync data", async () => {
const r2Deletes: string[] = [];
const tokenHash = await authTokenHash(ACCESS_TOKEN);
const d1 = testD1Database({
firstRows: [
{ device_id: DEVICE_ID },
{ actor_device_id: "device-02", outcome: "success", created_at: 1_780_001_000 },
],
});
const response = await handleRequest(
syncResetRequest(syncResetBody()),
testEnv({
d1,
r2Deletes,
kvEntries: [[authSessionCacheKvKey("local", tokenHash), sessionDocument(DEVICE_ID)]],
}),
);
assert.equal(response.status, 400);
assert.deepEqual(await response.json(), { error: "invalid_sync_reset" });
assert.deepEqual(r2Deletes, []);
assert.deepEqual(d1.batches, []);
});
it("rejects missing confirmation before reset reads", async () => {
const r2Deletes: string[] = [];
const tokenHash = await authTokenHash(ACCESS_TOKEN);
const d1 = testD1Database({ firstRows: [{ device_id: DEVICE_ID }] });
const response = await handleRequest(
syncResetRequest(syncResetBody({ confirmation: "delete" })),
testEnv({
d1,
r2Deletes,
kvEntries: [[authSessionCacheKvKey("local", tokenHash), sessionDocument(DEVICE_ID)]],
}),
);
assert.equal(response.status, 400);
assert.deepEqual(await response.json(), { error: "invalid_sync_reset" });
assert.deepEqual(r2Deletes, []);
assert.equal(d1.queries.length, 1);
assert.deepEqual(d1.batches, []);
});
it("rejects revoked devices before reading reset bodies", async () => {
const tokenHash = await authTokenHash(ACCESS_TOKEN);
const d1 = testD1Database({ firstRows: [null] });
const response = await handleRequest(
syncResetRequest(syncResetBody()),
testEnv({
d1,
kvEntries: [[authSessionCacheKvKey("local", tokenHash), sessionDocument(DEVICE_ID)]],
}),
);
assert.equal(response.status, 403);
assert.deepEqual(await response.json(), { error: "device_not_approved" });
assert.equal(d1.queries.length, 1);
assert.deepEqual(d1.batches, []);
});
it("fails closed when stored R2 keys are malformed", async () => {
const r2Deletes: string[] = [];
const tokenHash = await authTokenHash(ACCESS_TOKEN);
const d1 = testD1Database({
firstRows: [{ device_id: DEVICE_ID }, null, resetCountsRow()],
allRows: [{ r2_key: "sync-snapshots/../bad.bin" }],
});
const response = await handleRequest(
syncResetRequest(syncResetBody()),
testEnv({
d1,
r2Deletes,
kvEntries: [[authSessionCacheKvKey("local", tokenHash), sessionDocument(DEVICE_ID)]],
}),
);
assert.equal(response.status, 500);
assert.deepEqual(await response.json(), { error: "sync_reset_failed" });
assert.deepEqual(r2Deletes, []);
assert.deepEqual(d1.batches, []);
});
});
function syncResetRequest(body: Record<string, unknown>): Request {
return new Request("https://elydora.test/api/sync/reset", {
method: "POST",
headers: {
authorization: `Bearer ${ACCESS_TOKEN}`,
"content-type": "application/json",
},
body: JSON.stringify(body),
});
}
function syncResetBody(overrides: Record<string, unknown> = {}): Record<string, unknown> {
return {
version: 1,
confirmation: "delete-cloud-sync-data",
idempotency_key: IDEMPOTENCY_KEY,
...overrides,
};
}
function resetCountsRow(overrides: Record<string, unknown> = {}): Record<string, unknown> {
return { objects: 4, changes: 9, snapshots: 2, tombstones: 1, ...overrides };
}
function syncResetEventId(): string {
return `sync-reset:${USER_ID}:${IDEMPOTENCY_KEY}`;
}
function bytes(value: string): ArrayBuffer {
return new TextEncoder().encode(value).buffer;
}
function sha256(payload: ArrayBuffer): string {
return createHash("sha256").update(new Uint8Array(payload)).digest("hex");
}