Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Pin content type and size on resume signed uploads

Cursor Agent05b6b0f411b4
diff --git a/apps/web/app/api/resumes/upload-url/route.ts b/apps/web/app/api/resumes/upload-url/route.ts
index 0adb8290c5..c2ca2e0c15 100644
--- a/apps/web/app/api/resumes/upload-url/route.ts
+++ b/apps/web/app/api/resumes/upload-url/route.ts
@@ -1,42 +1,68 @@
-import { storage } from "@/lib/storage";
+import { handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { createSignedUploadUrl } from "@/lib/storage/create-signed-upload-url";
+import { signedUploadInputSchema } from "@/lib/storage/schemas";
+import { validateSignedUpload } from "@/lib/storage/validate-signed-upload";
import { ratelimit } from "@/lib/upstash";
-import { LOCALHOST_IP, nanoid, R2_URL } from "@dub/utils";
+import { LOCALHOST_IP, nanoid } from "@dub/utils";
import { ipAddress } from "@vercel/functions";
import { NextRequest, NextResponse } from "next/server";
-const CORS_HEADERS = new Headers({
- "Access-Control-Allow-Methods": "POST",
- "Access-Control-Allow-Headers": "Content-Type",
-});
+function getCorsHeaders(req: NextRequest) {
+ const headers = new Headers({
+ "Access-Control-Allow-Methods": "POST",
+ "Access-Control-Allow-Headers": "Content-Type",
+ });
-// POST /api/resumes/upload-url – get a signed URL to upload a resume
-export const POST = async (req: NextRequest) => {
const origin = req.headers.get("origin");
-
if (origin && (origin === "https://dub.co" || origin.endsWith(".dub.co"))) {
- CORS_HEADERS["Access-Control-Allow-Origin"] = origin;
+ headers.set("Access-Control-Allow-Origin", origin);
}
- // Max 5 requests per minute
- const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP;
- const { success } = await ratelimit(5, "1 m").limit(`upload-resume:${ip}`);
+ return headers;
+}
- if (!success) {
- return new Response("Don't DDoS me pls 🥺", { status: 429 });
- }
+// POST /api/resumes/upload-url – get a signed URL to upload a resume
+export const POST = async (req: NextRequest) => {
+ const corsHeaders = getCorsHeaders(req);
- const key = `resumes/${nanoid(16)}`;
- const signedUrl = await storage.getSignedUploadUrl({
- key,
- contentType: "application/pdf",
- });
+ try {
+ const { contentType, contentLength } = signedUploadInputSchema.parse(
+ await req.json(),
+ );
+
+ validateSignedUpload({
+ contentLength,
+ contentType,
+ policy: "resumes",
+ });
+
+ // Max 5 requests per minute
+ const ip = process.env.VERCEL === "1" ? ipAddress(req) : LOCALHOST_IP;
+ const { success } = await ratelimit(5, "1 m").limit(`upload-resume:${ip}`);
- return NextResponse.json(
- {
+ if (!success) {
+ return new Response("Don't DDoS me pls 🥺", {
+ status: 429,
+ headers: corsHeaders,
+ });
+ }
+
+ const key = `resumes/${nanoid(16)}`;
+ const { signedUrl, destinationUrl } = await createSignedUploadUrl({
key,
- signedUrl,
- destinationUrl: `${R2_URL}/${key}`,
- },
- { headers: CORS_HEADERS },
- );
+ contentType,
+ contentLength,
+ });
+
+ return NextResponse.json(
+ {
+ key,
+ signedUrl,
+ destinationUrl,
+ },
+ { headers: corsHeaders },
+ );
+ } catch (error) {
+ return handleAndReturnErrorResponse(error, corsHeaders);
+ }
};
diff --git a/apps/web/lib/storage/upload-policies.ts b/apps/web/lib/storage/upload-policies.ts
index 640fcdd254..71359f94e7 100644
--- a/apps/web/lib/storage/upload-policies.ts
+++ b/apps/web/lib/storage/upload-policies.ts
@@ -95,6 +95,11 @@ export const UPLOAD_POLICIES = {
contentTypes: ["image/jpeg", "image/png", "image/webp"] as const,
maxBytes: 5 * 1024 * 1024,
},
+
+ resumes: {
+ contentTypes: ["application/pdf"] as const,
+ maxBytes: 5 * 1024 * 1024,
+ },
} as const;
export function getMimeTypeLabel(mimeType: string): string {
diff --git a/apps/web/tests/misc/create-signed-upload-url.test.ts b/apps/web/tests/misc/create-signed-upload-url.test.ts
index 9474e8ed25..5b0222651e 100644
--- a/apps/web/tests/misc/create-signed-upload-url.test.ts
+++ b/apps/web/tests/misc/create-signed-upload-url.test.ts
@@ -70,6 +70,7 @@ describe("createSignedUploadUrl", () => {
"programResourceLogos",
"programResourceFiles",
"bountySubmissionImages",
+ "resumes",
] as const satisfies ReadonlyArray<keyof typeof UPLOAD_POLICIES>)(
"pins Content-Type and Content-Length into the signer for %s",
async (policy) => {
@@ -200,6 +201,7 @@ describe("validateSignedUpload", () => {
"programResourceLogos",
"programResourceFiles",
"bountySubmissionImages",
+ "resumes",
] as const satisfies ReadonlyArray<keyof typeof UPLOAD_POLICIES>;
const xssContentTypes = [