Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

[EMB-2532] docs for `customVisualizationsSandboxUrl` (#84888)

Nicolò Pretto4b2f959df1dd
diff --git a/docs/embedding/custom-visualizations.md b/docs/embedding/custom-visualizations.md
index 451d7a01427..8251d5ecca2 100644
--- a/docs/embedding/custom-visualizations.md
+++ b/docs/embedding/custom-visualizations.md
@@ -12,6 +12,7 @@ Authenticated [modular embeds](./modular-embedding.md) can render [custom visual
- [An admin has to turn on custom visualizations first](#an-admin-has-to-turn-on-custom-visualizations-first)
- [Add custom visualizations to your allowlist](#add-custom-visualizations-to-your-allowlist)
- [Set a Content Security Policy in your app](#set-a-content-security-policy-in-your-app)
+- [Use a sandbox page if your app's CSP blocks eval (React SDK only)](#use-a-sandbox-page-if-your-apps-csp-blocks-eval-react-sdk-only)
- [Custom visualizations don't work in guest embeds](#custom-visualizations-dont-work-in-guest-embeds)
## An admin has to turn on custom visualizations first
@@ -79,6 +80,40 @@ A custom visualization runs third-party JavaScript in your app. Metabase runs th
To limit where custom visualizations can load images from, set a [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CSP) with an `img-src` allowlist in your app. The core Metabase app sets this CSP with [Restrict image domains](../configuring-metabase/domains.md#restrict-where-images-can-load-from), but you should also set a CSP in your app.
+## Use a sandbox page if your app's CSP blocks eval (React SDK only)
+
+With the [React SDK](./sdk/introduction.md), the sandbox needs `eval` to run a custom visualization's code. By default, the SDK creates the sandbox in an `about:blank` iframe, which inherits your app's CSP. If that CSP blocks `eval`, every custom visualization falls back to the default visualization, and the browser console shows an `EvalError`.
+
+If you don't want to change the CSP for the whole app, you can serve an empty page that allows `eval` and point the `customVisualizationsSandboxUrl` prop on `MetabaseProvider` to it.
+
+The sandbox page must be on the same origin as your app.
+
+To set up the sandbox page:
+
+1. Serve an empty HTML page from your app, for example at `/metabase-sandbox.html`:
+
+ ```html
+ <!doctype html>
+ <html>
+ <head>
+ <meta charset="utf-8" />
+ </head>
+ <body></body>
+ </html>
+ ```
+
+2. Serve that page with its own CSP:
+
+ ```
+ Content-Security-Policy: default-src 'none'; script-src 'unsafe-eval'
+ ```
+
+Then pass the page's URL to `customVisualizationsSandboxUrl` on `MetabaseProvider`:
+
+```typescript
+{% include_file "{{ dirname }}/sdk/snippets/config/config-with-custom-visualizations-sandbox-url.tsx" snippet="example" %}
+```
+
## Custom visualizations don't work in guest embeds
Custom visualizations only work when Metabase knows who's viewing the embed. [Guest embeds](./guest-embedding.md) ignore `allowedCustomVisualizations` (and log a warning to the console), and fall back to the default visualization.
diff --git a/docs/embedding/sdk/snippets/config/config-with-custom-visualizations-sandbox-url.tsx b/docs/embedding/sdk/snippets/config/config-with-custom-visualizations-sandbox-url.tsx
new file mode 100644
index 00000000000..5c0b4350fd6
--- /dev/null
+++ b/docs/embedding/sdk/snippets/config/config-with-custom-visualizations-sandbox-url.tsx
@@ -0,0 +1,21 @@
+import {
+ type MetabaseAuthConfig,
+ MetabaseProvider,
+} from "@metabase/embedding-sdk-react";
+import type { PropsWithChildren } from "react";
+
+const authConfig = {} as MetabaseAuthConfig;
+
+const Example = ({ children }: PropsWithChildren) => (
+ // [<snippet example>]
+ <MetabaseProvider
+ authConfig={authConfig}
+ allowedCustomVisualizations={["custom:Calendar Heatmap", "custom:Thumbs"]}
+ // An empty page on your app's origin, served with its own CSP:
+ // `default-src 'none'; script-src 'unsafe-eval'`
+ customVisualizationsSandboxUrl="/metabase-sandbox.html"
+ >
+ {children}
+ </MetabaseProvider>
+ // [<endsnippet example>]
+);