Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions sandpack-client/src/clients/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,13 @@ export class SandpackNode extends SandpackClient {
private async globalListeners(): Promise<void> {
window.addEventListener("message", (event) => {
if (event.data.type === PREVIEW_LOADED_MESSAGE_TYPE) {
injectScriptToIframe(this.iframe, this.messageChannelId);
if (
this.options.bundlerURL &&
event.origin !== new URL(this.options.bundlerURL).origin
) {
return;
}
injectScriptToIframe(this.iframe, this.messageChannelId, window.location.origin);
}

if (
Expand Down Expand Up @@ -435,9 +441,11 @@ export class SandpackNode extends SandpackClient {
break;

case "urlback":
case "urlforward":
this.iframe?.contentWindow?.postMessage(message, "*");
case "urlforward": {
const targetOrigin = this.iframePreviewUrl ? new URL(this.iframePreviewUrl).origin : "*";
this.iframe?.contentWindow?.postMessage(message, targetOrigin);
break;
}

case "shell/restart":
this.restartShellProcess();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* @jest-environment jsdom
*/
import { setupHistoryListeners } from "./historyListener";

describe("historyListener", () => {
let mockPostMessage: jest.Mock;
let mockAddEventListener: jest.Mock;
let historyBackSpy: jest.SpyInstance;

beforeEach(() => {
mockPostMessage = jest.fn();
mockAddEventListener = jest.fn();

// Mock parent postMessage
(global as any).parent = {
postMessage: mockPostMessage,
};

// Mock window methods
window.addEventListener = mockAddEventListener;
historyBackSpy = jest.spyOn(window.history, "back").mockImplementation(() => {});

Object.defineProperty(window, "location", {
value: {
href: "https://sandbox.com",
reload: jest.fn(),
},
writable: true
});
});

afterEach(() => {
jest.clearAllMocks();
});

it("should ignore messages from unauthorized origins", () => {
setupHistoryListeners({
scope: { channelId: "123", parentOrigin: "https://trusted.com" },
});

const historyGoSpy = jest.spyOn(window.history, "go").mockImplementation(() => {});

const handleMessage = mockAddEventListener.mock.calls.find(
(call) => call[0] === "message"
)[1];

handleMessage({
data: { type: "urlback" },
origin: "https://malicious.com",
});

expect(historyGoSpy).not.toHaveBeenCalled();
});

it("should accept messages from authorized origin", () => {
setupHistoryListeners({
scope: { channelId: "123", parentOrigin: "https://trusted.com" },
});

const historyGoSpy = jest.spyOn(window.history, "go").mockImplementation(() => {});

const handleMessage = mockAddEventListener.mock.calls.find(
(call) => call[0] === "message"
)[1];

handleMessage({
data: { type: "urlback" },
origin: "https://trusted.com",
});

expect(historyGoSpy).toHaveBeenCalledWith(-1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
export function setupHistoryListeners({
scope,
}: {
scope: { channelId: string };
scope: { channelId: string; parentOrigin: string };
}) {
// @ts-ignore
const origHistoryProto = window.history.__proto__;
Expand All @@ -20,7 +20,7 @@ export function setupHistoryListeners({
forward: historyPosition < historyList.length - 1,
channelId: scope.channelId,
},
"*"
scope.parentOrigin
);
};

Expand Down Expand Up @@ -71,11 +71,14 @@ export function setupHistoryListeners({
interface NavigationMessage {
type: "urlback" | "urlforward" | "refresh";
}
function handleMessage({ data }: { data: NavigationMessage }) {
function handleMessage(event: MessageEvent) {
const { data, origin } = event;
if (origin !== scope.parentOrigin) return;

if (data.type === "urlback") {
history.back();
window.history.back();
} else if (data.type === "urlforward") {
history.forward();
window.history.forward();
} else if (data.type === "refresh") {
document.location.reload();
}
Expand Down
5 changes: 3 additions & 2 deletions sandpack-client/src/clients/node/inject-scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ const scripts = [

export const injectScriptToIframe = (
iframe: HTMLIFrameElement,
channelId: string
channelId: string,
parentOrigin: string
): void => {
scripts.forEach(({ code, id }) => {
const message: InjectMessage = {
uid: id,
type: INJECT_MESSAGE_TYPE,
code: `exports.activate = ${code}`,
scope: { channelId },
scope: { channelId, parentOrigin },
};

iframe.contentWindow?.postMessage(message, "*");
Expand Down