Skip to content

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

add `customVisualizationsSandboxUrl` to the react sdk (#84366)

Nicolò Pretto26712fb0132f
diff --git a/enterprise/frontend/src/embedding-sdk-ee/custom-viz/initialize.ts b/enterprise/frontend/src/embedding-sdk-ee/custom-viz/initialize.ts
index d6687a97667..2c385c11c8f 100644
--- a/enterprise/frontend/src/embedding-sdk-ee/custom-viz/initialize.ts
+++ b/enterprise/frontend/src/embedding-sdk-ee/custom-viz/initialize.ts
@@ -41,11 +41,27 @@ import { isWidgetMount } from "../../metabase-enterprise/custom_viz/widget-mount
/**
* EAJS has no eval-permissive page CSP in production, so its plugin sandbox
* uses the hosted donor document (served with a per-document eval CSP). The
- * react-sdk npm package runs on the customer's own page and inherits its
- * CSP, so "blank" (about:blank) works there.
+ * react-sdk npm package runs on the customer's own page: it uses the donor
+ * page the host app passes as `customVisualizationsSandboxUrl`, or
+ * else "blank" (about:blank), which inherits the page CSP.
*/
-export function getSdkSandboxMode(): SandboxMode {
- return isEmbeddingEajs() ? "hosted" : "blank";
+export function getSdkSandboxMode(sandboxUrl?: string): SandboxMode {
+ if (isEmbeddingEajs()) {
+ return "hosted";
+ }
+ return sandboxUrl ? { iframeSrc: sandboxUrl } : "blank";
+}
+
+function getSandboxUrl(): string | undefined {
+ return ensureMetabaseProviderPropsStore().getState().props
+ ?.customVisualizationsSandboxUrl;
+}
+
+function useSdkSandboxMode(): SandboxMode {
+ const { state } = useMetabaseProviderPropsStore();
+ const sandboxUrl = state?.props?.customVisualizationsSandboxUrl;
+ // `sandboxMode` is an effect dependency downstream, so keep it stable
+ return useMemo(() => getSdkSandboxMode(sandboxUrl), [sandboxUrl]);
}
/**
@@ -155,7 +171,7 @@ export function initializeSdkCustomVizPlugin() {
}
return eeLoadCustomVizPlugin(plugin, {
...options,
- sandboxMode: getSdkSandboxMode(),
+ sandboxMode: getSdkSandboxMode(getSandboxUrl()),
});
},
@@ -167,13 +183,14 @@ export function initializeSdkCustomVizPlugin() {
return { status: "unavailable" };
}
return eeLoadCustomVizPluginForDisplay(dispatch, display, {
- sandboxMode: getSdkSandboxMode(),
+ sandboxMode: getSdkSandboxMode(getSandboxUrl()),
onMessage: logUnavailableCustomVizMessage,
});
},
useAutoLoadCustomVizPlugin: (display: string | undefined) => {
const allowlist = useAllowlist();
+ const sandboxMode = useSdkSandboxMode();
const allowed =
// Regular (non-custom) displays are always allowed.
!isCustomVizDisplay(display) || isCustomVizAllowed(display, allowlist);
@@ -185,7 +202,7 @@ export function initializeSdkCustomVizPlugin() {
}, [display, allowed]);
return eeUseAutoLoadCustomVizPlugin(allowed ? display : undefined, {
- sandboxMode: getSdkSandboxMode(),
+ sandboxMode,
onMessage: logUnavailableCustomVizMessage,
});
},
diff --git a/enterprise/frontend/src/embedding-sdk-ee/custom-viz/initialize.unit.spec.ts b/enterprise/frontend/src/embedding-sdk-ee/custom-viz/initialize.unit.spec.ts
index 2c4d4e049b9..f704f1bb710 100644
--- a/enterprise/frontend/src/embedding-sdk-ee/custom-viz/initialize.unit.spec.ts
+++ b/enterprise/frontend/src/embedding-sdk-ee/custom-viz/initialize.unit.spec.ts
@@ -14,10 +14,22 @@ describe("getSdkSandboxMode", () => {
expect(getSdkSandboxMode()).toBe("hosted");
});
- it('returns "blank" for the react-sdk npm package (not EAJS)', () => {
+ it('returns "blank" for the react-sdk npm package without a sandbox url', () => {
EMBEDDING_SDK_IFRAME_EMBEDDING_CONFIG.isSimpleEmbedding = false;
expect(getSdkSandboxMode()).toBe("blank");
});
+
+ it("returns the sandbox url for the react-sdk npm package when it is passed", () => {
+ EMBEDDING_SDK_IFRAME_EMBEDDING_CONFIG.isSimpleEmbedding = false;
+ expect(getSdkSandboxMode("/metabase-sandbox.html")).toEqual({
+ iframeSrc: "/metabase-sandbox.html",
+ });
+ });
+
+ it('ignores the sandbox url and returns "hosted" when running as EAJS', () => {
+ EMBEDDING_SDK_IFRAME_EMBEDDING_CONFIG.isSimpleEmbedding = true;
+ expect(getSdkSandboxMode("/metabase-sandbox.html")).toBe("hosted");
+ });
});
const okResponse = () =>
diff --git a/enterprise/frontend/src/metabase-enterprise/custom_viz/sandbox/index.ts b/enterprise/frontend/src/metabase-enterprise/custom_viz/sandbox/index.ts
index 6f483dd60bf..1608d829703 100644
--- a/enterprise/frontend/src/metabase-enterprise/custom_viz/sandbox/index.ts
+++ b/enterprise/frontend/src/metabase-enterprise/custom_viz/sandbox/index.ts
@@ -5,7 +5,7 @@ import type { CustomVizPluginId } from "metabase-types/api";
import { makeDistortionCallback } from "./distortions";
-export type SandboxMode = "hosted" | "blank";
+export type SandboxMode = "hosted" | "blank" | { iframeSrc: string };
// Needed for React style declarations to be applied correctly.
function isLiveTarget(target: object): boolean {
@@ -16,16 +16,25 @@ function isLiveTarget(target: object): boolean {
// per-document CSP (allowing `'unsafe-eval'`).
const SANDBOX_HOST_URL = "/api/ee/custom-viz-plugin/sandbox-host";
+function getSandboxIframeSrc(mode: SandboxMode): string | undefined {
+ if (mode === "hosted") {
+ return getSubpathSafeUrl(SANDBOX_HOST_URL);
+ }
+ // "blank" skips the sandbox-host endpoint and lets the library fall back to
+ // about:blank. Used in Storybook where no backend serves the endpoint.
+ if (mode === "blank") {
+ return undefined;
+ }
+ return mode.iframeSrc;
+}
+
export async function createPluginSandbox(
pluginId: CustomVizPluginId,
mode: SandboxMode = "hosted",
) {
let capturedFactory: unknown;
- // "blank" skips the sandbox-host endpoint and lets the library fall back to
- // about:blank. Used in Storybook where no backend serves the endpoint.
- const iframeSrc =
- mode === "hosted" ? getSubpathSafeUrl(SANDBOX_HOST_URL) : undefined;
+ const iframeSrc = getSandboxIframeSrc(mode);
const env = await createVirtualEnvironment(window, {
...(iframeSrc ? { iframeSrc } : {}),
diff --git a/frontend/src/embedding-sdk-bundle/types/metabase-provider.ts b/frontend/src/embedding-sdk-bundle/types/metabase-provider.ts
index 43b4d75cf2e..5abe56b09f8 100644
--- a/frontend/src/embedding-sdk-bundle/types/metabase-provider.ts
+++ b/frontend/src/embedding-sdk-bundle/types/metabase-provider.ts
@@ -86,4 +86,13 @@ export interface MetabaseProviderProps {
* Requires the `custom-viz` premium feature.
**/
allowedCustomVisualizations?: `custom:${string}`[];
+
+ /**
+ * URL of an empty HTML page that custom visualizations run in. Use it when
+ * your page's Content Security Policy does not allow `'unsafe-eval'`.
+ *
+ * The page must be served from the same origin as your app, with its own
+ * `Content-Security-Policy: default-src 'none'; script-src 'unsafe-eval'`.
+ **/
+ customVisualizationsSandboxUrl?: string;
}