Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add validation and restrictions to pages retrieval
Marc Seitz3aeb611284a8
diff --git a/app/api/views/pages/route.ts b/app/api/views/pages/route.tsindex fdb0a080..2de1573a 100644--- a/app/api/views/pages/route.ts+++ b/app/api/views/pages/route.ts@@ -1,60 +1,94 @@import { NextRequest, NextResponse } from "next/server";+import { z } from "zod";++import { verifyDataroomSession } from "@/lib/auth/dataroom-auth";import { getFile } from "@/lib/files/get-file";import prisma from "@/lib/prisma";+import { ratelimit } from "@/lib/redis";import { log } from "@/lib/utils";-const MAX_PAGES_PER_REQUEST = 50;+const MAX_PAGES_PER_REQUEST = 15;+const VIEW_MAX_AGE_MS = 23 * 60 * 60 * 1000; // 23 hours++const requestSchema = z.object({+ viewId: z.string().cuid(),+ documentVersionId: z.string().cuid(),+ pageNumbers: z+ .array(z.number().int().positive())+ .min(1)+ .max(MAX_PAGES_PER_REQUEST),+});export async function POST(request: NextRequest) {try {const body = await request.json();- const { viewId, documentVersionId, pageNumbers } = body as {- viewId: string;- documentVersionId: string;- pageNumbers: number[];- };-- if (!documentVersionId || !pageNumbers || pageNumbers.length === 0) {+ const parsed = requestSchema.safeParse(body);+ if (!parsed.success) {return NextResponse.json(- { message: "Missing required fields." },+ { message: "Invalid request." },{ status: 400 },);}- if (pageNumbers.length > MAX_PAGES_PER_REQUEST) {- return NextResponse.json(- {- message: `Cannot request more than ${MAX_PAGES_PER_REQUEST} pages at once.`,+ const { viewId, documentVersionId, pageNumbers } = parsed.data;++ // Run rate limit, view lookup, and version lookup in parallel+ const [rateLimitResult, view, documentVersion] = await Promise.all([+ ratelimit(60, "1 m").limit(`view-pages:${viewId}`),+ prisma.view.findUnique({+ where: { id: viewId },+ select: {+ id: true,+ documentId: true,+ dataroomId: true,+ linkId: true,+ viewedAt: true,},- { status: 400 },+ }),+ prisma.documentVersion.findUnique({+ where: { id: documentVersionId },+ select: { documentId: true },+ }),+ ]);++ if (!rateLimitResult.success) {+ return NextResponse.json(+ { message: "Too many requests. Please try again later." },+ { status: 429 },);}- if (viewId) {- const view = await prisma.view.findUnique({- where: { id: viewId },- select: { id: true, documentId: true },- });+ if (!view) {+ return NextResponse.json({ message: "View not found." }, { status: 404 });+ }- if (!view) {- return NextResponse.json(- { message: "View not found." },- { status: 404 },- );- }+ if (Date.now() - view.viewedAt.getTime() > VIEW_MAX_AGE_MS) {+ return NextResponse.json(+ { message: "View session expired." },+ { status: 401 },+ );+ }- // Verify the document version belongs to the document associated with this view- const documentVersion = await prisma.documentVersion.findUnique({- where: { id: documentVersionId },- select: { documentId: true },- });+ if (!documentVersion || documentVersion.documentId !== view.documentId) {+ return NextResponse.json(+ { message: "Unauthorized access." },+ { status: 403 },+ );+ }- if (!documentVersion || documentVersion.documentId !== view.documentId) {+ // Validate dataroom session for dataroom document views+ if (view.dataroomId && view.linkId) {+ const session = await verifyDataroomSession(+ request,+ view.linkId,+ view.dataroomId,+ );+ if (!session) {return NextResponse.json(- { message: "Unauthorized access." },- { status: 403 },+ { message: "Invalid or expired session." },+ { status: 401 },);}}