fix(admin): refuse a restore whose payload names a different owner

The restore target came from the encrypted payload's own userId field
while the audit entry recorded the backup row's userId. A file declaring
a different account would therefore have been written into that account,
with the trail naming the one the operator selected.

Both values are in hand at that point, so compare them and refuse on
mismatch, recording both ids. Adds the first test coverage for this
route; the mismatch case fails without the guard.

Also correct the CLAUDE.md claim that every API route wraps apiHandler -
the three MCP OAuth routes deliberately use withBackgroundEvent so they
can return RFC-format error bodies, and carry the same protections by
hand.

chore(release): v1.30.20
This commit is contained in:
Marc-André Bombeck committed 2026-07-19 02:16:54 +02:00
1 parent 75943496f7
commit 3ffaf1906c
7 files changed
+162 -4

No files matched your search

@@ -0,0 +1,129 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { NextRequest } from "next/server";
/**
* A restore writes into the account the BACKUP RECORD names, not the one the
* encrypted payload claims.
*
* The route took its target from `payload.userId` — a field inside the blob —
* while logging `backup.userId` from the row. An admin selecting one user's
* backup could therefore write into a different account, and the audit trail
* would record the account they thought they picked. Both values are in hand at
* that point, so the only correct behaviour is to compare them.
*
* This is inside the admin privilege boundary, so it is an integrity problem
* rather than an escalation: the operator's intent was not honoured.
*/
vi.mock("@/lib/api-handler", () => ({
apiHandler: (fn: unknown) => fn,
HttpError: class extends Error {
constructor(
public status: number,
message: string,
) {
super(message);
}
},
requireAdmin: vi.fn(async () => ({ user: { id: "admin-1" } })),
}));
vi.mock("@/lib/db", () => ({
prisma: {
dataBackup: { findUnique: vi.fn() },
user: { findUnique: vi.fn() },
},
}));
vi.mock("@/lib/crypto", () => ({ decrypt: vi.fn(() => "{}") }));
vi.mock("@/lib/crypto/note-cipher", () => ({ encryptNote: vi.fn() }));
vi.mock("@/lib/validations/backup", () => ({
parseBackupPayload: vi.fn(),
summarizeBackup: vi.fn(() => ({})),
}));
vi.mock("@/lib/auth/audit", () => ({ auditLog: vi.fn() }));
vi.mock("@/lib/logging/context", () => ({ annotate: vi.fn() }));
vi.mock("@/lib/idempotency", () => ({
withIdempotency: (fn: unknown) => fn,
defaultUserIdResolver: vi.fn(),
}));
vi.mock("@/lib/rollups/mood-rollups", () => ({
recomputeUserMoodRollups: vi.fn(),
}));
vi.mock("@/lib/rollups/medication-compliance-rollups", () => ({
recomputeUserMedicationCompliance: vi.fn(),
MEDICATION_COMPLIANCE_BACKFILL_DAYS: 30,
}));
vi.mock("@/lib/rollups/measurement-rollups", () => ({
recomputeUserRollups: vi.fn(),
}));
import { POST } from "../route";
import { prisma } from "@/lib/db";
import { parseBackupPayload } from "@/lib/validations/backup";
import { auditLog } from "@/lib/auth/audit";
function request(): NextRequest {
return new NextRequest("http://localhost/api/admin/backups/b-1/restore", {
method: "POST",
headers: { "content-type": "application/json" },
// The route requires a typed confirmation before it will restore anything.
body: JSON.stringify({ confirm: "RESTORE" }),
});
}
const params = Promise.resolve({ id: "b-1" });
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(prisma.dataBackup.findUnique).mockResolvedValue({
id: "b-1",
userId: "user-A",
data: "cipher",
user: { id: "user-A", username: "alice" },
} as never);
});
describe("admin backup restore — declared owner", () => {
it("refuses when the payload names a different owner than the record", async () => {
// The blob claims user-B; the backup row says user-A.
vi.mocked(parseBackupPayload).mockReturnValue({
userId: "user-B",
} as never);
const res = await (
POST as unknown as (r: NextRequest, c: unknown) => Promise<Response>
)(request(), { params });
expect(res.status).toBe(409);
// The restore must not have gone looking for the claimed user at all.
expect(prisma.user.findUnique).not.toHaveBeenCalled();
// And the refusal is recorded with BOTH ids, so the attempt is reviewable.
expect(auditLog).toHaveBeenCalledWith(
"admin.backups.restore.failed",
expect.objectContaining({
details: expect.objectContaining({
reason: "owner_mismatch",
ownerId: "user-A",
declaredOwnerId: "user-B",
}),
}),
);
});
it("proceeds past the check when the two agree", async () => {
vi.mocked(parseBackupPayload).mockReturnValue({
userId: "user-A",
} as never);
vi.mocked(prisma.user.findUnique).mockResolvedValue(null as never);
const res = await (
POST as unknown as (r: NextRequest, c: unknown) => Promise<Response>
)(request(), { params });
// Reaching the owner lookup proves the mismatch branch did not fire; this
// case then fails on the deleted-user branch, which is the next guard.
expect(prisma.user.findUnique).toHaveBeenCalled();
expect(res.status).not.toBe(409);
});
});
@@ -195,6 +195,29 @@ const handler = apiHandler(
return apiError("Backup payload failed schema validation", 500);
}
// The payload declares its own owner, and the backup ROW records who the
// backup was taken for. Those must agree. Taking the owner from the
// payload alone means the restored-into account is whatever the blob
// claims — so an admin who selects one user's backup could write into a
// different account without the interface ever showing it. Both values are
// already in hand here; refuse on mismatch rather than trusting the blob.
if (payload.userId !== backup.userId) {
await auditLog("admin.backups.restore.failed", {
userId: admin.id,
ipAddress: getClientIp(request),
details: {
backupId: id,
ownerId: backup.userId,
reason: "owner_mismatch",
declaredOwnerId: payload.userId,
},
});
return apiError(
"Backup payload declares a different owner than the backup record",
409,
);
}
// The target of the restore is whoever the backup is for, NOT the
// admin running the operation. Make sure that user still exists —
// an upload referencing a since-deleted user would otherwise leave