Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add url validation
Marc Seitz3103b437a228
diff --git a/lib/zod/url-validation.ts b/lib/zod/url-validation.tsnew file mode 100644index 00000000..c7e33c21--- /dev/null+++ b/lib/zod/url-validation.ts@@ -0,0 +1,242 @@+import { z } from "zod";++import {+ SUPPORTED_DOCUMENT_MIME_TYPES,+ SUPPORTED_DOCUMENT_SIMPLE_TYPES,+} from "@/lib/constants";+import { getSupportedContentType } from "@/lib/utils/get-content-type";++/**+ * Validates basic security aspects of paths and URLs+ * Prevents directory traversal, null byte injection, and double encoding attacks+ */+export const validatePathSecurity = (pathOrUrl: string): boolean => {+ // Prevent directory traversal attacks+ if (pathOrUrl.includes("../") || pathOrUrl.includes("..\\")) {+ return false;+ }++ // Prevent null byte attacks+ if (pathOrUrl.includes("\0")) {+ return false;+ }++ // Prevent double encoding attacks+ if (pathOrUrl.includes("%2E%2E") || pathOrUrl.includes("%2F%2F")) {+ return false;+ }++ return true;+};++/**+ * Validates URL for SSRF protection+ * Blocks internal/private IP ranges, localhost, and link-local addresses+ */+export const validateUrlSSRFProtection = (url: string): boolean => {+ try {+ const urlObj = new URL(url);+ const hostname = urlObj.hostname;++ // Block localhost/loopback+ if (+ hostname === "localhost" ||+ hostname === "127.0.0.1" ||+ hostname === "::1"+ ) {+ return false;+ }++ // Block private IP ranges (simplified check)+ if (hostname.match(/^10\.|^172\.(1[6-9]|2[0-9]|3[01])\.|^192\.168\./)) {+ return false;+ }++ // Block link-local addresses+ if (hostname.match(/^169\.254\./)) {+ return false;+ }++ return true;+ } catch {+ return false;+ }+};++/**+ * Comprehensive security validation for URLs+ * Combines path security and SSRF protection+ */+export const validateUrlSecurity = (url: string): boolean => {+ return validatePathSecurity(url) && validateUrlSSRFProtection(url);+};++// Custom validator for file paths - either Notion URLs or S3 storage paths+const createFilePathValidator = () => {+ return z+ .string()+ .min(1, "File path is required")+ .refine(+ (path) => {+ // Case 1: Notion URLs - must start with notion domains+ if (path.startsWith("https://")) {+ try {+ const urlObj = new URL(path);+ const hostname = urlObj.hostname;++ // Valid notion domains+ const validNotionDomains = ["www.notion.so", "notion.so"];++ // Check for notion.site subdomains (e.g., example-something.notion.site)+ const isNotionSite = hostname.endsWith(".notion.site");+ const isValidNotionDomain = validNotionDomains.includes(hostname);++ // Check for vercel blob storage+ let isVercelBlob = false;+ if (process.env.VERCEL_BLOB_HOST) {+ isVercelBlob = hostname.startsWith(process.env.VERCEL_BLOB_HOST);+ }++ return isNotionSite || isValidNotionDomain || isVercelBlob;+ } catch {+ return false;+ }+ }++ // Case 2: file storage paths - must match pattern: <id>/doc_<someId>/<name>.<ext>+ const s3PathPattern =+ /^[a-zA-Z0-9_-]+\/doc_[a-zA-Z0-9_-]+\/[a-zA-Z0-9_.-]+\.[a-zA-Z0-9]+$/;+ return s3PathPattern.test(path);+ },+ {+ message:+ "File path must be either a Notion URL or an file storage path",+ },+ )+ .refine(+ (path) => {+ // Additional security checks for all paths+ return validatePathSecurity(path);+ },+ {+ message: "File path contains invalid or malicious characters",+ },+ );+};++// File path validation schema+export const filePathSchema = createFilePathValidator();++// Document upload validation schema with comprehensive type and content validation+export const documentUploadSchema = z+ .object({+ name: z+ .string()+ .min(1, "Document name is required")+ .max(255, "Document name too long"),+ url: filePathSchema,+ storageType: z.enum(["S3_PATH", "VERCEL_BLOB"], {+ errorMap: () => ({ message: "Invalid storage type" }),+ }),+ numPages: z.number().int().positive().optional(),+ type: z.enum(+ SUPPORTED_DOCUMENT_SIMPLE_TYPES as unknown as readonly [+ string,+ ...string[],+ ],+ {+ errorMap: () => ({+ message: `File type must be one of: ${SUPPORTED_DOCUMENT_SIMPLE_TYPES.join(", ")}`,+ }),+ },+ ),+ folderPathName: z.string().optional(),+ contentType: z+ .enum(+ SUPPORTED_DOCUMENT_MIME_TYPES as unknown as readonly [+ string,+ ...string[],+ ],+ {+ errorMap: () => ({ message: "Unsupported content type" }),+ },+ )+ .or(z.literal("text/html")), // Allow text/html for Notion documents+ createLink: z.boolean().optional(),+ fileSize: z.number().int().positive().optional(),+ })+ .refine(+ (data) => {+ // Validate that content type matches the declared file type+ const expectedType = getSupportedContentType(data.contentType);++ // Special case for Notion documents+ if (data.contentType === "text/html" && data.type === "notion") {+ return true;+ }++ return expectedType === data.type;+ },+ {+ message: "Content type does not match the declared file type",+ path: ["contentType"], // This will highlight the contentType field in errors+ },+ )+ .refine(+ (data) => {+ // Validate storage type consistency with path format+ if (data.storageType === "S3_PATH") {+ // S3_PATH should use file paths, not URLs+ return !data.url.startsWith("https://");+ } else if (data.storageType === "VERCEL_BLOB") {+ // VERCEL_BLOB can use either Notion URLs or S3 paths (for migration)+ if (data.url.startsWith("https://")) {+ // Must be a Notion URL for VERCEL_BLOB+ try {+ const urlObj = new URL(data.url);+ const hostname = urlObj.hostname;+ return (+ hostname === "www.notion.so" ||+ hostname === "notion.so" ||+ hostname.endsWith(".notion.site")+ );+ } catch {+ return false;+ }+ }+ // Or an S3 path (allowed for migration)+ return /^[a-zA-Z0-9_-]+\/doc_[a-zA-Z0-9_-]+\/[a-zA-Z0-9_.-]+\.[a-zA-Z0-9]+$/.test(+ data.url,+ );+ }+ return false;+ },+ {+ message: "Storage type does not match the URL/path format",+ path: ["storageType"],+ },+ );++// Webhook file URL validator - only allows trusted external sources for webhooks+export const webhookFileUrlSchema = z+ .string()+ .url("Invalid URL format")+ .refine(+ (url) => {+ // Must use HTTPS+ return url.startsWith("https://");+ },+ {+ message: "Webhook file URL must use HTTPS protocol",+ },+ )+ .refine(+ (url) => {+ // Comprehensive security validation including SSRF protection+ return validateUrlSecurity(url);+ },+ {+ message:+ "Webhook file URL contains invalid characters or targets internal resources",+ },+ );diff --git a/package.json b/package.jsonindex 28fd4e79..16a593e7 100644--- a/package.json+++ b/package.json@@ -13,8 +13,8 @@"postinstall": "prisma generate","vercel-build": "prisma migrate deploy && next build","email": "email dev --dir ./components/emails --port 3001",- "trigger:v3:dev": "npx trigger.dev@latest dev",- "trigger:v3:deploy": "npx trigger.dev@latest deploy",+ "trigger:v3:dev": "npx trigger.dev@3 dev",+ "trigger:v3:deploy": "npx trigger.dev@3 deploy","stripe:webhook": "pkgx stripe listen --forward-to localhost:3000/api/stripe/webhook","format": "prettier --write \"**/*.{js,jsx,ts,tsx,mdx}\"","dev:prisma": "npx prisma generate && npx prisma migrate deploy"diff --git a/pages/api/teams/[teamId]/documents/[id]/versions/index.ts b/pages/api/teams/[teamId]/documents/[id]/versions/index.tsindex 9f7bd929..164992f5 100644--- a/pages/api/teams/[teamId]/documents/[id]/versions/index.ts+++ b/pages/api/teams/[teamId]/documents/[id]/versions/index.ts@@ -13,6 +13,7 @@ import { convertPdfToImageRoute } from "@/lib/trigger/pdf-to-image-route";import { CustomUser } from "@/lib/types";import { log } from "@/lib/utils";import { conversionQueue } from "@/lib/utils/trigger-utils";+import { documentUploadSchema } from "@/lib/zod/url-validation";export default async function handle(req: NextApiRequest,@@ -30,15 +31,25 @@ export default async function handle(teamId: string;id: string;};+ // Validate request body using Zod schema for security+ const validationResult = documentUploadSchema.safeParse({+ ...req.body,+ name: `Version ${new Date().toISOString()}`, // Dummy name for validation+ });++ if (!validationResult.success) {+ log({+ message: `Document version validation failed for documentId: ${documentId}, teamId: ${teamId}. Errors: ${JSON.stringify(validationResult.error.errors)}`,+ type: "error",+ });+ return res.status(400).json({+ error: "Invalid document version data",+ details: validationResult.error.errors,+ });+ }+const { url, type, numPages, storageType, contentType, fileSize } =- req.body as {- url: string;- type: string;- numPages: number;- storageType: DocumentStorageType;- contentType: string;- fileSize: number | undefined;- };+ validationResult.data;const userId = (session.user as CustomUser).id;diff --git a/pages/api/teams/[teamId]/documents/agreement.ts b/pages/api/teams/[teamId]/documents/agreement.tsindex 9fe6fa45..ddd8a7ea 100644--- a/pages/api/teams/[teamId]/documents/agreement.ts+++ b/pages/api/teams/[teamId]/documents/agreement.ts@@ -11,6 +11,7 @@ import { convertPdfToImageRoute } from "@/lib/trigger/pdf-to-image-route";import { CustomUser } from "@/lib/types";import { getExtension, log, serializeFileSize } from "@/lib/utils";import { conversionQueue } from "@/lib/utils/trigger-utils";+import { documentUploadSchema } from "@/lib/zod/url-validation";import { authOptions } from "../../../auth/[...nextauth]";@@ -30,7 +31,24 @@ export default async function handle(const userId = (session.user as CustomUser).id;- // Assuming data is an object with `name` and `description` properties+ // Validate request body using Zod schema for security+ const validationResult = documentUploadSchema.safeParse({+ ...req.body,+ // Ensure type field is provided for validation+ type: req.body.type || getExtension(req.body.name),+ });++ if (!validationResult.success) {+ log({+ message: `Agreement document validation failed for teamId: ${teamId}. Errors: ${JSON.stringify(validationResult.error.errors)}`,+ type: "error",+ });+ return res.status(400).json({+ error: "Invalid agreement document data",+ details: validationResult.error.errors,+ });+ }+const {name,url: fileUrl,@@ -40,16 +58,7 @@ export default async function handle(folderPathName,fileSize,contentType,- } = req.body as {- name: string;- url: string;- storageType: DocumentStorageType;- numPages?: number;- type?: string;- folderPathName?: string;- fileSize?: number;- contentType: string;- };+ } = validationResult.data;try {const { team } = await getTeamWithUsersAndDocument({diff --git a/pages/api/teams/[teamId]/documents/index.ts b/pages/api/teams/[teamId]/documents/index.tsindex 6ec9f4cc..dba5a0b4 100644--- a/pages/api/teams/[teamId]/documents/index.ts+++ b/pages/api/teams/[teamId]/documents/index.ts@@ -12,6 +12,7 @@ import { getTeamWithUsersAndDocument } from "@/lib/team/helper";import { CustomUser } from "@/lib/types";import { log, serializeFileSize } from "@/lib/utils";import { supportsAdvancedExcelMode } from "@/lib/utils/get-content-type";+import { documentUploadSchema } from "@/lib/zod/url-validation";export const config = {// in order to enable `waitUntil` function@@ -240,7 +241,20 @@ export default async function handle(userId = (session.user as CustomUser).id;}- // Assuming data is an object with `name` and `description` properties+ // Validate request body using Zod schema for security+ const validationResult = documentUploadSchema.safeParse(req.body);++ if (!validationResult.success) {+ log({+ message: `Document upload validation failed for teamId: ${teamId}. Errors: ${JSON.stringify(validationResult.error.errors)}`,+ type: "error",+ });+ return res.status(400).json({+ error: "Invalid document upload data",+ details: validationResult.error.errors,+ });+ }+const {name,url: fileUrl,@@ -251,17 +265,7 @@ export default async function handle(contentType,createLink,fileSize,- } = req.body as {- name: string;- url: string;- storageType: DocumentStorageType;- numPages?: number;- type: string;- folderPathName?: string;- contentType: string;- createLink?: boolean;- fileSize?: number;- };+ } = validationResult.data;try {const { team } = await getTeamWithUsersAndDocument({diff --git a/pages/api/webhooks/services/[...path]/index.ts b/pages/api/webhooks/services/[...path]/index.tsindex 46fddacc..fd65b735 100644--- a/pages/api/webhooks/services/[...path]/index.ts+++ b/pages/api/webhooks/services/[...path]/index.ts@@ -19,6 +19,7 @@ import {} from "@/lib/utils";import { getSupportedContentType } from "@/lib/utils/get-content-type";import { sendLinkCreatedWebhook } from "@/lib/webhook/triggers/link-created";+import { webhookFileUrlSchema } from "@/lib/zod/url-validation";export const config = {// in order to enable `waitUntil` function@@ -53,7 +54,7 @@ const BaseSchema = z.object({const DocumentCreateSchema = BaseSchema.extend({resourceType: z.literal("document.create"),- fileUrl: z.string().url(),+ fileUrl: webhookFileUrlSchema,name: z.string(),contentType: z.string(),dataroomId: z.string().optional(),@@ -287,12 +288,21 @@ async function handleDocumentCreate(return res.status(400).json({ error: "Failed to fetch file from URL" });}- // 5. Convert to buffer+ // 5. Validate response content type matches expected+ const responseContentType = response.headers.get("content-type");+ if (responseContentType && !responseContentType.startsWith(contentType)) {+ console.warn(+ `Content type mismatch: expected ${contentType}, got ${responseContentType}`,+ );+ // Log but don't fail - some services return generic types+ }++ // 6. Convert to bufferconst fileBuffer = Buffer.from(await response.arrayBuffer());console.log("Uploading file to storage", teamId, name, contentType);- // Upload the file to storage+ // 7. Upload the file to storageconst { type: storageType, data: fileData } = await putFileServer({file: {name: name,@@ -307,7 +317,7 @@ async function handleDocumentCreate(return res.status(500).json({ error: "Failed to save file to storage" });}- // 6. Create document using our service+ // 8. Create document using our service// Note: The createDocument function doesn't accept linkData in its parameters// so we will just pass createLink flagconst documentCreationResponse = await createDocument({