The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
GDGT-2400 Custom viz plugin can overflow its own container (#77695)
Kamil Mielnik83215dcacd74
diff --git a/e2e/test/scenarios/visualizations-charts/custom-viz.cy.spec.ts b/e2e/test/scenarios/visualizations-charts/custom-viz.cy.spec.tsindex e61bbf5512e..90d448a34b5 100644--- a/e2e/test/scenarios/visualizations-charts/custom-viz.cy.spec.ts+++ b/e2e/test/scenarios/visualizations-charts/custom-viz.cy.spec.ts@@ -2442,6 +2442,92 @@ describe("sandbox", () => {.should("not.have.attr", "data-pwned-by-plugin");});+ it("confines custom viz and custom viz setting widget to its container (GDGT-2400)", () => {+ const timeout = 100;+ const payload = `+ setInterval(function() {+ var containers = document.querySelectorAll('[data-plugin-sandbox]');+ for (var i = 0; i < containers.length; i++) {+ containers[i].setAttribute('style',+ 'position:fixed;inset:0;z-index:99999;background:red;');+ }+ }, ${timeout});+ `;++ cy.intercept("GET", "/api/ee/custom-viz-plugin/*/bundle*", (req) => {+ req.continue((res) => {+ res.body = `${payload}\n${String(res.body)};\n`;+ res.send();+ });+ }).as("injectedBundle");++ H.addCustomVizPlugin(H.CUSTOM_VIZ_FIXTURE_TGZ_4_SECURITY_COMPONENT);+ H.createQuestion(+ {+ name: "Custom Viz Setting Widget Confinement Test",+ query: {+ "source-table": SAMPLE_DB_TABLES.STATIC_ORDERS_ID,+ aggregation: [["count"]],+ },+ display: "table",+ },+ { wrapId: true, idAlias: "componentCardId" },+ );++ H.visitQuestion("@componentCardId");++ H.openVizTypeSidebar();+ cy.findByTestId("custom-viz-plugins-toggle").click();+ cy.findByTestId(+ `${H.CUSTOM_VIZ_IDENTIFIER_4_SECURITY_COMPONENT}-button`,+ ).click();+ H.openVizTypeSidebar();+ cy.wait("@injectedBundle");++ H.openVizSettingsSidebar();++ const viewportWidth = Cypress.config("viewportWidth");+ const viewportHeight = Cypress.config("viewportHeight");++ H.vizSettingsSidebar()+ .find("[data-plugin-sandbox]")+ .should(($element) => {+ expect($element, "attack applied position:fixed").to.have.css(+ "position",+ "fixed",+ );++ const rect = $element[0].getBoundingClientRect();+ const viewportWidth = Cypress.config("viewportWidth");++ expect(+ rect.top,+ "setting widget top offset from viewport",+ ).to.be.greaterThan(0);+ expect(rect.width, "setting widget width vs viewport").to.be.lessThan(+ viewportWidth,+ );+ expect(rect.height, "setting widget height vs viewport").to.be.eq(0);+ });++ H.queryVisualizationRoot()+ .find("[data-plugin-sandbox]")+ .should(($element) => {+ const rect = $element[0].getBoundingClientRect();++ expect(+ rect.top,+ "container top offset from viewport",+ ).to.be.greaterThan(0);+ expect(rect.width, "container width vs viewport").to.be.lessThan(+ viewportWidth,+ );+ expect(rect.height, "container height vs viewport").to.be.lessThan(+ viewportHeight,+ );+ });+ });+it("MutationObserver on out-of-scope nodes observes a decoy and never fires for host mutations", () => {const payload = `var seenMutations = 0;diff --git a/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/src/index-widget-security.tsx b/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/src/index-widget-security.tsxindex c73eb46798f..f5b1d6fe4ef 100644--- a/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/src/index-widget-security.tsx+++ b/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/src/index-widget-security.tsx@@ -33,7 +33,7 @@ const createVisualization: CreateCustomVisualization<Settings> = ({settings: {threshold: defineSetting({id: "threshold",- title: "Threspachold",+ title: "Threshold",widget: "number",getDefault() {return 0;diff --git a/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/src/index.tsx b/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/src/index.tsxindex a9354455c71..248c2cbd9fb 100644--- a/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/src/index.tsx+++ b/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/src/index.tsx@@ -44,7 +44,7 @@ const createVisualization: CreateCustomVisualization<Settings> = ({settings: {threshold: defineSetting({id: "threshold",- title: "Threspachold",+ title: "Threshold",widget: "number",getDefault() {return 0;diff --git a/enterprise/frontend/src/metabase-enterprise/custom_viz/components/CustomVizSettingWidget.tsx b/enterprise/frontend/src/metabase-enterprise/custom_viz/components/CustomVizSettingWidget.tsxindex 30d80c54dcd..20b4a609b13 100644--- a/enterprise/frontend/src/metabase-enterprise/custom_viz/components/CustomVizSettingWidget.tsx+++ b/enterprise/frontend/src/metabase-enterprise/custom_viz/components/CustomVizSettingWidget.tsx@@ -3,6 +3,8 @@ import type { WidgetMount } from "custom-viz";import { usePluginMount } from "../use-plugin-mount";import { getWidgetMountPluginId } from "../widget-mount";+import { SandboxedPluginContainer } from "./SandboxedPluginContainer";+type Props = {mount: WidgetMount<Record<string, unknown>>;widgetProps: Record<string, unknown>;@@ -16,10 +18,6 @@ export function CustomVizSettingWidget({ mount, widgetProps }: Props) {const containerRef = usePluginMount(mount, widgetProps);return (- <div- ref={containerRef}- data-plugin-sandbox={pluginId}- style={{ width: "100%" }}- />+ <SandboxedPluginContainer containerRef={containerRef} pluginId={pluginId} />);}diff --git a/enterprise/frontend/src/metabase-enterprise/custom_viz/components/CustomVizSettingWidget.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/custom_viz/components/CustomVizSettingWidget.unit.spec.tsxindex 514975bbd6a..eaccad003da 100644--- a/enterprise/frontend/src/metabase-enterprise/custom_viz/components/CustomVizSettingWidget.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/custom_viz/components/CustomVizSettingWidget.unit.spec.tsx@@ -1,8 +1,9 @@-import { render, screen, waitFor } from "@testing-library/react";import type { WidgetMount } from "custom-viz";import type { ComponentType } from "react";import { createRoot } from "react-dom/client";+import { render, screen, waitFor } from "__support__/ui";+import { wrapPluginWidget } from "../widget-mount";import { CustomVizSettingWidget } from "./CustomVizSettingWidget";@@ -125,4 +126,16 @@ describe("CustomVizSettingWidget", () => {expect(getWidgetContainer()).toHaveAttribute("data-plugin-sandbox", "11");});});++ it("mounts the plugin inside a host-controlled wrapper the plugin cannot tag", async () => {+ const { getWidgetContainer } = setup({ pluginId: 11 });++ await waitFor(() => {+ expect(getWidgetContainer()).toHaveAttribute("data-plugin-sandbox", "11");+ });++ const wrapper = getWidgetContainer()?.parentElement;+ expect(wrapper).not.toBeNull();+ expect(wrapper).not.toHaveAttribute("data-plugin-sandbox");+ });});diff --git a/enterprise/frontend/src/metabase-enterprise/custom_viz/components/SandboxedPluginContainer.module.css b/enterprise/frontend/src/metabase-enterprise/custom_viz/components/SandboxedPluginContainer.module.cssnew file mode 100644index 00000000000..b64b96b0ec2--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/custom_viz/components/SandboxedPluginContainer.module.css@@ -0,0 +1,3 @@+.container {+ contain: layout paint; /* Security boundary */+}diff --git a/enterprise/frontend/src/metabase-enterprise/custom_viz/components/SandboxedPluginContainer.tsx b/enterprise/frontend/src/metabase-enterprise/custom_viz/components/SandboxedPluginContainer.tsxnew file mode 100644index 00000000000..af8b0e166fd--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/custom_viz/components/SandboxedPluginContainer.tsx@@ -0,0 +1,29 @@+import type { Ref } from "react";++import { Box } from "metabase/ui";+import type { CustomVizPluginId } from "metabase-types/api";++import S from "./SandboxedPluginContainer.module.css";++type Props = {+ containerRef: Ref<HTMLDivElement>;+ fill?: boolean;+ pluginId: CustomVizPluginId | undefined;+};++export function SandboxedPluginContainer({+ containerRef,+ fill = false,+ pluginId,+}: Props) {+ return (+ <Box className={S.container} h={fill ? "100%" : undefined} w="100%">+ <Box+ data-plugin-sandbox={pluginId}+ h={fill ? "100%" : undefined}+ ref={containerRef}+ w="100%"+ />+ </Box>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/custom_viz/custom-viz-plugins.tsx b/enterprise/frontend/src/metabase-enterprise/custom_viz/custom-viz-plugins.tsxindex edb8d1cc64e..5b0b794d2f4 100644--- a/enterprise/frontend/src/metabase-enterprise/custom_viz/custom-viz-plugins.tsx+++ b/enterprise/frontend/src/metabase-enterprise/custom_viz/custom-viz-plugins.tsx@@ -36,6 +36,7 @@ import type {import { isObject } from "metabase-types/guards";import { isCustomVizDisplay } from "metabase-types/guards/visualization";+import { SandboxedPluginContainer } from "./components/SandboxedPluginContainer";import { applyDefaultVisualizationProps } from "./custom-viz-common";import { ensureVizApi } from "./custom-viz-globals";import type { SandboxMode } from "./sandbox";@@ -536,10 +537,10 @@ function createCustomVizWrapper();return (- <div- ref={containerRef}- data-plugin-sandbox={pluginId}- style={{ width: "100%", height: "100%" }}+ <SandboxedPluginContainer+ containerRef={containerRef}+ fill+ pluginId={pluginId}/>);};