Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(csp): allow the email templater to load the unlayer editor (#101291)

Tom Piccirelloea135f29d165
diff --git a/posthog/middleware.py b/posthog/middleware.py
index ae9b6e906930..10b459276db0 100644
--- a/posthog/middleware.py
+++ b/posthog/middleware.py
@@ -1330,14 +1330,23 @@ class CSPMiddleware:
# can. Session replay decompresses snapshots with snappy-wasm and the HogQL editor
# parses with a WebAssembly build, so both break without it.
#
- # Stripe and Turnstile are the two scripts we cannot serve ourselves: both vendors
- # require the file to load from their own origin, so the flag-font trick of shipping
+ # Stripe, Turnstile and Unlayer are the scripts we cannot serve ourselves: each vendor
+ # requires the file to load from their own origin, so the flag-font trick of shipping
# a copy does not apply. `loadStripe` injects js.stripe.com for the payment entry
- # modal, and the signup captcha loads the Turnstile API. `frame-src 'self' https:`
- # already admits the iframes each one opens, and neither produced a connect-src
+ # modal, the signup captcha loads the Turnstile API, and `react-email-editor` injects
+ # editor.unlayer.com/embed.js for the email templater. `frame-src 'self' https:`
+ # already admits the iframes each one opens, and none produced a connect-src
# violation while this policy was report-only, so their API calls run inside those
- # frames rather than from our page.
- f"script-src 'self' 'nonce-{nonce}' 'wasm-unsafe-eval' {resource_url} https://*.i.posthog.com https://js.stripe.com https://challenges.cloudflare.com",
+ # frames rather than from our page. Unlayer bears that out: embed.js is the only
+ # unlayer URL this policy has ever reported, because the editor itself runs in a
+ # frame that carries its own policy rather than ours.
+ #
+ # Unlayer is pinned to a path rather than the host, because react-email-editor
+ # hardcodes that one URL and we do not pass its `scriptUrl` prop. A source path is
+ # matched against the URL path alone, so the `?2` the library appends does not
+ # defeat it. The cost is that a version bump which moves the file needs this line
+ # updated, or the editor stops loading.
+ f"script-src 'self' 'nonce-{nonce}' 'wasm-unsafe-eval' {resource_url} https://*.i.posthog.com https://js.stripe.com https://challenges.cloudflare.com https://editor.unlayer.com/embed.js",
# A data: font cannot execute script, and this directive governs font loading only,
# so the token widens nothing else. It also carries nothing out: a data: URL makes
# no request, which is what the CSS-injection attacks on this directive need. The