Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions apps/api/src/privacy/admin-workflows-postgres.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,99 @@ suite("privacy case administration on PostgreSQL", () => {
}
});

describe("creating a case with an Idempotency-Key", () => {
const body = (locator: string, channel = "email") => ({
channel,
kind: "access",
subject: { locatorType: "email", locator, accountState: "unknown" },
locale: "en",
timeZone: "UTC",
});
const post = (server: Awaited<ReturnType<typeof app>>, key: string, payload: unknown) =>
server.inject({
method: "POST",
url: "/api/privacy/admin/requests",
headers: { "idempotency-key": key },
payload: payload as Record<string, unknown>,
});
const caseCount = async () =>
(await fixture.db.select({ id: dataSubjectRequest.id }).from(dataSubjectRequest)).length;

it("returns the first case when the same key is replayed for a subject without an account", async () => {
const key = `create-${randomUUID()}`;
const locator = `${randomUUID()}@example.test`;
const server = await app();
try {
const before = await caseCount();
const first = await post(server, key, body(locator));
const second = await post(server, key, body(locator));
expect(first.statusCode, first.body).toBe(202);
expect(second.statusCode, second.body).toBe(202);
expect(second.json().request.id).toBe(first.json().request.id);
expect(await caseCount()).toBe(before + 1);
const events = await fixture.db
.select()
.from(dataSubjectRequestEvent)
.where(eq(dataSubjectRequestEvent.idempotencyKey, key));
expect(events).toHaveLength(1);
} finally {
await server.close();
}
});

it("serializes two concurrent creates with the same key into one case", async () => {
const key = `create-${randomUUID()}`;
const locator = `${randomUUID()}@example.test`;
const server = await app();
try {
const before = await caseCount();
const [a, b] = await Promise.all([
post(server, key, body(locator)),
post(server, key, body(locator)),
]);
expect(a.statusCode, a.body).toBe(202);
expect(b.statusCode, b.body).toBe(202);
expect(b.json().request.id).toBe(a.json().request.id);
expect(await caseCount()).toBe(before + 1);
} finally {
await server.close();
}
});

it("creates a separate case for a different key", async () => {
const locator = `${randomUUID()}@example.test`;
const server = await app();
try {
const before = await caseCount();
const first = await post(server, `create-${randomUUID()}`, body(locator));
const second = await post(server, `create-${randomUUID()}`, body(locator));
expect(first.statusCode, first.body).toBe(202);
expect(second.statusCode, second.body).toBe(202);
expect(second.json().request.id).not.toBe(first.json().request.id);
expect(await caseCount()).toBe(before + 2);
} finally {
await server.close();
}
});

it("refuses a key reused with a different request", async () => {
const key = `create-${randomUUID()}`;
const locator = `${randomUUID()}@example.test`;
const server = await app();
try {
const before = await caseCount();
const first = await post(server, key, body(locator));
expect(first.statusCode, first.body).toBe(202);
const changed = await post(server, key, body(locator, "post"));
expect(changed.statusCode, changed.body).toBe(409);
expect(changed.json()).toEqual({ code: "IDEMPOTENCY_KEY_REUSED" });
expect(await caseCount()).toBe(before + 1);
} finally {
await server.close();
}
});
});

