Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

Take a closed account's key rows before the audit lock when deleting it

Claude213a706c0f24
diff --git a/src/lib/account-purge.dbtest.ts b/src/lib/account-purge.dbtest.ts
index 5c0ad16..e9af412 100644
--- a/src/lib/account-purge.dbtest.ts
+++ b/src/lib/account-purge.dbtest.ts
@@ -7,12 +7,14 @@ import { after, before, test } from "node:test";
import type pg from "pg";
+import { createApiKey } from "./api-keys.ts";
import { afterStatement, openTestDatabase } from "./db/test-database.ts";
import { runRetentionSweep } from "./retention.ts";
import {
createStorageService,
type StorageService,
} from "./services/storage.ts";
+import { SYSTEM_AUDIT_LOCK_ID } from "./services/system-audit.ts";
// Deleting a closed account against a real Postgres, timed against a signer
// who finishes one of its envelopes at that moment. A signed record is the
@@ -282,3 +284,54 @@ test("with no administrator to take it, an envelope completed during the deletio
);
}
});
+
+test("deleting a closed account takes its API key rows before the audit lock, as revoking a key does", async () => {
+ // revokeApiKey locks a key row and then the audit chain. Hold a key row of
+ // the account the way it does: the deletion has to wait for it without
+ // already holding the audit chain, or the two would deadlock.
+ await createUser("admin");
+
+ const leaver = await createUser("admin");
+ const { summary: key } = await createApiKey(
+ { email: `${leaver}@example.test`, userId: leaver },
+ { expiresInDays: null, name: "Leaver's app", scope: "write" },
+ database,
+ );
+
+ await closeAccount(leaver);
+
+ const holder = await database.connect();
+ let sweep: ReturnType<typeof runRetentionSweep> | undefined;
+
+ try {
+ await holder.query("begin");
+ await holder.query(`select id from api_keys where id = $1 for update`, [
+ key.id,
+ ]);
+
+ sweep = runRetentionSweep({ database, storageService });
+ await waitForLockWaiter();
+
+ const free = await holder.query<{ locked: boolean }>(
+ `select pg_try_advisory_xact_lock($1) as locked`,
+ [SYSTEM_AUDIT_LOCK_ID],
+ );
+
+ assert.equal(free.rows[0]?.locked, true);
+ } finally {
+ await holder.query("rollback").catch(() => undefined);
+ holder.release();
+ }
+
+ assert.ok(sweep);
+
+ const summary = await sweep;
+
+ assert.ok(!summary.accountPurgeFailures.includes(leaver));
+ assert.equal(await accountExists(leaver), false);
+ assert.equal(
+ (await database.query(`select 1 from api_keys where id = $1`, [key.id]))
+ .rowCount,
+ 0,
+ );
+});
diff --git a/src/lib/retention.ts b/src/lib/retention.ts
index 7a77457..ba15380 100644
--- a/src/lib/retention.ts
+++ b/src/lib/retention.ts
@@ -630,6 +630,15 @@ async function deleteAccountRows(
[userId],
);
+ // The account's API keys go with its row below. revokeApiKey locks a key
+ // row and then the audit chain, so the key rows are taken here, before
+ // the audit event: in the other order a revoke at the same moment and
+ // this delete would each hold what the other waits for.
+ await database.query(
+ `SELECT id FROM api_keys WHERE user_id = $1 FOR UPDATE`,
+ [userId],
+ );
+
await recordSystemAuditEvent(
{
actorEmail: null,