Skip to content

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

Gdgt 2390 add sources for the tgz test plugins (#73819)

Jakub Chodorowiczfeaaafb8e87a
diff --git a/.gitignore b/.gitignore
index b2490a9534a..2451dc1009e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -56,6 +56,7 @@ cypress.env.json
!/e2e/tmp/db_dumps
/e2e/tmp/db_dumps/*
!/e2e/tmp/db_dumps/.gitkeep
+/e2e/support/assets/example_custom_viz_plugin*.tgz
/lein-plugins/*/target
/local
/locales/metabase-*.pot
diff --git a/e2e/support/assets/example_custom_viz_plugin.tgz b/e2e/support/assets/example_custom_viz_plugin.tgz
deleted file mode 100644
index d5325551cb6..00000000000
Binary files a/e2e/support/assets/example_custom_viz_plugin.tgz and /dev/null differ
diff --git a/e2e/support/assets/example_custom_viz_plugin_2.tgz b/e2e/support/assets/example_custom_viz_plugin_2.tgz
deleted file mode 100644
index 998a3bcb92d..00000000000
Binary files a/e2e/support/assets/example_custom_viz_plugin_2.tgz and /dev/null differ
diff --git a/e2e/support/config.js b/e2e/support/config.js
index b41f20008cd..3324c95d03f 100644
--- a/e2e/support/config.js
+++ b/e2e/support/config.js
@@ -1,3 +1,4 @@
+import { execFileSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
@@ -76,6 +77,18 @@ const defaultConfig = {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
+ // Build custom-viz .tgz fixtures from sources
+ execFileSync(
+ "node",
+ [
+ path.resolve(
+ __dirname,
+ "../../enterprise/frontend/src/custom-viz/fixtures/build-example-custom-viz.mjs",
+ ),
+ ],
+ { stdio: "inherit" },
+ );
+
// Use cypress-on-fix to enable multiple handlers
const on = cypressOnFix(cypressOn);
diff --git a/e2e/support/helpers/e2e-custom-viz-helpers.ts b/e2e/support/helpers/e2e-custom-viz-helpers.ts
index c088f6b8c33..cbbe8aca8a6 100644
--- a/e2e/support/helpers/e2e-custom-viz-helpers.ts
+++ b/e2e/support/helpers/e2e-custom-viz-helpers.ts
@@ -6,11 +6,17 @@ export const CUSTOM_VIZ_FIXTURE_TGZ_2 =
Cypress.config("projectRoot") +
"/e2e/support/assets/example_custom_viz_plugin_2.tgz";
+export const CUSTOM_VIZ_FIXTURE_TGZ_3_SECURITY =
+ Cypress.config("projectRoot") +
+ "/e2e/support/assets/example_custom_viz_plugin_3_security.tgz";
+
// Identifier comes from the manifest's `name` field in the packaged bundle.
export const CUSTOM_VIZ_IDENTIFIER = "demo-viz";
export const CUSTOM_VIZ_IDENTIFIER_2 = "demo-viz-2";
+export const CUSTOM_VIZ_IDENTIFIER_3_SECURITY = "demo-viz-security";
+
// Frontend display type: "custom:{identifier}"
export const CUSTOM_VIZ_DISPLAY = `custom:${CUSTOM_VIZ_IDENTIFIER}` as const;
diff --git a/e2e/test/scenarios/visualizations-charts/custom-viz.cy.spec.ts b/e2e/test/scenarios/visualizations-charts/custom-viz.cy.spec.ts
index 1dcd1572d9f..4dcbd77efd8 100644
--- a/e2e/test/scenarios/visualizations-charts/custom-viz.cy.spec.ts
+++ b/e2e/test/scenarios/visualizations-charts/custom-viz.cy.spec.ts
@@ -2503,4 +2503,46 @@ describe("sandbox", () => {
/\[plugin \d+\] swapped out-of-scope <body> with decoy/,
);
});
+
+ it("blocks forbidden apis in widget settings", () => {
+ H.restore("postgres-writable");
+ H.activateToken("bleeding-edge");
+ cy.signInAsAdmin();
+ H.updateSetting("custom-viz-enabled", true);
+ H.addCustomVizPlugin(H.CUSTOM_VIZ_FIXTURE_TGZ_3_SECURITY);
+
+ H.createQuestion(
+ {
+ name: "Custom Viz Question Test",
+ query: {
+ "source-table": SAMPLE_DB_TABLES.STATIC_ORDERS_ID,
+ aggregation: [["count"]],
+ },
+ display: "table",
+ },
+ { wrapId: true, idAlias: "questionId" },
+ );
+
+ H.visitQuestion("@questionId", {
+ onBeforeLoad(win) {
+ cy.spy(win.console, "error").as("consoleError");
+ },
+ });
+
+ cy.findByTestId("viz-type-button").click();
+ cy.findByTestId("custom-viz-plugins-toggle").click();
+ cy.findByTestId(`${H.CUSTOM_VIZ_IDENTIFIER_3_SECURITY}-button`).click();
+ cy.findByTestId("viz-type-button").click();
+
+ cy.log("open viz settings");
+ cy.findByTestId("viz-settings-button").click();
+
+ cy.get("@consoleError").should(
+ "have.been.calledWithMatch",
+ Cypress.sinon.match.has(
+ "message",
+ Cypress.sinon.match(/blocked API call: window\.fetch/),
+ ),
+ );
+ });
});
diff --git a/enterprise/frontend/src/custom-viz/fixtures/build-example-custom-viz.mjs b/enterprise/frontend/src/custom-viz/fixtures/build-example-custom-viz.mjs
new file mode 100644
index 00000000000..4ef34ac4ccd
--- /dev/null
+++ b/enterprise/frontend/src/custom-viz/fixtures/build-example-custom-viz.mjs
@@ -0,0 +1,85 @@
+/**
+ * Builds the e2e custom-viz fixtures from source.
+ *
+ * Invoked automatically by Cypress `setupNodeEvents`. Can also be run manually:
+ *
+ * bun run build:custom-viz-fixtures
+ */
+
+/* global console */
+import { execFileSync } from "node:child_process";
+import { cpSync, mkdirSync, rmSync, statSync } from "node:fs";
+import { dirname, resolve } from "node:path";
+import { fileURLToPath } from "node:url";
+
+import { build } from "esbuild";
+
+const __dirname = dirname(fileURLToPath(import.meta.url));
+const REPO_ROOT = resolve(__dirname, "../../../../..");
+const E2E_ASSETS = resolve(REPO_ROOT, "e2e/support/assets");
+const PLUGIN_ROOT = resolve(__dirname, "example_custom_viz_plugin");
+
+const visualizations = [
+ {
+ index: resolve(PLUGIN_ROOT, "src/index.tsx"),
+ manifest: resolve(PLUGIN_ROOT, "manifests/demo-viz.json"),
+ out: resolve(E2E_ASSETS, "example_custom_viz_plugin.tgz"),
+ },
+ {
+ index: resolve(PLUGIN_ROOT, "src/index.tsx"),
+ manifest: resolve(PLUGIN_ROOT, "manifests/demo-viz-2.json"),
+ out: resolve(E2E_ASSETS, "example_custom_viz_plugin_2.tgz"),
+ },
+ {
+ index: resolve(PLUGIN_ROOT, "src/index-widget-security.tsx"),
+ manifest: resolve(PLUGIN_ROOT, "manifests/demo-viz-security.json"),
+ out: resolve(E2E_ASSETS, "example_custom_viz_plugin_3_security.tgz"),
+ },
+];
+
+export async function buildCustomVizFixtures() {
+ for (const viz of visualizations) {
+ await buildDemoViz(viz);
+ }
+}
+
+await buildCustomVizFixtures();
+
+async function buildDemoViz({ index, manifest, out }) {
+ const stage = resolve(PLUGIN_ROOT, ".stage");
+
+ rmSync(stage, { recursive: true, force: true });
+ mkdirSync(resolve(stage, "dist/assets"), { recursive: true });
+
+ // Bundle the React-based plugin into a single IIFE. We bundle React
+ // itself: real customer plugins also self-contain React, and the
+ // sandbox doesn't expose a host React on `__METABASE_VIZ_API__`.
+ await build({
+ entryPoints: [index],
+ bundle: true,
+ format: "iife",
+ globalName: "__customVizPlugin__",
+ footer: {
+ js: "globalThis.__customVizPlugin__ = __customVizPlugin__.default;",
+ },
+ platform: "browser",
+ target: "es2020",
+ outfile: resolve(stage, "dist/index.js"),
+ jsx: "automatic",
+ define: { "process.env.NODE_ENV": '"production"' },
+ legalComments: "none",
+ minify: true,
+ });
+
+ cpSync(manifest, resolve(stage, "metabase-plugin.json"));
+
+ execFileSync(
+ "tar",
+ ["-czf", out, "-C", stage, "metabase-plugin.json", "dist"],
+ { stdio: "inherit" },
+ );
+ const { size } = statSync(out);
+ console.log(`Packed ${out} (${(size / 1024).toFixed(1)} KiB)`);
+
+ rmSync(stage, { recursive: true, force: true });
+}
diff --git a/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/manifests/demo-viz-2.json b/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/manifests/demo-viz-2.json
new file mode 100644
index 00000000000..8d28abca9a0
--- /dev/null
+++ b/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/manifests/demo-viz-2.json
@@ -0,0 +1,7 @@
+{
+ "name": "demo-viz-2",
+ "icon": "icon.svg",
+ "metabase": {
+ "version": ">=0.57.0"
+ }
+}
diff --git a/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/manifests/demo-viz-security.json b/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/manifests/demo-viz-security.json
new file mode 100644
index 00000000000..44d9d133676
--- /dev/null
+++ b/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/manifests/demo-viz-security.json
@@ -0,0 +1,7 @@
+{
+ "name": "demo-viz-security",
+ "icon": "icon.svg",
+ "metabase": {
+ "version": ">=0.57.0"
+ }
+}
diff --git a/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/manifests/demo-viz.json b/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/manifests/demo-viz.json
new file mode 100644
index 00000000000..3cde2a832b0
--- /dev/null
+++ b/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/manifests/demo-viz.json
@@ -0,0 +1,7 @@
+{
+ "name": "demo-viz",
+ "icon": "icon.svg",
+ "metabase": {
+ "version": ">=0.57.0"
+ }
+}
diff --git a/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/public/assets/icon.svg b/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/public/assets/icon.svg
new file mode 100644
index 00000000000..41d00625565
--- /dev/null
+++ b/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/public/assets/icon.svg
@@ -0,0 +1,3 @@
+<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
+ <path d="M1.79978 6.86661C1.65041 7.01583 1.53192 7.19302 1.45107 7.38806C1.37023 7.58309 1.32861 7.79215 1.32861 8.00328C1.32861 8.21441 1.37023 8.42347 1.45107 8.61851C1.53192 8.81354 1.65041 8.99073 1.79978 9.13995L6.85978 14.1999C7.00899 14.3493 7.18618 14.4678 7.38122 14.5487C7.57626 14.6295 7.78531 14.6711 7.99644 14.6711C8.20757 14.6711 8.41663 14.6295 8.61167 14.5487C8.80671 14.4678 8.9839 14.3493 9.13311 14.1999L14.1931 9.13995C14.3425 8.99073 14.461 8.81354 14.5418 8.61851C14.6227 8.42347 14.6643 8.21441 14.6643 8.00328C14.6643 7.79215 14.6227 7.58309 14.5418 7.38806C14.461 7.19302 14.3425 7.01583 14.1931 6.86661L9.13311 1.80661C8.9839 1.65725 8.80671 1.53875 8.61167 1.45791C8.41663 1.37706 8.20757 1.33545 7.99644 1.33545C7.78531 1.33545 7.57626 1.37706 7.38122 1.45791C7.18618 1.53875 7.00899 1.65725 6.85978 1.80661L1.79978 6.86661Z" stroke="black" stroke-width="1.33333" stroke-linecap="round" stroke-linejoin="round"/>
+</svg>
diff --git a/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/src/Visualization.tsx b/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/src/Visualization.tsx
new file mode 100644
index 00000000000..d5986f8ff36
--- /dev/null
+++ b/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/src/Visualization.tsx
@@ -0,0 +1,77 @@
+import { useState } from "react";
+import type { MouseEvent } from "react";
+import type { CustomVisualizationProps, RowValue } from "@metabase/custom-viz";
+import type { Settings } from "./types";
+
+export const Visualization = (
+ props: CustomVisualizationProps<Settings> & { locale: string },
+) => {
+ const { series, settings, onClick, onHover, locale } = props;
+ const { threshold } = settings;
+ const { cols, rows } = series[0].data;
+ const value = rows[0][0];
+
+ const [lastClickValue, setLastClickValue] = useState<RowValue | null>(null);
+ const [lastHoverValue, setLastHoverValue] = useState<RowValue | null>(null);
+
+ if (typeof value !== "number" || typeof threshold !== "number") {
+ throw new Error("Value and threshold need to be numbers");
+ }
+
+ function handleClick(event: MouseEvent<HTMLButtonElement>) {
+ setLastClickValue(value);
+ onClick({
+ value,
+ column: cols[0],
+ settings,
+ event: event.nativeEvent,
+ element: event.currentTarget,
+ origin: { row: rows[0], cols },
+ data: [{ value, col: cols[0] }],
+ });
+ }
+
+ function handleHoverEnter(event: MouseEvent<HTMLDivElement>) {
+ setLastHoverValue(value);
+ onHover({
+ value,
+ column: cols[0],
+ event: event.nativeEvent,
+ element: event.currentTarget,
+ data: [{ key: cols[0].name, value, col: cols[0] }],
+ });
+ }
+
+ function handleHoverLeave() {
+ onHover(null);
+ }
+
+ return (
+ <div>
+ <h1>Custom viz rendered successfully</h1>
+ <div>Threshold: {threshold}</div>
+ <div>Value: {value}</div>
+ <div data-testid="demo-viz-locale">Locale: {locale}</div>
+ <button
+ type="button"
+ data-testid="demo-viz-click-target"
+ onClick={handleClick}
+ >
+ Click me
+ </button>
+ <div
+ onMouseEnter={handleHoverEnter}
+ onMouseLeave={handleHoverLeave}
+ data-testid="demo-viz-hover-target"
+ >
+ Hover me
+ </div>
+ <div data-testid="demo-viz-last-click">
+ Last clicked: {lastClickValue === null ? "none" : lastClickValue}
+ </div>
+ <div data-testid="demo-viz-last-hover">
+ Last hovered: {lastHoverValue === null ? "none" : lastHoverValue}
+ </div>
+ </div>
+ );
+};
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.tsx
new file mode 100644
index 00000000000..58d17c561f2
--- /dev/null
+++ b/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/src/index-widget-security.tsx
@@ -0,0 +1,45 @@
+import { defineConfig } from "../../../src/index";
+import type { CreateCustomVisualization } from "../../../src/types/viz";
+import { Visualization } from "./Visualization";
+import { Settings } from "./types";
+
+const createVisualization: CreateCustomVisualization<Settings> = ({
+ defineSetting,
+ locale,
+}) => {
+ return defineConfig<Settings>({
+ id: "example_custom_viz_plugin",
+ getName: () => "example_custom_viz_plugin",
+ minSize: { width: 2, height: 2 },
+ checkRenderable(series) {
+ if (series.length !== 1) {
+ throw new Error("Only 1 series is supported");
+ }
+ },
+ settings: {
+ threshold: defineSetting({
+ id: "threshold",
+ title: "Threspachold",
+ widget: "number",
+ getDefault() {
+ return 0;
+ },
+ getProps() {
+ window.fetch("https://example.com");
+ return {
+ options: {
+ isInteger: false,
+ isNonNegative: false,
+ },
+ placeholder: "Set threshold",
+ };
+ },
+ }),
+ },
+ VisualizationComponent: (props) => (
+ <Visualization {...props} locale={locale} />
+ ),
+ });
+};
+
+export default createVisualization;
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.tsx
new file mode 100644
index 00000000000..a9354455c71
--- /dev/null
+++ b/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/src/index.tsx
@@ -0,0 +1,69 @@
+import { defineConfig } from "../../../src/index";
+import type { CreateCustomVisualization } from "../../../src/types/viz";
+import { Visualization } from "./Visualization";
+
+type Settings = {
+ threshold?: number;
+};
+
+const createVisualization: CreateCustomVisualization<Settings> = ({
+ defineSetting,
+ locale,
+}) => {
+ return defineConfig<Settings>({
+ id: "example_custom_viz_plugin",
+ getName: () => "example_custom_viz_plugin",
+ minSize: { width: 2, height: 2 },
+ checkRenderable(series, settings) {
+ if (series.length !== 1) {
+ throw new Error("Only 1 series is supported");
+ }
+
+ const [
+ {
+ data: { cols, rows },
+ },
+ ] = series;
+
+ if (cols.length !== 1) {
+ throw new Error("Query results should only have 1 column");
+ }
+
+ if (rows.length !== 1) {
+ throw new Error("Query results should only have 1 row");
+ }
+
+ if (typeof rows[0][0] !== "number") {
+ throw new Error("Result is not a number");
+ }
+
+ if (typeof settings.threshold !== "number") {
+ throw new Error("Threshold setting is not set");
+ }
+ },
+ settings: {
+ threshold: defineSetting({
+ id: "threshold",
+ title: "Threspachold",
+ widget: "number",
+ getDefault() {
+ return 0;
+ },
+ getProps() {
+ return {
+ options: {
+ isInteger: false,
+ isNonNegative: false,
+ },
+ placeholder: "Set threshold",
+ };
+ },
+ }),
+ },
+ VisualizationComponent: (props) => (
+ <Visualization {...props} locale={locale} />
+ ),
+ });
+};
+
+export default createVisualization;
diff --git a/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/src/types.ts b/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/src/types.ts
new file mode 100644
index 00000000000..3058efa51dc
--- /dev/null
+++ b/enterprise/frontend/src/custom-viz/fixtures/example_custom_viz_plugin/src/types.ts
@@ -0,0 +1,3 @@
+export type Settings = {
+ threshold?: number;
+};
diff --git a/eslint.config.mjs b/eslint.config.mjs
index 0fd687524ad..e4d5df0805e 100644
--- a/eslint.config.mjs
+++ b/eslint.config.mjs
@@ -82,6 +82,7 @@ const configs = [
"e2e/embedding-sdk-host-apps/**",
"e2e/tmp/**",
"frontend/test/__support__/custom-viz-fixtures/**/*.js",
+ "**/custom-viz/fixtures/example_custom_viz_plugin/**",
"node_modules/**",
"**/dist/**",
"**/target/**",
diff --git a/package.json b/package.json
index af3144523b9..e0ef26fe2ea 100644
--- a/package.json
+++ b/package.json
@@ -478,6 +478,7 @@
"build-watch:cljs": "bun install && shadow-cljs watch app",
"build-watch:js": "bun install && NODE_OPTIONS=--max-old-space-size=8192 rspack serve --watch",
"build:cljs": "bun install && bun run build-pure:cljs",
+ "build:custom-viz-fixtures": "node enterprise/frontend/src/custom-viz/fixtures/build-example-custom-viz.mjs",
"build:js": "bun install && rspack build",
"chromatic": "bun run build:cljs && NODE_OPTIONS=--max-old-space-size=8192 chromatic",
"ci": "bun run ci-frontend && bun run ci-backend",