Skip to content

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

fix: remove handlebars and validate separately

Marc Seitz50a64915356a
diff --git a/components/view/watermark-svg.tsx b/components/view/watermark-svg.tsx
index a0e13736..bc696de4 100644
--- a/components/view/watermark-svg.tsx
+++ b/components/view/watermark-svg.tsx
@@ -1,8 +1,7 @@
import React, { useMemo } from "react";
-import Handlebars from "handlebars";
-
import { WatermarkConfig } from "@/lib/types";
+import { safeTemplateReplace } from "@/lib/utils";
export const SVGWatermark = ({
config,
@@ -16,8 +15,7 @@ export const SVGWatermark = ({
pageIndex: number;
}) => {
const watermarkText = useMemo(() => {
- const template = Handlebars.compile(config.text);
- return template(viewerData);
+ return safeTemplateReplace(config.text, viewerData);
}, [config.text, viewerData]);
const { width, height } = documentDimensions;
diff --git a/lib/utils.ts b/lib/utils.ts
index 5ea64a71..2ca0dd6b 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -759,3 +759,27 @@ export const parseDateTime = (str: Date | string) => {
if (str instanceof Date) return str;
return chrono.parseDate(str);
};
+
+/**
+ * Safely replaces template variables in user input with actual values.
+ * Only allows whitelisted variables to prevent template injection.
+ */
+export function safeTemplateReplace(
+ template: string,
+ data: Record<string, any>,
+): string {
+ // Define allowed template variables - only these will be replaced
+ const allowedVariables = ["email", "date", "time", "link", "ipAddress"];
+
+ let result = template;
+
+ for (const key of allowedVariables) {
+ if (data[key] !== undefined && data[key] !== null) {
+ // Use a regex to match {{variable}} patterns with optional whitespace
+ const regex = new RegExp(`{{\\s*${key}\\s*}}`, "gi");
+ result = result.replace(regex, String(data[key]));
+ }
+ }
+
+ return result;
+}
diff --git a/package-lock.json b/package-lock.json
index a4ef40dc..b35dcf01 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -76,7 +76,6 @@
"eslint-config-next": "^14.2.30",
"exceljs": "^4.4.0",
"fluent-ffmpeg": "^2.1.3",
- "handlebars": "^4.7.8",
"input-otp": "^1.4.2",
"js-cookie": "^3.0.5",
"jsonwebtoken": "^9.0.2",
@@ -10375,26 +10374,6 @@
"resolved": "https://registry.npmjs.org/graphemer/-/graphemer-1.4.0.tgz",
"integrity": "sha512-EtKwoO6kxCL9WO5xipiHTZlSzBm7WLT627TqC/uVRd0HKmq8NXyebnNYxDoBi7wt8eTWrUrKXCOVaFq9x1kgag=="
},
- "node_modules/handlebars": {
- "version": "4.7.8",
- "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.7.8.tgz",
- "integrity": "sha512-vafaFqs8MZkRrSX7sFVUdo3ap/eNiLnb4IakshzvP56X5Nr1iGKAIqdX6tMlm6HcNRIkr6AxO5jFEoJzzpT8aQ==",
- "dependencies": {
- "minimist": "^1.2.5",
- "neo-async": "^2.6.2",
- "source-map": "^0.6.1",
- "wordwrap": "^1.0.0"
- },
- "bin": {
- "handlebars": "bin/handlebars"
- },
- "engines": {
- "node": ">=0.4.7"
- },
- "optionalDependencies": {
- "uglify-js": "^3.1.4"
- }
- },
"node_modules/has-bigints": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/has-bigints/-/has-bigints-1.0.2.tgz",
@@ -12320,11 +12299,6 @@
"node": ">= 0.6"
}
},
- "node_modules/neo-async": {
- "version": "2.6.2",
- "resolved": "https://registry.npmjs.org/neo-async/-/neo-async-2.6.2.tgz",
- "integrity": "sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw=="
- },
"node_modules/neverthrow": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/neverthrow/-/neverthrow-7.2.0.tgz",
@@ -15452,14 +15426,6 @@
"react-dom": "^18.0.0 || ^19.0.0 || ^19.0.0-rc"
}
},
- "node_modules/source-map": {
- "version": "0.6.1",
- "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
- "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
- "engines": {
- "node": ">=0.10.0"
- }
- },
"node_modules/source-map-js": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.1.tgz",
@@ -16443,18 +16409,6 @@
"integrity": "sha512-UsUk3byDzKd04EyoZ7U4DOlxQaD14JUKQl6/P7wiX4FNvUfm3XL246n9W5AmqwW5RSFJ27NAuM0iLscAOYUiGQ==",
"dev": true
},
- "node_modules/uglify-js": {
- "version": "3.19.3",
- "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.19.3.tgz",
- "integrity": "sha512-v3Xu+yuwBXisp6QYTcH4UbH+xYJXqnq2m/LtQVWKWzYc1iehYnLixoQDN9FH6/j9/oybfd6W9Ghwkl8+UMKTKQ==",
- "optional": true,
- "bin": {
- "uglifyjs": "bin/uglifyjs"
- },
- "engines": {
- "node": ">=0.8.0"
- }
- },
"node_modules/ulid": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/ulid/-/ulid-2.3.0.tgz",
@@ -16957,11 +16911,6 @@
"node": ">=0.10.0"
}
},
- "node_modules/wordwrap": {
- "version": "1.0.0",
- "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-1.0.0.tgz",
- "integrity": "sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q=="
- },
"node_modules/wrap-ansi": {
"version": "8.1.0",
"resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-8.1.0.tgz",
@@ -17280,21 +17229,6 @@
"peerDependencies": {
"zod": "^3.18.0"
}
- },
- "node_modules/react-email/node_modules/@next/swc-win32-ia32-msvc": {
- "version": "14.2.30",
- "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-14.2.30.tgz",
- "integrity": "sha512-pVZMnFok5qEX4RT59mK2hEVtJX+XFfak+/rjHpyFh7juiT52r177bfFKhnlafm0UOSldhXjj32b+LZIOdswGTg==",
- "cpu": [
- "ia32"
- ],
- "optional": true,
- "os": [
- "win32"
- ],
- "engines": {
- "node": ">= 10"
- }
}
}
}
diff --git a/package.json b/package.json
index b5a2fc06..dfe0d895 100644
--- a/package.json
+++ b/package.json
@@ -87,7 +87,6 @@
"eslint-config-next": "^14.2.30",
"exceljs": "^4.4.0",
"fluent-ffmpeg": "^2.1.3",
- "handlebars": "^4.7.8",
"input-otp": "^1.4.2",
"js-cookie": "^3.0.5",
"jsonwebtoken": "^9.0.2",
diff --git a/pages/api/mupdf/annotate-document.ts b/pages/api/mupdf/annotate-document.ts
index a1b28cd2..85cd583c 100644
--- a/pages/api/mupdf/annotate-document.ts
+++ b/pages/api/mupdf/annotate-document.ts
@@ -1,10 +1,9 @@
import { NextApiRequest, NextApiResponse } from "next";
import fontkit from "@pdf-lib/fontkit";
-import Handlebars from "handlebars";
import { PDFDocument, StandardFonts, degrees, rgb } from "pdf-lib";
-import { hexToRgb, log } from "@/lib/utils";
+import { hexToRgb, log, safeTemplateReplace } from "@/lib/utils";
// This function can run for a maximum of 120 seconds
export const config = {
@@ -71,9 +70,8 @@ async function insertWatermark(
const font = await pdfDoc.embedFont(StandardFonts.Helvetica);
- // Compile the Handlebars template
- const template = Handlebars.compile(config.text);
- const rawWatermarkText = template(viewerData);
+ // Safely replace template variables with whitelisted values only
+ const rawWatermarkText = safeTemplateReplace(config.text, viewerData);
// Handle Unicode characters that can't be encoded in WinAnsi
const sanitizeText = (text: string): string => {