Skip to content

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

Add remaining data apps sandboxing e2e (#82170)

Timofey Kachalovfc18a66b6ce4
diff --git a/e2e/support/assets/data-apps/isolation/src/probes.ts b/e2e/support/assets/data-apps/isolation/src/probes.ts
index 748f2de8a1e..2f9202ba16d 100644
--- a/e2e/support/assets/data-apps/isolation/src/probes.ts
+++ b/e2e/support/assets/data-apps/isolation/src/probes.ts
@@ -1,5 +1,12 @@
import type { IsolationTestEnv, Probe, Report } from "./types";
-import { blobUrl, describeError, reportRealm } from "./utils";
+import {
+ blobUrl,
+ describeError,
+ htmlDocIframe,
+ reportRealm,
+ templateIframe,
+ xmlDocIframe,
+} from "./utils";
// BFS the endowed object graph for a raw `Api` exposing a provisioning
// endpoint; stop at the reference, never dispatch.
@@ -65,6 +72,90 @@ const endowmentApi = (report: Report) => {
report("isolated:no-provisioning-endpoint-in-endowments");
};
+// V8 `CallSite[]` from a host-dispatched frame would hand back the real host
+// `Function` (→ host `globalThis`, un-gated `fetch`). The guest realm's
+// `prepareStackTrace` is a non-configurable accessor that rejects functions.
+const stackTraceRealm = (report: Report) => {
+ const guestFetch = window.fetch;
+ const captureStackTrace = (
+ Error as { captureStackTrace?: (o: object) => void }
+ ).captureStackTrace;
+
+ if (typeof captureStackTrace !== "function") {
+ report("isolated:no-callsite-formatter");
+ return;
+ }
+
+ const original = Error.prepareStackTrace;
+ const formatter = (_err: Error, frames: unknown[]) => frames;
+
+ Error.prepareStackTrace = formatter;
+
+ const installed = Error.prepareStackTrace === formatter;
+
+ const holder: { stack?: unknown } = {};
+ captureStackTrace(holder);
+
+ const frames = (Array.isArray(holder.stack) ? holder.stack : []) as Array<{
+ getFunction?: () => unknown;
+ getThis?: () => unknown;
+ }>;
+
+ Error.prepareStackTrace = original;
+
+ if (!installed && frames.length === 0) {
+ report("isolated:prepare-stack-trace-gated");
+ return;
+ }
+
+ for (const frame of frames) {
+ let fn: unknown;
+ let self: unknown;
+
+ try {
+ fn = frame.getFunction?.();
+ } catch {
+ // strict frame — suppressed
+ }
+
+ try {
+ self = frame.getThis?.();
+ } catch {
+ // strict frame — suppressed
+ }
+
+ const carrier =
+ typeof fn === "function"
+ ? (fn as { constructor?: unknown })
+ : self && typeof self === "object"
+ ? ((self as { constructor?: unknown }).constructor as {
+ constructor?: unknown;
+ })
+ : undefined;
+
+ const FnCtor = carrier?.constructor as
+ | ((body: string) => () => unknown)
+ | undefined;
+
+ if (typeof FnCtor !== "function") {
+ continue;
+ }
+
+ try {
+ const foreign = FnCtor("return globalThis")() as Window;
+
+ if (foreign && foreign.fetch !== guestFetch) {
+ report("reached:stack-trace-realm");
+ return;
+ }
+ } catch {
+ // constructor gated in this realm — keep scanning
+ }
+ }
+
+ report(`reached:call-sites-in-${frames.length}-frames`);
+};
+
export const createProbes = (
env: IsolationTestEnv,
report: Report,
@@ -72,6 +163,13 @@ export const createProbes = (
const realm = (win: Window | null | undefined, label: string) =>
reportRealm(report, win, label);
+ const adoptedRealm = (make: () => HTMLIFrameElement, label: string) => {
+ const iframe = make();
+ document.body.appendChild(document.adoptNode(iframe));
+
+ realm(iframe.contentWindow, label);
+ };
+
const parsedIframeRealm = (root: ParentNode, label: string) => {
const iframe = root.querySelector("iframe");
@@ -159,6 +257,31 @@ export const createProbes = (
"dom-parser",
),
},
+ {
+ id: "adopt-html-doc-iframe",
+ label: "createHTMLDocument iframe + adoptNode",
+ run: () => adoptedRealm(htmlDocIframe, "adopt-html-doc"),
+ },
+ {
+ id: "import-html-doc-iframe",
+ label: "createHTMLDocument iframe + importNode",
+ run: () => {
+ const iframe = document.importNode(htmlDocIframe(), true);
+ document.body.appendChild(iframe);
+
+ realm(iframe.contentWindow, "import-html-doc");
+ },
+ },
+ {
+ id: "xml-doc-iframe",
+ label: "createDocument iframe + adoptNode",
+ run: () => adoptedRealm(xmlDocIframe, "xml-doc"),
+ },
+ {
+ id: "template-doc-iframe",
+ label: "template owner-document iframe + adoptNode",
+ run: () => adoptedRealm(templateIframe, "template-doc"),
+ },
{
id: "range-fragment-iframe",
label: "iframe via Range.createContextualFragment",
@@ -190,6 +313,55 @@ export const createProbes = (
}, 1000);
},
},
+ {
+ id: "window-parent",
+ label: "window.parent",
+ run: () => realm(window.parent, "parent"),
+ },
+ {
+ id: "window-top",
+ label: "window.top",
+ run: () => realm(window.top, "top"),
+ },
+ {
+ id: "frame-element",
+ label: "window.frameElement",
+ run: () =>
+ realm(window.frameElement?.ownerDocument?.defaultView, "frame-element"),
+ },
+ {
+ id: "parent-chain",
+ label: "window.parent.parent",
+ run: () => realm(window.parent?.parent, "parent-chain"),
+ },
+ {
+ id: "window-frames",
+ label: "window.frames[0]",
+ run: () => {
+ document.body.appendChild(document.adoptNode(htmlDocIframe()));
+
+ realm(
+ window.frames?.[0] ?? (window as unknown as Window[])[0],
+ "window-frames",
+ );
+ },
+ },
+ {
+ id: "window-opener",
+ label: "window.opener",
+ run: () => realm(window.opener, "window-opener"),
+ },
+ {
+ id: "child-frame-grab",
+ label: "host iframe's realm",
+ run: () => {
+ const foreign = Array.from(document.querySelectorAll("iframe"))
+ .map((iframe) => iframe.contentWindow)
+ .find((win): win is Window => !!win && win !== window);
+
+ realm(foreign ?? null, "child-frame");
+ },
+ },
{
id: "parent-cookie",
label: "host cookie via window.parent",
@@ -353,5 +525,10 @@ export const createProbes = (
label: "Raw API via endowed SDK bundle",
run: () => endowmentApi(report),
},
+ {
+ id: "stack-trace-realm",
+ label: "Realm reference via Error.prepareStackTrace",
+ run: () => stackTraceRealm(report),
+ },
];
};
diff --git a/e2e/test/scenarios/data-apps/sandboxing_isolation.cy.spec.ts b/e2e/test/scenarios/data-apps/sandboxing_isolation.cy.spec.ts
index dbad5a01357..515ae37b075 100644
--- a/e2e/test/scenarios/data-apps/sandboxing_isolation.cy.spec.ts
+++ b/e2e/test/scenarios/data-apps/sandboxing_isolation.cy.spec.ts
@@ -122,11 +122,56 @@ describe("scenarios > data apps > sandbox isolation", () => {
runProbe("isolation-dom-parser");
});
+ it("keeps a createHTMLDocument iframe adopted into the realm gated", () => {
+ setUp();
+ runProbe("isolation-adopt-html-doc-iframe");
+ });
+
+ it("keeps a createHTMLDocument iframe imported into the realm gated", () => {
+ setUp();
+ runProbe("isolation-import-html-doc-iframe");
+ });
+
+ it("keeps a createDocument iframe adopted into the realm gated", () => {
+ setUp();
+ runProbe("isolation-xml-doc-iframe");
+ });
+
+ it("keeps a template owner-document iframe adopted into the realm gated", () => {
+ setUp();
+ runProbe("isolation-template-doc-iframe");
+ });
+
it("keeps a raw API out of the SDK endowments", () => {
setUp();
runProbe("isolation-endowment-api");
});
+ it("keeps an Error.prepareStackTrace realm reference gated", () => {
+ setUp();
+ runProbe("isolation-stack-trace-realm");
+ });
+
+ it("keeps window.parent within the gated realm", () => {
+ setUp();
+ runProbe("isolation-window-parent");
+ });
+
+ it("keeps window.top within the gated realm", () => {
+ setUp();
+ runProbe("isolation-window-top");
+ });
+
+ it("keeps window.frameElement's owner realm gated", () => {
+ setUp();
+ runProbe("isolation-frame-element");
+ });
+
+ it("keeps window.parent.parent within the gated realm", () => {
+ setUp();
+ runProbe("isolation-parent-chain");
+ });
+
it("gates document.cookie on the parent realm", () => {
setUp();
runProbe("isolation-parent-cookie");
@@ -152,6 +197,16 @@ describe("scenarios > data apps > sandbox isolation", () => {
runProbe("isolation-parent-caches");
});
+ it("keeps indexed window.frames access gated", () => {
+ setUp();
+ runProbe("isolation-window-frames");
+ });
+
+ it("keeps window.opener's realm gated", () => {
+ setUp();
+ runProbe("isolation-window-opener");
+ });
+
it("gates FontFace.load", () => {
setUp();
runProbe("isolation-font-face");
@@ -177,6 +232,29 @@ describe("scenarios > data apps > sandbox isolation", () => {
runProbe("isolation-custom-element");
});
+ it("keeps a host iframe's realm gated", () => {
+ setUp();
+
+ // A srcless (about:blank) iframe is same-origin, so its `contentWindow` is a
+ // live realm with an un-gated `fetch` — the same capability html2canvas's
+ // clone iframe has. Create it in the PARENT (whose createElement the host
+ // guard never patched) and adopt it into the data-app document, mimicking a
+ // host-created child frame the guest then reaches for.
+ H.dataAppIframe(APP_DISPLAY_NAME).then(($body) => {
+ const doc = $body[0].ownerDocument;
+ const parentDoc = doc.defaultView!.parent.document;
+ const iframe = parentDoc.createElement("iframe");
+ iframe.style.display = "none";
+ doc.body.appendChild(doc.adoptNode(iframe));
+
+ expect(iframe.contentWindow !== null, "adopted iframe realm").to.equal(
+ true,
+ );
+ });
+
+ runProbe("isolation-child-frame-grab");
+ });
+
it("gates an allowed_host redirect to the instance", () => {
const instanceUrl = Cypress.config("baseUrl") ?? "";
const cors = {