Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

fix: ratelimiting to endpoint

Marc Seitz40889191dd0c
diff --git a/pages/api/links/download/by-email.ts b/pages/api/links/download/by-email.ts
index e4541c5e..4862a650 100644
--- a/pages/api/links/download/by-email.ts
+++ b/pages/api/links/download/by-email.ts
@@ -1,6 +1,9 @@
import { NextApiRequest, NextApiResponse } from "next";
+import { getDataroomSessionByLinkIdInPagesRouter } from "@/lib/auth/dataroom-auth";
import prisma from "@/lib/prisma";
+import { ratelimit } from "@/lib/redis";
+import { getIpAddress } from "@/lib/utils/ip";
export default async function handler(
req: NextApiRequest,
@@ -11,12 +14,30 @@ export default async function handler(
return res.status(405).end(`Method ${req.method} Not Allowed`);
}
+ // 1. IP-based rate limiting (same pattern as OTP endpoints in verify.ts)
+ const ipAddress = getIpAddress(req.headers) ?? "unknown";
+ const { success } = await ratelimit(10, "1 m").limit(
+ `download-by-email-ip:${ipAddress}`,
+ );
+ if (!success) {
+ return res
+ .status(429)
+ .json({ error: "Too many requests. Try again later." });
+ }
+
const linkId = req.query.linkId as string;
const email = req.query.email as string;
if (!linkId || !email) {
return res.status(400).json({ error: "linkId and email are required" });
}
+ // 2. Require a valid dataroom session
+ const session = await getDataroomSessionByLinkIdInPagesRouter(req, linkId);
+ if (!session) {
+ return res.status(401).json({ error: "Session required" });
+ }
+
+ // 3. Prevent email enumeration — always return 200 with uniform body
const view = await prisma.view.findFirst({
where: {
linkId,
@@ -27,9 +48,5 @@ export default async function handler(
orderBy: { viewedAt: "desc" },
});
- if (!view) {
- return res.status(404).json({ error: "No view found for this link and email" });
- }
-
- return res.status(200).json({ viewId: view.id });
+ return res.status(200).json({ viewId: view?.id ?? null });
}