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
12 changes: 9 additions & 3 deletions src/app/api/upload/route.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@ import { createHash } from "node:crypto";
import { NextResponse } from "next/server";
import { z } from "zod";
import { env } from "@/lib/env";
import { assertAllowedFile, assertFileContentSignature, jsonError } from "@/lib/http";
import { assertAllowedFile, assertFileContentSignature, jsonError, PublicApiError } from "@/lib/http";
import { logger } from "@/lib/logger";
import { writeAuditLog } from "@/lib/audit";
import { planDocumentName } from "@/lib/document-naming";
Expand All@@ -28,7 +28,13 @@ export async function POST(request: Request) {
try {
supabase = createAdminClient();
const user = await requireAuthenticatedUser(request, supabase);
const formData = await request.formData();
const formData = await request.formData().catch((cause) => {
throw new PublicApiError("Invalid upload form data.", 400, {
code: "invalid_form_data",
causeName: cause instanceof Error ? cause.name : null,
causeMessage: cause instanceof Error ? cause.message : null,
});
});
const file = formData.get("file");
if (!(file instanceof File)) {
return NextResponse.json({ error: "Missing file field." }, { status: 400 });
Expand DownExpand Up@@ -174,6 +180,6 @@ export async function POST(request: Request) {
return unauthorizedResponse();
}

return jsonError(error, 400);
return jsonError(error);
}
}
40 changes: 40 additions & 0 deletions tests/api-validation-contract.test.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -386,6 +386,46 @@ describe("API validation contracts", () => {
expect(client.from).not.toHaveBeenCalled();
});

it("rejects non-form upload content before storage upload or database writes", async () => {
const client = createSupabaseMock();
mockRuntime(client);
const { POST } = await import("../src/app/api/upload/route");

const response = await POST(
authenticatedRequest("/api/upload", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ file: "nope" }),
}),
);
const body = await payload(response);

expect(response.status).toBe(400);
expect(body).toEqual({ error: "Invalid upload form data." });
expect(client.storageMocks.upload).not.toHaveBeenCalled();
expect(client.from).not.toHaveBeenCalled();
});

it("rejects malformed multipart upload bodies before storage upload or database writes", async () => {
const client = createSupabaseMock();
mockRuntime(client);
const { POST } = await import("../src/app/api/upload/route");

const response = await POST(
authenticatedRequest("/api/upload", {
method: "POST",
headers: { "content-type": "multipart/form-data; boundary=broken" },
body: "--broken\r\nContent-Disposition: form-data; name=\"file\"; filename=\"guideline.pdf\"\r\n\r\n%PDF-1.7",
}),
);
const body = await payload(response);

expect(response.status).toBe(400);
expect(body).toEqual({ error: "Invalid upload form data." });
expect(client.storageMocks.upload).not.toHaveBeenCalled();
expect(client.from).not.toHaveBeenCalled();
});

it("accepts valid document rename JSON through the shared body parser", async () => {
const client = createSupabaseMock((call) => {
if (call.table === "documents" && call.operation === "select" && call.maybeSingle) {
Expand Down
4 changes: 2 additions & 2 deletions tests/private-access-routes.test.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -1489,7 +1489,7 @@ describe("private document API access", () => {
);
const uploadPath = client.storageMocks.upload.mock.calls[0]?.[0] as string;

expect(response.status).toBe(400);
expect(response.status).toBe(500);
expect(client.storageMocks.remove).toHaveBeenCalledWith([uploadPath]);
});

Expand All@@ -1516,7 +1516,7 @@ describe("private document API access", () => {
);
const uploadPath = client.storageMocks.upload.mock.calls[0]?.[0] as string;

expect(response.status).toBe(400);
expect(response.status).toBe(500);
expect(client.storageMocks.remove).toHaveBeenCalledWith([uploadPath]);
});

Expand Down
Loading