it("rejects a changed replay of the same idempotency key", async () => {
const request = await caseIn("in_progress");
const key = `same-${request.id}`;
Expand Down
32 changes: 31 additions & 1 deletion apps/api/src/privacy/idempotency.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createHash, randomUUID } from "node:crypto";
import { and, eq } from "drizzle-orm";
import { and, eq, isNull } from "drizzle-orm";
import type { db as defaultDb } from "../db/index.js";
import { dataSubjectRequestEvent } from "../db/schema.js";
import { isUniqueViolation } from "../db/unique-violation.js";
Expand Down Expand Up @@ -62,6 +62,36 @@ export async function findIdempotentEvent(
return event;
}

/**
* The `received` event of the case an earlier create with this key and actor
* produced. A create makes a new request id, so the per-request lookup above
* cannot find it. A key reused with a different operation is refused.
*/
export async function findIdempotentCreate(
database: typeof defaultDb,
input: { idempotencyKey: string; actorId: string | null; operationDigest: string },
) {
if (!IDEMPOTENCY_KEY.test(input.idempotencyKey)) throw new PrivacyIdempotencyError();
const rows = await database
.select()
.from(dataSubjectRequestEvent)
.where(
and(
eq(dataSubjectRequestEvent.idempotencyKey, input.idempotencyKey),
eq(dataSubjectRequestEvent.eventType, "received"),
input.actorId === null
? isNull(dataSubjectRequestEvent.actorId)
: eq(dataSubjectRequestEvent.actorId, input.actorId),
),
)
.limit(1);
const event = rows[0];
if (!event) return undefined;
if (payloadFingerprint(event.payload) !== input.operationDigest)
throw new PrivacyIdempotencyError();
return event;
}

export async function recordIdempotentEvent(
database: typeof defaultDb,
input: {
Expand Down
48 changes: 45 additions & 3 deletions apps/api/src/privacy/request-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ import { decrypt, encrypt } from "../services/secrets.js";
import { SUBJECT_DATA_CATALOGUE } from "./catalogue.js";
import { addCalendarMonths, calculateSubjectRequestDueAt } from "./deadline.js";
import { PrivacyRequestError } from "./errors.js";
import { findIdempotentEvent, idempotencyFingerprint } from "./idempotency.js";
import {
findIdempotentCreate,
findIdempotentEvent,
idempotencyFingerprint,
} from "./idempotency.js";
import { withPreservationRetentionLock } from "./preservation.js";
import {
assertValidRequestTransition,
Expand Down Expand Up @@ -203,8 +207,42 @@ export class PrivacyRequestService {
const applicable = this.catalogue.filter(
(registration) => registration.strategy !== "not_personal",
);
// A replayed create (same key, same administrator, same request) returns
// the case it first made. `receivedAt` enters only as supplied, because an
// omitted one defaults to "now" and would differ on every retry. The
// subject's locator stays out: it is stored encrypted, and an unsalted
// digest of an e-mail address could be confirmed by guessing.
const createDigest = parsed.idempotencyKey
? idempotencyFingerprint({
action: "create",
kind: parsed.kind,
channel: parsed.channel,
userId: parsed.userId,
subject: parsed.subject
? {
locatorType: parsed.subject.locatorType,
accountState: parsed.subject.accountState,
}
: null,
receivedAt: parsed.receivedAt ? toDate(parsed.receivedAt).toISOString() : null,
locale: parsed.locale,
timeZone: parsed.timeZone,
})
: undefined;
let resultId: string = id;
try {
await withPreservationRetentionLock(this.database, async (tx) => {
if (parsed.idempotencyKey && createDigest) {
const previous = await findIdempotentCreate(tx as unknown as typeof defaultDb, {
idempotencyKey: parsed.idempotencyKey,
actorId: parsed.actorUserId ?? null,
operationDigest: createDigest,
});
if (previous) {
resultId = previous.requestId;
return;
}
}
const [created] = await tx
.insert(dataSubjectRequest)
.values({
Expand Down Expand Up @@ -240,7 +278,11 @@ export class PrivacyRequestService {
eventType: "received",
actorKind: selfService ? "subject" : "admin",
actorId: parsed.actorUserId ?? null,
payload: boundedEventPayload({ kind: parsed.kind, channel: parsed.channel }),
payload: boundedEventPayload({
kind: parsed.kind,
channel: parsed.channel,
...(createDigest ? { operationDigest: createDigest } : {}),
}),
payloadVersion: 1,
...(parsed.idempotencyKey ? { idempotencyKey: parsed.idempotencyKey } : {}),
createdAt: now,
Expand Down Expand Up @@ -326,7 +368,7 @@ export class PrivacyRequestService {
}
throw error;
}
return this.getById(id);
return this.getById(resultId);
}

async getById(requestId: string): Promise<RequestRow> {
Expand Down
Loading