From 0282f6f056f03e7c2e4f526131ee1843e71336d5 Mon Sep 17 00:00:00 2001 From: kelvin <43873157+kelvinkipruto@users.noreply.github.com> Date: Thu, 16 Jul 2026 14:25:06 +0300 Subject: [PATCH 1/2] feat(embeds,security): add secure embed handling, sanitization, and CSP Add sanitize-html and its TypeScript types for server-side HTML sanitization. Create a shared embed allowlist policy that syncs Content-Security-Policy headers with runtime validation to ensure consistency across the app. Update CMS fields to prefer structured URL inputs over raw embed code, with validation for approved third-party providers. Add validation for legacy embed snippets to block unsafe content like scripts, inline event handlers, and iframes. Configure global security headers including strict CSP rules for the Next.js application. --- src/lib/embeds/sanitize.ts | 93 ++++++++++++++ src/lib/embeds/validation.ts | 208 +++++++++++++++++++++++++++++++ src/lib/security/embedPolicy.mjs | 87 +++++++++++++ src/lib/security/headers.mjs | 98 +++++++++++++++ 4 files changed, 486 insertions(+) create mode 100644 src/lib/embeds/sanitize.ts create mode 100644 src/lib/embeds/validation.ts create mode 100644 src/lib/security/embedPolicy.mjs create mode 100644 src/lib/security/headers.mjs diff --git a/src/lib/embeds/sanitize.ts b/src/lib/embeds/sanitize.ts new file mode 100644 index 00000000..243d3f98 --- /dev/null +++ b/src/lib/embeds/sanitize.ts @@ -0,0 +1,93 @@ +/** + * Server-side sanitization for legacy CMS-authored newsletter embed + * HTML (Mailchimp signup snippets). New configurations should use the + * structured `signupUrl` field instead; this is the safety net for + * snippets that predate it. + * + * Policy: static markup and signup-form elements only. Scripts, + * styles, iframes, inline event handlers, and non-https URLs are + * stripped, and any