The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Sanitize and limit partner invite email body
Marcus Farrell15db2646bec7
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/invite-partner-sheet.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/invite-partner-sheet.tsxindex ebec2c8573..c0ffc889e2 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/invite-partner-sheet.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/invite-partner-sheet.tsx@@ -104,7 +104,13 @@ function InvitePartnerSheetContent({ setIsOpen }: InvitePartnerSheetProps) {const handleSaveEmail = () => {const sanitizedSubject = draftEmailContent.subject.trim();const sanitizedTitle = draftEmailContent.title.trim();- const sanitizedBody = draftEmailContent.body.trim();+ let sanitizedBody = draftEmailContent.body.trim();++ // Enforce max length validation (matches schema)+ if (sanitizedBody.length > 3000) {+ sanitizedBody = sanitizedBody.substring(0, 3000);+ toast.error("Email body was truncated to 3000 characters");+ }const updatedContent: EmailContent = {subject: sanitizedSubject || defaultEmailContent.subject,@@ -390,7 +396,7 @@ function EmailPreview({<textareaid="email-body"rows={8}- maxLength={10000}+ maxLength={3000}onKeyDown={handleKeyDown}value={draftEmailContent.body}onChange={(e) =>diff --git a/apps/web/lib/actions/partners/invite-partner.ts b/apps/web/lib/actions/partners/invite-partner.tsindex 5276a4084e..d21c255f56 100644--- a/apps/web/lib/actions/partners/invite-partner.ts+++ b/apps/web/lib/actions/partners/invite-partner.ts@@ -4,6 +4,7 @@ import { recordAuditLog } from "@/lib/api/audit-logs/record-audit-log";import { createAndEnrollPartner } from "@/lib/api/partners/create-and-enroll-partner";import { getPartnerInviteRewardsAndBounties } from "@/lib/api/partners/get-partner-invite-rewards-and-bounties";import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";+import { sanitizeMarkdown } from "@/lib/partners/sanitize-markdown";import { invitePartnerSchema } from "@/lib/zod/schemas/partners";import { sendEmail } from "@dub/email";import ProgramInvite from "@dub/email/templates/program-invite";@@ -69,6 +70,9 @@ export const invitePartnerAction = authActionClientstatus: "invited",});+ // Sanitize emailBody before passing to template+ const sanitizedEmailBody = emailBody ? sanitizeMarkdown(emailBody) : null;+try {await sendEmail({subject:@@ -86,7 +90,7 @@ export const invitePartnerAction = authActionClient},...(emailSubject && { subject: emailSubject }),...(emailTitle && { title: emailTitle }),- ...(emailBody && { body: emailBody }),+ ...(sanitizedEmailBody && { body: sanitizedEmailBody }),...(await getPartnerInviteRewardsAndBounties({programId,groupId: enrolledPartner.groupId || program.defaultGroupId,diff --git a/apps/web/lib/partners/sanitize-markdown.ts b/apps/web/lib/partners/sanitize-markdown.tsnew file mode 100644index 0000000000..9ea8278235--- /dev/null+++ b/apps/web/lib/partners/sanitize-markdown.ts@@ -0,0 +1,50 @@+"server-only";++/**+ * Sanitizes and validates markdown content for safe use in email templates.+ *+ * This function:+ * - Trims whitespace+ * - Validates the content is valid text (not binary)+ * - Checks for suspicious patterns that could cause DoS issues+ * - Normalizes line endings+ *+ * @param markdown - The markdown string to sanitize+ * @returns The sanitized markdown string, or null if invalid/binary content detected+ */+export function sanitizeMarkdown(markdown: string | null | undefined): string | null {+ if (!markdown || typeof markdown !== "string") {+ return null;+ }++ // Trim whitespace+ let sanitized = markdown.trim();++ // Return null if empty after trimming+ if (!sanitized) {+ return null;+ }++ // Check for binary content - markdown should be valid UTF-8 text+ // Reject if there are null bytes (indicates binary content)+ if (sanitized.includes("\0")) {+ return null;+ }++ // Check for suspicious patterns that could cause DoS or rendering issues+ // Truncate extremely long lines (potential DoS) instead of rejecting+ const lines = sanitized.split("\n");+ const maxLineLength = 1000;+ for (let i = 0; i < lines.length; i++) {+ if (lines[i].length > maxLineLength) {+ lines[i] = lines[i].substring(0, maxLineLength);+ }+ }+ sanitized = lines.join("\n");++ // Normalize line endings+ sanitized = sanitized.replace(/\r\n/g, "\n").replace(/\r/g, "\n");++ return sanitized;+}+diff --git a/apps/web/lib/zod/schemas/partners.ts b/apps/web/lib/zod/schemas/partners.tsindex 6ec68e1fc2..23f9c72428 100644--- a/apps/web/lib/zod/schemas/partners.ts+++ b/apps/web/lib/zod/schemas/partners.ts@@ -662,7 +662,7 @@ export const invitePartnerSchema = z.object({groupId: z.string().nullish().default(null),emailSubject: z.string().optional(),emailTitle: z.string().optional(),- emailBody: z.string().optional(),+ emailBody: z.string().max(3000).optional(),});export const approvePartnerSchema = z.object({diff --git a/packages/email/package.json b/packages/email/package.jsonindex 35dbcaf2ce..cac3c42ec3 100644--- a/packages/email/package.json+++ b/packages/email/package.json@@ -14,6 +14,7 @@"nodemailer": "^6.9.3","react-email": "^2.1.6","react-markdown": "^9.0.3",+ "rehype-sanitize": "^6.0.0","remark-gfm": "^4.0.0","resend": "^6.1.2"},diff --git a/packages/email/src/templates/program-invite.tsx b/packages/email/src/templates/program-invite.tsxindex be33b17de8..60f6c8e34a 100644--- a/packages/email/src/templates/program-invite.tsx+++ b/packages/email/src/templates/program-invite.tsx@@ -15,6 +15,7 @@ import {Text,} from "@react-email/components";import ReactMarkdown, { type Components } from "react-markdown";+import rehypeSanitize from "rehype-sanitize";import remarkGfm from "remark-gfm";import { Footer } from "../components/footer";@@ -156,6 +157,7 @@ export default function ProgramInvite({{body ? (<ReactMarkdownremarkPlugins={[remarkGfm]}+ rehypePlugins={[rehypeSanitize]}components={markdownComponents}>{body}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex b78a83d642..664c123787 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -535,6 +535,9 @@ importers:react-markdown:specifier: ^9.0.3version: 9.0.3(@types/[email protected])([email protected])+ rehype-sanitize:+ specifier: ^6.0.0+ version: 6.0.0remark-gfm:specifier: ^4.0.0version: 4.0.0@@ -8805,6 +8808,9 @@ packages:resolution: {integrity: sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==}engines: {node: '>= 0.4'}+ resolution: {integrity: sha512-3yTWghByc50aGS7JlGhk61SPenfE/p1oaFeNwkOOyrscaOkMGrcW9+Cy/QAIOBpZxP1yqDIzFMR0+Np0i0+usg==}+resolution: {integrity: sha512-H/y0+IWPdsLLS738P8tDnrQ8Z+dj12zQQ6WC11TIM21C8WFVoIxcqWXf2H3hiTVZjF1AWqoimGwrTWecWrnmRQ==}@@ -11607,6 +11613,9 @@ packages:resolution: {integrity: sha512-+crtS5QjFRqFCoQmvGduwYWEBng99ZvmFvF+cUJkGYF1L1BfU8C6Zp9T7f5vPAwyLkUExpvK+ANVZmGU49qi4Q==}engines: {node: '>=12'}+ resolution: {integrity: sha512-CsnhKNsyI8Tub6L4sm5ZFsme4puGfc6pYylvXo1AeqaGbjOYyzNv3qZPwvs0oMJ39eryyeOdmxwUIo94IpEhqg==}+resolution: {integrity: sha512-U92vJgBPkbw4Zfu/IiW2oTZLSL3Zpv+uI7My2eq8JxKgqraFdU8YUGicEJCEgSbeaG+QDFqIcwwfMTOEelPxuA==}@@ -23586,6 +23595,12 @@ snapshots:dependencies:function-bind: 1.1.2+ dependencies:+ '@types/hast': 3.0.4+ '@ungap/structured-clone': 1.2.0+ unist-util-position: 5.0.0+dependencies:'@types/estree': 1.0.5@@ -27147,6 +27162,11 @@ snapshots:dependencies:rc: 1.2.8+ dependencies:+ '@types/hast': 3.0.4+ hast-util-sanitize: 5.0.2+dependencies:'@types/mdast': 4.0.4