Skip to content

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

8.0 KB
Raw Blame
import { makeDistortionCallback } from "./distortions";
import type { SandboxRealm } from "./types";

// A minimal stand-in for the iframe window the data-app sandbox is built on.
// 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 {},
  location: {
    href: "https://mb.example.com/embed/apps/sales",
    origin: "https://mb.example.com",
  },
});

describe("makeDistortionCallback", () => {
  it("allows style elements for bundled CSS injection", () => {
    const win = fakeWindow();
    const callback = makeDistortionCallback("sales", win, []);
    // The membrane types a distortion as `object -> object`, erasing the call
    // signature of the native ref it replaces — restore it to invoke it.
    const createElement = callback(
      Document.prototype.createElement,
    ) as typeof Document.prototype.createElement;

    expect(() => createElement.call(document, "style")).not.toThrow();
  });

  it("keeps the shared dangerous-tag blocklist for non-style elements", () => {
    const consoleErrorSpy = jest
      .spyOn(console, "error")
      .mockImplementation(() => {});
    const win = fakeWindow();
    const callback = makeDistortionCallback("sales", win, []);
    // Same signature erasure as above.
    const createElement = callback(
      Document.prototype.createElement,
    ) as typeof Document.prototype.createElement;

    expect(() => createElement.call(document, "script")).toThrow(
      "[data-app sales] blocked createElement: script",
    );
    consoleErrorSpy.mockRestore();
  });

  it("routes window.fetch to the allowlisted wrapper when allowed_hosts is set", async () => {
    const win = fakeWindow();
    const realFetch = jest.fn(() => Promise.resolve(new Response("ok")));
    win.fetch = realFetch;

    const callback = makeDistortionCallback("sales", win, [
      "https://api.example.com",
    ]);
    // Same signature erasure as above.
    const distorted = callback(win.fetch) as typeof fetch;

    // Not the native fetch — a wrapper that enforces the allowlist.
    expect(distorted).not.toBe(win.fetch);
    await expect(distorted("https://evil.example.org/")).rejects.toThrow(
      /blocked fetch/,
    );
    await distorted("https://api.example.com/data");
    expect(realFetch).toHaveBeenCalledTimes(1);
  });

  it("never allowlists window.XMLHttpRequest, even when allowed_hosts is set", () => {
    const win = fakeWindow();
    const callback = makeDistortionCallback("sales", win, [
      "https://api.example.com",
    ]);
    // XHR can't opt out of following a redirect back to the instance, so it is
    // never allowlisted: it passes through to the shared hard block (in production
    // `blockHostRealmXhr` has already replaced it with a throwing stub before the
    // membrane is built).
    expect(callback(win.XMLHttpRequest)).toBe(win.XMLHttpRequest);
  });

  it("allows only the analytics proxy when allowed_hosts is empty", async () => {
    const win = fakeWindow();
    const realFetch = jest.fn(() => Promise.resolve(new Response("ok")));
    win.fetch = realFetch;

    const callback = makeDistortionCallback("sales", win, []);

    // Restore the fetch's type signature from the distortion callback
    const distorted = callback(win.fetch) as typeof fetch;
    await distorted("/api/analytics-proxy", { method: "POST" });

    // Analytics proxy endpoint must not be blocked
    expect(realFetch).toHaveBeenCalledWith("/api/analytics-proxy", {
      method: "POST",
      redirect: "manual",
    });

    await expect(distorted("/api/user/current")).rejects.toThrow(
      /blocked fetch/,
    );

    expect(realFetch).toHaveBeenCalledTimes(1);
    expect(callback(win.XMLHttpRequest)).toBe(win.XMLHttpRequest);
  });

  // 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 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.
    const win = {
      ...fakeWindow(),
      parent,
      top: grandparent,
    } 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(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)", () => {
    // A top-level realm: parent === self, so the walk finds no foreign ancestor.
    // Assigning `parent` needs the extra field the narrow type doesn't declare.
    const win = fakeWindow() as SandboxRealm & { parent: unknown };
    win.parent = win;
    const callback = makeDistortionCallback("sales", win, []);

    const plain = {};
    expect(callback(plain)).toBe(plain);
  });

  it("delegates non-network values to the shared callback", () => {
    const win = fakeWindow();
    const callback = makeDistortionCallback("sales", win, [
      "https://api.example.com",
    ]);
    const other = { some: "object" };
    expect(callback(other)).toBe(other);
  });

  describe("onBlocked", () => {
    it("reports a blocked API instead of logging it", () => {
      const consoleErrorSpy = jest
        .spyOn(console, "error")
        .mockImplementation(() => {});
      const onBlocked = jest.fn();
      const callback = makeDistortionCallback(
        "sales",
        fakeWindow(),
        [],
        onBlocked,
      );
      // Same signature erasure as above.
      const createElement = callback(
        Document.prototype.createElement,
      ) as typeof Document.prototype.createElement;

      expect(() => createElement.call(document, "script")).toThrow();

      expect(onBlocked).toHaveBeenCalledWith({
        type: "api",
        message: expect.stringContaining("blocked createElement: script"),
      });
      expect(consoleErrorSpy).not.toHaveBeenCalled();
      consoleErrorSpy.mockRestore();
    });

    it("reports a blocked fetch with the resolved url and reason", async () => {
      const onBlocked = jest.fn();
      const win = fakeWindow();
      const callback = makeDistortionCallback(
        "sales",
        win,
        ["https://api.example.com"],
        onBlocked,
      );
      // Same signature erasure as above.
      const distorted = callback(win.fetch) as typeof fetch;

      await expect(distorted("https://evil.example.org/")).rejects.toThrow();

      expect(onBlocked).toHaveBeenCalledWith({
        type: "network",
        api: "fetch",
        url: "https://evil.example.org/",
        reason: "evil.example.org (not in allowed_hosts)",
      });
    });
  });
});