Skip to content

Beautiful Open-Source Form Builder

Refactor frame-ancestors logic in embeddable.js for improved security

JhumanJf2bfabd48f8e
diff --git a/client/server/plugins/embeddable.js b/client/server/plugins/embeddable.js
index 22b262521..cf2ec68aa 100644
--- a/client/server/plugins/embeddable.js
+++ b/client/server/plugins/embeddable.js
@@ -13,24 +13,33 @@ export default defineNitroPlugin((nitroApp) => {
// Normalize allowed domains to hostnames (supports plain host or full URL)
const normalizedAllowed = allowedDomains.map((d) => (getDomain(d) || '').toLowerCase()).filter(Boolean)
- // Build frame-ancestors for non-form routes: only allowed domains + localhost variants
- const ancestors = ["'self'",
- 'http://localhost', 'https://localhost',
- 'http://127.0.0.1', 'https://127.0.0.1',
- 'http://0.0.0.0', 'https://0.0.0.0',
- 'http://[::1]', 'https://[::1]'
- ]
- normalizedAllowed.forEach((host) => {
- ancestors.push(`https://${host}`)
- ancestors.push(`http://${host}`)
- })
-
// Remove legacy header to control framing via CSP
delete response.headers["X-Frame-Options"]
delete response.headers["x-frame-options"]
if (routePath && !routePath.startsWith("/forms/")) {
- // Restrict embedding to explicit allowlist + localhost variants
+ // Build frame-ancestors for non-form routes: localhost variants + matching allowlisted domain (if Referer present)
+ const ancestors = ["'self'",
+ 'http://localhost:*', 'https://localhost:*',
+ 'http://127.0.0.1:*', 'https://127.0.0.1:*',
+ 'http://0.0.0.0:*', 'https://0.0.0.0:*',
+ 'http://[::1]:*', 'https://[::1]:*'
+ ]
+
+ const referer = event.node?.req?.headers?.referer || ''
+ const refererHost = referer ? (getDomain(referer) || '').toLowerCase() : ''
+
+ function hostMatchesAllowed(host, allowedHost) {
+ if (!host || !allowedHost) return false
+ return host === allowedHost || host.endsWith(`.${allowedHost}`)
+ }
+
+ const matched = normalizedAllowed.find((allowedHost) => hostMatchesAllowed(refererHost, allowedHost))
+ if (matched) {
+ ancestors.push(`https://${matched}`)
+ }
+
+ // Restrict embedding to localhost + (optionally) matched allowlisted domain
response.headers["Content-Security-Policy"] = `frame-ancestors ${ancestors.join(' ')};`
} else {
// Forms: embeddable anywhere, omit CSP directive