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.

Rate limit resume uploads and stop returning the storage key

Kiran K2fdc29ddc1c6
diff --git a/apps/web/app/api/resumes/upload-url/route.ts b/apps/web/app/api/resumes/upload-url/route.ts
index c2ca2e0c15..2f2dbeebd5 100644
--- a/apps/web/app/api/resumes/upload-url/route.ts
+++ b/apps/web/app/api/resumes/upload-url/route.ts
@@ -2,29 +2,25 @@ 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 { assertRateLimit } from "@/lib/upstash/assert-rate-limit";
+import { RATELIMIT_POLICIES } from "@/lib/upstash/ratelimit-policies";
import { LOCALHOST_IP, nanoid } from "@dub/utils";
import { ipAddress } from "@vercel/functions";
import { NextRequest, NextResponse } from "next/server";
-function getCorsHeaders(req: NextRequest) {
- const headers = new Headers({
- "Access-Control-Allow-Methods": "POST",
- "Access-Control-Allow-Headers": "Content-Type",
- });
+const CORS_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"))) {
- headers.set("Access-Control-Allow-Origin", origin);
+ CORS_HEADERS["Access-Control-Allow-Origin"] = origin;
}
- return headers;
-}
-
-// POST /api/resumes/upload-url – get a signed URL to upload a resume
-export const POST = async (req: NextRequest) => {
- const corsHeaders = getCorsHeaders(req);
-
try {
const { contentType, contentLength } = signedUploadInputSchema.parse(
await req.json(),
@@ -36,33 +32,31 @@ export const POST = async (req: NextRequest) => {
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}`);
- if (!success) {
- return new Response("Don't DDoS me pls 🥺", {
- status: 429,
- headers: corsHeaders,
+ if (ip) {
+ await assertRateLimit({
+ policy: RATELIMIT_POLICIES.resumeUpload,
+ identifier: [ip],
});
}
- const key = `resumes/${nanoid(16)}`;
const { signedUrl, destinationUrl } = await createSignedUploadUrl({
- key,
+ key: `resumes/${nanoid(10)}`,
contentType,
contentLength,
});
return NextResponse.json(
{
- key,
signedUrl,
destinationUrl,
},
- { headers: corsHeaders },
+ {
+ headers: CORS_HEADERS,
+ },
);
} catch (error) {
- return handleAndReturnErrorResponse(error, corsHeaders);
+ return handleAndReturnErrorResponse(error, CORS_HEADERS);
}
};
diff --git a/apps/web/lib/upstash/ratelimit-policies.ts b/apps/web/lib/upstash/ratelimit-policies.ts
index 34cfc59acc..cb39b0341e 100644
--- a/apps/web/lib/upstash/ratelimit-policies.ts
+++ b/apps/web/lib/upstash/ratelimit-policies.ts
@@ -202,4 +202,11 @@ export const RATELIMIT_POLICIES = {
keyPrefix: "rl:partner:identity:verification:start",
message: "Too many verification attempts. Please try again later.",
},
+
+ resumeUpload: {
+ attempts: 5,
+ window: "1 h",
+ keyPrefix: "rl:resume:upload",
+ message: "Too many resume uploads. Please try again later.",
+ },
} as const satisfies Record<string, RatelimitPolicy>;