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
gate event apis on window instance (#81680)
Jakub Chodorowicze4cf63ff2331
diff --git a/e2e/test/scenarios/visualizations-charts/custom-viz.cy.spec.ts b/e2e/test/scenarios/visualizations-charts/custom-viz.cy.spec.tsindex 7b4adaf0d04..f3a2a7182d7 100644--- a/e2e/test/scenarios/visualizations-charts/custom-viz.cy.spec.ts+++ b/e2e/test/scenarios/visualizations-charts/custom-viz.cy.spec.ts@@ -1813,6 +1813,33 @@ describe("sandbox", () => {payload: "document.onpaste = function () {};",errorPattern: blockedPattern(/API call: Document\.set onpaste/),},+ {+ name: "window.onstorage setter",+ payload: "window.onstorage = function () {};",+ errorPattern: blockedPattern(/API call: window\.set onstorage/),+ },+ {+ name: "window.onkeydown setter",+ payload: "window.onkeydown = function () {};",+ errorPattern: blockedPattern(/API call: window\.set onkeydown/),+ },+ {+ name: "document.body.onstorage setter",+ payload: "document.body.onstorage = function () {};",+ errorPattern: blockedPattern(/API call: HTMLBodyElement\.set onstorage/),+ },+ {+ name: "detached body.onstorage setter",+ payload: 'document.createElement("body").onstorage = function () {};',+ errorPattern: blockedPattern(/API call: HTMLBodyElement\.set onstorage/),+ },+ {+ name: "detached frameset.onstorage setter",+ payload: 'document.createElement("frameset").onstorage = function () {};',+ errorPattern: blockedPattern(+ /API call: HTMLFrameSetElement\.set onstorage/,+ ),+ },{name: 'setAttribute("onclick", ...)',payload: 'document.body.setAttribute("onclick", "alert(1)");',diff --git a/frontend/src/metabase/utils/scripts-sandbox/distortions-blocked-apis.ts b/frontend/src/metabase/utils/scripts-sandbox/distortions-blocked-apis.tsindex db1e22c21c2..6139d54ed30 100644--- a/frontend/src/metabase/utils/scripts-sandbox/distortions-blocked-apis.ts+++ b/frontend/src/metabase/utils/scripts-sandbox/distortions-blocked-apis.ts@@ -106,10 +106,20 @@ if (window.CookieStore) {}// Global event-handler IDL setters — the property-path twin of the `addEventListener` block.+// Window is a WebIDL [Global] interface, so its on* accessors are own+// properties of the window instance, not Window.prototype.for (const eventType of GLOBAL_BLOCKED_EVENT_TYPES) {const handler = `on${eventType}`;block(setter(Document.prototype, handler), `Document.set ${handler}`);- block(setter(Window.prototype, handler), `Window.set ${handler}`);+ block(setter(window, handler), `window.set ${handler}`);+ block(+ setter(HTMLBodyElement.prototype, handler),+ `HTMLBodyElement.set ${handler}`,+ );+ block(+ setter(HTMLFrameSetElement.prototype, handler),+ `HTMLFrameSetElement.set ${handler}`,+ );}// Referrer — URL of the page that linked here, which can leak internaldiff --git a/frontend/src/metabase/utils/scripts-sandbox/distortions-event.unit.spec.ts b/frontend/src/metabase/utils/scripts-sandbox/distortions-event.unit.spec.tsindex 672c764b4cf..4c3881e81a8 100644--- a/frontend/src/metabase/utils/scripts-sandbox/distortions-event.unit.spec.ts+++ b/frontend/src/metabase/utils/scripts-sandbox/distortions-event.unit.spec.ts@@ -9,14 +9,76 @@ function asStringArg(value: object): string {return value as unknown as string;}-const setterOf = (proto: object, key: string) =>- Object.getOwnPropertyDescriptor(proto, key)?.set;+type HandlerSetterOf<T> = Extract<keyof T, `on${string}`>;++const WINDOW_HANDLER_SETTERS: readonly HandlerSetterOf<Window>[] = [+ "onkeydown",+ "onkeyup",+ "onkeypress",+ "onbeforeinput",+ "oninput",+ "onpaste",+ "oncopy",+ "oncut",+ "onstorage",+];++const DOCUMENT_HANDLER_SETTERS: readonly HandlerSetterOf<Document>[] = [+ "onkeydown",+ "onkeyup",+ "onkeypress",+ "onbeforeinput",+ "oninput",+ "onpaste",+ "oncopy",+ "oncut",+];++const BODY_HANDLER_SETTERS: readonly HandlerSetterOf<HTMLBodyElement>[] = [+ "onstorage",+];-const GLOBAL_TARGETS = [- { name: "Document", proto: Document.prototype },- { name: "Window", proto: Window.prototype },+const FRAMESET_HANDLER_SETTERS: readonly HandlerSetterOf<HTMLFrameSetElement>[] =+ ["onstorage"];++const SETTER_TARGETS = [+ {+ name: "window instance",+ target: window,+ receiver: window,+ setters: WINDOW_HANDLER_SETTERS,+ },+ {+ name: "Document.prototype",+ target: Document.prototype,+ receiver: document,+ setters: DOCUMENT_HANDLER_SETTERS,+ },+ {+ name: "HTMLBodyElement.prototype",+ target: HTMLBodyElement.prototype,+ receiver: document.createElement("body"),+ setters: BODY_HANDLER_SETTERS,+ },+ {+ name: "HTMLFrameSetElement.prototype",+ target: HTMLFrameSetElement.prototype,+ receiver: document.createElement("frameset"),+ setters: FRAMESET_HANDLER_SETTERS,+ },];+function setterOf(target: object, key: string) {+ return Object.getOwnPropertyDescriptor(target, key)?.set;+}++function blockedHandlerSettersOn(target: object) {+ return [...GLOBAL_BLOCKED_EVENT_TYPES]+ .map((type) => `on${type}`)+ .filter((handler) => setterOf(target, handler))+ .sort();+}+describe("scripts-sandbox global event-handler setter distortions", () => {it("distorts addEventListener('keydown') on document (reference boundary)", () => {const distort = makeSandboxDistortionCallback("plugin 1");@@ -37,43 +99,32 @@ describe("scripts-sandbox global event-handler setter distortions", () => {expect(distort(cookieSetter)).not.toBe(cookieSetter);});- it("distorts every on* setter that mirrors a blocked global event type", () => {- const distort = makeSandboxDistortionCallback("plugin 1");-- // filter out the event types that don't have an on* setter in this environment- const results = [...GLOBAL_BLOCKED_EVENT_TYPES].flatMap((type) =>- GLOBAL_TARGETS.flatMap(({ name, proto }) => {- const setter = setterOf(proto, `on${type}`);- // filter out the event types that don't have an on* setter in this environment- return setter- ? [- {- label: `${name}.on${type}`,- distorted: distort(setter) !== setter,- },- ]- : [];- }),- );-- const undistorted = results.filter((r) => !r.distorted).map((r) => r.label);- expect(undistorted).toEqual([]);- // make sure that at least some setter in the env were found- expect(results.length).toBeGreaterThan(0);- });-- it("replaces the Document.onkeydown setter with a distortion that refuses the assignment", () => {- const distort = makeSandboxDistortionCallback("plugin 1");- const onkeydownSetter = setterOf(Document.prototype, "onkeydown");- if (!onkeydownSetter) {- throw new Error("expected a Document.onkeydown setter");- }-- const distorted = distort(onkeydownSetter);- expect(distorted).not.toBe(onkeydownSetter);-- expect(() => distorted.call(document, () => {})).toThrow();- });+ describe.each(SETTER_TARGETS)(+ "$name",+ ({ name, target, receiver, setters }) => {+ it("lists every blocked event type that has a handler setter here", () => {+ expect([...setters].sort()).toEqual(blockedHandlerSettersOn(target));+ });++ it.each(setters)(+ "replaces the %s setter with a distortion that refuses the assignment",+ (handler) => {+ const distort = makeSandboxDistortionCallback("plugin 1");+ const handlerSetter = setterOf(target, handler);+ if (!handlerSetter) {+ throw new Error(`expected a ${handler} setter on ${name}`);+ }++ const distorted = distort(handlerSetter);+ expect(distorted).not.toBe(handlerSetter);++ expect(() => distorted.call(receiver, () => {})).toThrow(+ /blocked API call/,+ );+ },+ );+ },+ );});describe("addEventListenerDistortion", () => {