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
4 changes: 2 additions & 2 deletions app/api/uploads/sessions/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { apiErrorResponse, parseJson } from "@/lib/http/api";
import { secureJson } from "@/lib/security/http";
import {
artifactIntentSchema,
validateArtifactIntent,
validateArtifactIntentFromUploadRequest,
} from "@/lib/security/policy";
import { enforceRateLimit } from "@/lib/security/rate-limit";
import { createR2PresignedUpload } from "@/lib/storage/r2-presign";
Expand All @@ -24,7 +24,7 @@ export async function POST(request: Request) {
windowMs: 24 * 60 * 60 * 1000,
});
const input = await parseJson(request, requestSchema);
const validated = validateArtifactIntent(input);
const validated = validateArtifactIntentFromUploadRequest(input);
if (!validated.ok) {
return secureJson({ error: validated.error }, { status: 400 });
}
Expand Down
14 changes: 14 additions & 0 deletions lib/security/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,20 @@ export function validateArtifactIntent(input: unknown) {
};
}

export function validateArtifactIntentFromUploadRequest(input: {
kind: unknown;
fileName: unknown;
contentType: unknown;
byteSize: unknown;
}) {
return validateArtifactIntent({
kind: input.kind,
fileName: input.fileName,
contentType: input.contentType,
byteSize: input.byteSize,
});
}

export function normalizeUploadFilename(value: string): string | null {
const normalized = value.normalize("NFKC").trim();
if (
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions tests/security-policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
parseShowcaseSlug,
showcaseDraftSchema,
validateArtifactIntent,
validateArtifactIntentFromUploadRequest,
} from "../lib/security/policy";
import { secureJson } from "../lib/security/http";
import { zipSync, strToU8 } from "fflate";
Expand Down Expand Up @@ -461,6 +462,26 @@ test("upload intent accepts only the declared kind, MIME, and size contract", ()
);
});

test("upload request validation projects out the owning showcase id", () => {
const request = {
showcaseId: "11111111-2222-4333-8444-555555555555",
kind: "image",
fileName: "proof.png",
contentType: "image/png",
byteSize: 1024,
} as const;

assert.deepEqual(validateArtifactIntentFromUploadRequest(request), {
ok: true,
value: {
kind: "image",
fileName: "proof.png",
contentType: "image/png",
byteSize: 1024,
},
});
});

test("direct R2 uploads cryptographically bind size, type, and session", async () => {
const environment = {
accountId: process.env.R2_ACCOUNT_ID,
Expand Down
Loading