Skip to content

Commit a165aa4

Browse files
committed
fix(upload): enforce strict mp4 validation
1 parent c30ca23 commit a165aa4

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

app/api/upload/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from "node:path";
44
import { readSession, authConfigured, SESSION_COOKIE } from "@/lib/session";
55
import { findOrCreateAccountByEmail, ownsParkedDomain, getTenantConfig, upsertTenant } from "@/lib/db";
66
import { safeDomain } from "@/lib/config";
7-
import { ffmpegPoster } from "@/lib/media";
7+
import { ffmpegPoster, mediaTypeForUpload } from "@/lib/media";
88

99
export const runtime = "nodejs";
1010
export const dynamic = "force-dynamic";
@@ -36,7 +36,7 @@ export async function POST(req: NextRequest) {
3636
try { form = await req.formData(); } catch { return NextResponse.json({ error: "Bad upload." }, { status: 400 }); }
3737
const file = form.get("file");
3838
if (!(file instanceof File)) return NextResponse.json({ error: "No file." }, { status: 400 });
39-
const isMp4 = /\.mp4$/i.test(file.name) || /video\/mp4|application\/octet-stream/.test(file.type);
39+
const isMp4 = mediaTypeForUpload(file.name, file.type) === "video/mp4";
4040
if (!isMp4) return NextResponse.json({ error: "MP4 files only." }, { status: 400 });
4141
if (file.size > MAX_BYTES) return NextResponse.json({ error: "Too big — 100 MB max." }, { status: 413 });
4242

tests/media-upload-type.test.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ test("media upload type rejects empty or generic non-video names", () => {
2020
assert.equal(mediaTypeForUpload("payload", "application/octet-stream"), null);
2121
assert.equal(mediaTypeForUpload("clip.gif", "application/octet-stream"), null);
2222
});
23+
24+
test("media upload type rejects substring MIME matches", () => {
25+
assert.equal(mediaTypeForUpload("payload.bin", "application/octet-stream-extra"), null);
26+
assert.equal(mediaTypeForUpload("payload.bin", "text/video/mp4"), null);
27+
assert.equal(mediaTypeForUpload("payload.bin", "application/json; video/mp4"), null);
28+
});

0 commit comments

Comments
 (0)