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
Fix getComputedStyle crash in data app sandbox (#81739)
Phoomparin Manoee9cd6a54e8d
diff --git a/enterprise/frontend/src/metabase-enterprise/data_apps/sandbox/distortions.ts b/enterprise/frontend/src/metabase-enterprise/data_apps/sandbox/distortions.tsindex ee0eaf50b7c..459ea2c1c9c 100644--- a/enterprise/frontend/src/metabase-enterprise/data_apps/sandbox/distortions.ts+++ b/enterprise/frontend/src/metabase-enterprise/data_apps/sandbox/distortions.ts@@ -51,11 +51,9 @@ export function makeDistortionCallback(// Ancestor realms reachable from the sandboxed iframe.// Near-Membrane remaps the guest's `window.parent` to the real parent (the// same-origin main app) — a realm it never wrapped, so its `fetch` isn't gated.- // Redirect every ancestor window, plus the frame element reachable via- // `ownerDocument.defaultView`, to the gated target: a valid Window->Window- // distortion, so `window.parent`/`frameElement` resolve to THIS realm (where- // `fetch`/`XMLHttpRequest` are gated). Captured before the membrane is built,- // while the ancestors are still reachable.+ // Redirect every ancestor window to the gated target, and hide the frame+ // element so DOM traversal libraries don't escape into the parent document.+ // Captured before the membrane is built, while the ancestors are reachable.const realm = targetWindow as unknown as Window;// A same-origin child frame's realm is reachable ONLY through@@ -65,8 +63,20 @@ export function makeDistortionCallback(// creates during a chart export — collapses to THIS gated realm. Deliberately// narrow: matching the whole `Window` type instead fires on every window the SDK// resolves at load and blows the stack.- const getterOf = (proto: object, key: string) =>- Object.getOwnPropertyDescriptor(proto, key)?.get;+ const getterOf = (target: object, key: string) => {+ let current: object | null = target;++ while (current) {+ const getter = Object.getOwnPropertyDescriptor(current, key)?.get;++ if (getter) {+ return getter;+ }++ current = Object.getPrototypeOf(current);+ }+ };+const realmCreatingProtos = [HTMLIFrameElement,HTMLFrameElement,@@ -83,6 +93,7 @@ export function makeDistortionCallback(.map((p) => getterOf(p, "contentDocument")).filter(Boolean),);+ const frameElementGetter = getterOf(realm, "frameElement");const getGatedRealm = () => realm;const getNull = () => null;@@ -116,14 +127,6 @@ export function makeDistortionCallback(// top unreachable — nothing to redirect}- try {- if (realm.frameElement) {- ancestors.add(realm.frameElement);- }- } catch {- // frameElement unreachable — nothing to redirect- }-try {if (realm.opener) {ancestors.add(realm.opener);@@ -147,6 +150,10 @@ export function makeDistortionCallback(return realm;}+ if (frameElementGetter && value === frameElementGetter) {+ return getNull;+ }+// A child frame's `contentWindow` resolves to the gated realm; its// `contentDocument` to null — so neither reaches a live child realm.if (contentWindowGetters.has(value)) {diff --git a/enterprise/frontend/src/metabase-enterprise/data_apps/sandbox/distortions.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/data_apps/sandbox/distortions.unit.spec.tsindex 6902165ecc6..a4b92491aaa 100644--- a/enterprise/frontend/src/metabase-enterprise/data_apps/sandbox/distortions.unit.spec.ts+++ b/enterprise/frontend/src/metabase-enterprise/data_apps/sandbox/distortions.unit.spec.ts@@ -2,9 +2,9 @@ import { makeDistortionCallback } from "./distortions";import type { SandboxRealm } from "./types";// A minimal stand-in for the iframe window the data-app sandbox is built on.-// `fetch`/`XMLHttpRequest` are the native refs the membrane passes to the-// distortion callback; `makeDistortionCallback` should route those to the-// allowlist wrappers and delegate everything else to the shared callback.+// These are the native refs the membrane passes to the distortion callback;+// `makeDistortionCallback` should route them to the data-app-specific wrappers+// and delegate everything else to the shared callback.const fakeWindow = (): SandboxRealm => ({fetch: async () => new Response("native"),XMLHttpRequest: class NativeXHR extends XMLHttpRequest {},@@ -88,11 +88,20 @@ describe("makeDistortionCallback", () => {// The sandboxed iframe is same-origin with the main app, and Near-Membrane// remaps the guest's `window.parent` to the target's real parent — an ungated// realm whose fetch carries the session. The callback must redirect every- // ancestor (and the frame element) back to the gated target window.- it("redirects ancestor windows and the frame element to the gated target", () => {+ // ancestor to the gated target window and hide the host frame element.+ it("redirects ancestor windows and hides the frame element", () => {const grandparent = { name: "top" };const parent = { name: "main-app", parent: grandparent };const frameElement = { tagName: "IFRAME" };++ const frameElementGetter = () => frameElement;+ const windowPrototype = {};++ Object.defineProperty(windowPrototype, "frameElement", {+ configurable: true,+ get: frameElementGetter,+ });+// The real realm is a `Window`; the distortion only reads `parent`/`top`/// `frameElement` off it, which this stand-in supplies. Cast through the// narrow `SandboxRealm` the callback is typed against.@@ -100,15 +109,27 @@ describe("makeDistortionCallback", () => {...fakeWindow(),parent,top: grandparent,- frameElement,} as unknown as SandboxRealm;+ Object.setPrototypeOf(win, windowPrototype);+const callback = makeDistortionCallback("sales", win, []);+ const distortedFrameElementGetter = callback(frameElementGetter);// Every ancestor reachable from the realm resolves back to the gated realm.expect(callback(parent)).toBe(win);expect(callback(grandparent)).toBe(win);- expect(callback(frameElement)).toBe(win);++ expect(Object.hasOwn(win, "frameElement")).toBe(false);+ expect(distortedFrameElementGetter).not.toBe(frameElementGetter);+ expect(distortedFrameElementGetter).toEqual(expect.any(Function));++ if (typeof distortedFrameElementGetter !== "function") {+ throw new Error("Expected frameElement getter distortion");+ }++ expect(distortedFrameElementGetter()).toBeNull();+ expect(callback(frameElement)).toBe(frameElement);});it("leaves the target window itself untouched (parent chain stops at self)", () => {