Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add zod validation
Marc Seitzabb2b874319b
diff --git a/ee/features/templates/api/datarooms/apply-template.ts b/ee/features/templates/api/datarooms/apply-template.tsindex a7995e4d..ae968f41 100644--- a/ee/features/templates/api/datarooms/apply-template.ts+++ b/ee/features/templates/api/datarooms/apply-template.ts@@ -4,6 +4,7 @@ import {DATAROOM_TEMPLATES,FolderTemplate,} from "@/ee/features/templates/constants/dataroom-templates";+import { applyTemplateSchema } from "@/ee/features/templates/schemas/dataroom-templates";import { authOptions } from "@/pages/api/auth/[...nextauth]";import slugify from "@sindresorhus/slugify";import { getServerSession } from "next-auth/next";@@ -28,15 +29,19 @@ export default async function handle(teamId: string;id: string;};- const { type } = req.body as { type: string };- // Validate the type- if (!type || !DATAROOM_TEMPLATES[type]) {+ // Validate request body using Zod schema for SSRF protection+ const validationResult = applyTemplateSchema.safeParse(req.body);++ if (!validationResult.success) {return res.status(400).json({message: "Invalid template type.",+ errors: validationResult.error.flatten().fieldErrors,});}+ const { type } = validationResult.data;+try {// Check if the user is part of the team and has access to the dataroomconst dataroom = await prisma.dataroom.findUnique({diff --git a/ee/features/templates/api/datarooms/generate.ts b/ee/features/templates/api/datarooms/generate.tsindex 54d24211..7dfc68a8 100644--- a/ee/features/templates/api/datarooms/generate.ts+++ b/ee/features/templates/api/datarooms/generate.ts@@ -4,6 +4,7 @@ import {DATAROOM_TEMPLATES,FolderTemplate,} from "@/ee/features/templates/constants/dataroom-templates";+import { generateDataroomSchema } from "@/ee/features/templates/schemas/dataroom-templates";import { getLimits } from "@/ee/limits/server";import { authOptions } from "@/pages/api/auth/[...nextauth]";import slugify from "@sindresorhus/slugify";@@ -28,15 +29,19 @@ export default async function handle(const userId = (session.user as CustomUser).id;const { teamId } = req.query as { teamId: string };- const { name, type } = req.body as { name: string; type: string };- // Validate the type- if (!type || !DATAROOM_TEMPLATES[type]) {+ // Validate request body using Zod schema for SSRF protection+ const validationResult = generateDataroomSchema.safeParse(req.body);++ if (!validationResult.success) {return res.status(400).json({message: "Invalid dataroom type. Please select a valid type.",+ errors: validationResult.error.flatten().fieldErrors,});}+ const { name, type } = validationResult.data;+try {// Check if the user is part of the teamconst team = await prisma.team.findUnique({diff --git a/ee/features/templates/schemas/dataroom-templates.ts b/ee/features/templates/schemas/dataroom-templates.tsnew file mode 100644index 00000000..44fe1a07--- /dev/null+++ b/ee/features/templates/schemas/dataroom-templates.ts@@ -0,0 +1,94 @@+import { z } from "zod";++/**+ * Valid dataroom template types+ */+export const DATAROOM_TEMPLATE_TYPES = [+ "startup-fundraising",+ "raising-first-fund",+ "ma-acquisition",+ "series-a-plus",+ "real-estate-transaction",+ "fund-management",+ "portfolio-management",+ "project-management",+ "sales-dataroom",+] as const;++/**+ * Schema for validating template type selection+ * Protects against invalid template types and injection attacks+ */+export const applyTemplateSchema = z.object({+ type: z+ .enum(DATAROOM_TEMPLATE_TYPES, {+ errorMap: () => ({+ message: `Invalid template type. Must be one of: ${DATAROOM_TEMPLATE_TYPES.join(", ")}`,+ }),+ })+ .refine(+ (type) => {+ // Additional validation: ensure no path traversal or special characters+ return (+ !type.includes("..") &&+ !type.includes("/") &&+ !type.includes("\\") &&+ !type.includes("\0")+ );+ },+ {+ message: "Template type contains invalid characters",+ },+ ),+});++/**+ * Schema for validating dataroom generation request+ * Includes both name validation and template type validation+ */+export const generateDataroomSchema = z.object({+ name: z+ .string()+ .min(1, "Dataroom name is required")+ .max(255, "Dataroom name is too long")+ .refine(+ (name) => {+ // Ensure no malicious characters in the name+ return (+ !name.includes("\0") &&+ !name.includes("..") &&+ !name.includes("<script>")+ );+ },+ {+ message: "Dataroom name contains invalid characters",+ },+ ),+ type: z+ .enum(DATAROOM_TEMPLATE_TYPES, {+ errorMap: () => ({+ message: `Invalid template type. Must be one of: ${DATAROOM_TEMPLATE_TYPES.join(", ")}`,+ }),+ })+ .refine(+ (type) => {+ // Additional validation: ensure no path traversal or special characters+ return (+ !type.includes("..") &&+ !type.includes("/") &&+ !type.includes("\\") &&+ !type.includes("\0")+ );+ },+ {+ message: "Template type contains invalid characters",+ },+ ),+});++/**+ * Type exports for TypeScript+ */+export type ApplyTemplateInput = z.infer<typeof applyTemplateSchema>;+export type GenerateDataroomInput = z.infer<typeof generateDataroomSchema>;+export type DataroomTemplateType = (typeof DATAROOM_TEMPLATE_TYPES)[number];