Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
fix: Ignore messages from unknown sources e.g. browser extension (#5666)
Oleg Isonen4f36bfd89927
diff --git a/apps/builder/app/shared/pubsub/create.test.ts b/apps/builder/app/shared/pubsub/create.test.tsindex f0eb0e0664..402f16df31 100644--- a/apps/builder/app/shared/pubsub/create.test.ts+++ b/apps/builder/app/shared/pubsub/create.test.ts@@ -305,7 +305,7 @@ describe("createPubsub", () => {});describe("message unwrapping and validation", () => {- test("should reject invalid payload (not an object)", () => {+ test("should silently ignore invalid payload (not an object)", () => {window.self = asWindow(window);window.top = asWindow(window);@@ -316,23 +316,13 @@ describe("createPubsub", () => {const messageHandler = addEventListenerSpy.mock.calls[0][1];- // Mock console.error to avoid test output noise- const consoleErrorSpy = vi- .spyOn(console, "error")- .mockImplementation(() => {});+ // Non-object data is silently ignored by handleMessage guard+ messageHandler({ data: "invalid" });- expect(() => {- messageHandler({ data: "invalid" });- }).toThrow("Invalid payload");-- expect(consoleErrorSpy).toHaveBeenCalledWith(- "Invalid payload",- "invalid"- );- consoleErrorSpy.mockRestore();+ expect(handler).not.toHaveBeenCalled();});- test("should reject payload without token", () => {+ test("should silently ignore payload without token", () => {window.self = asWindow(window);window.top = asWindow(window);@@ -343,9 +333,10 @@ describe("createPubsub", () => {const messageHandler = addEventListenerSpy.mock.calls[0][1];- expect(() => {- messageHandler({ data: { action: { type: "testAction" } } });- }).toThrow("Invalid payload, not wrapped");+ // Payload without token is silently ignored by handleMessage guard+ messageHandler({ data: { action: { type: "testAction" } } });++ expect(handler).not.toHaveBeenCalled();});test("should reject payload with invalid token", () => {@@ -430,7 +421,13 @@ describe("createPubsub", () => {// Get the message handler from this pubsub instanceconst messageHandler = localAddEventListenerSpy.mock.calls[0][1];- messageHandler({ data: "storybook-data" });+ // In storybook, messages still need to be properly wrapped to pass handleMessage guard+ messageHandler({+ data: {+ token: "development-token",+ action: { type: "storybook", payload: "storybook-data" },+ },+ });vi.runAllTimers();diff --git a/apps/builder/app/shared/pubsub/create.ts b/apps/builder/app/shared/pubsub/create.tsindex 6f06a5f6fb..e9f60d3544 100644--- a/apps/builder/app/shared/pubsub/create.ts+++ b/apps/builder/app/shared/pubsub/create.ts@@ -99,6 +99,14 @@ export const createPubsub = <PublishMap>() => {};const handleMessage = (event: MessageEvent) => {+ // Ignore messages from unknown sources (e.g. browser extensions, third-party scripts)+ if (+ typeof event.data !== "object" ||+ event.data === null ||+ !("token" in event.data)+ ) {+ return;+ }const action = unwrapAction(event.data);const type = action.type;// Execute all updates within a single batch to improve performance