Skip to content

Commit d2ef349

Browse files
authored
Remove the use of wildcard postMessage (codesandbox#1088)
1 parent b5b6cbc commit d2ef349

File tree

2 files changed

+36
-12
lines changed
  • packages

2 files changed

+36
-12
lines changed

packages/app/src/app/components/Preview/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class BasePreview extends React.Component<Props, State> {
113113
handleMessage = (data: Object, source: any) => {
114114
if (data && data.codesandbox) {
115115
if (data.type === 'initialized' && source) {
116-
registerFrame(source);
116+
registerFrame(source, frameUrl(this.props.sandbox.id));
117117

118118
if (!this.state.frameInitialized && this.props.onInitialized) {
119119
this.disposeInitializer = this.props.onInitialized(this);

packages/codesandbox-api/src/dispatcher/index.ts

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,52 @@
11
// import * as debug from 'debug';
22
import host from './host';
33

4-
const bundlers: Window[] = [];
4+
const bundlers: Map<Window, string> = new Map();
55

66
// Whether the tab has a connection with the editor
77
export const isStandalone =
88
typeof window === 'undefined' || (!window.opener && window.parent === window);
99

10+
let parentOrigin: string | null = null;
11+
12+
const parentOriginListener = (e: MessageEvent) => {
13+
if (e.data.type === 'register-frame') {
14+
parentOrigin = e.data.origin;
15+
16+
self.removeEventListener('message', parentOriginListener);
17+
}
18+
};
19+
20+
self.addEventListener('message', parentOriginListener);
21+
1022
/**
1123
* Send a message to the editor, this is most probably an action you generated
1224
*
1325
* @export
1426
* @param {*} message
1527
* @returns
1628
*/
17-
export function dispatch(message: Object) {
29+
export function dispatch(message: any) {
1830
if (!message) return;
1931

2032
const newMessage = { ...message, codesandbox: true };
2133
notifyListeners(newMessage);
2234
notifyFrames(newMessage);
2335

2436
if (isStandalone) return;
37+
if (parentOrigin === null && message.type !== 'initialized') return;
2538

2639
if (window.opener) {
27-
window.opener.postMessage(newMessage, '*');
40+
window.opener.postMessage(newMessage, parentOrigin === null ? '*' : parentOrigin);
2841
} else {
29-
window.parent.postMessage(newMessage, '*');
42+
window.parent.postMessage(newMessage, parentOrigin === null ? '*' : parentOrigin);
3043
}
3144
}
3245

33-
export type Callback = (message: Object, source?: Window | null | undefined) => void;
46+
export type Callback = (
47+
message: Object,
48+
source?: MessageEvent['source'] | null | undefined
49+
) => void;
3450

3551
const listeners: { [id: string]: Callback } = {};
3652
let listenerId = 0;
@@ -58,17 +74,17 @@ export function notifyListeners(data: Object, source?: MessageEvent['source']) {
5874

5975
function notifyFrames(message: Object) {
6076
const rawMessage = JSON.parse(JSON.stringify(message));
61-
bundlers.forEach(frame => {
77+
bundlers.forEach((origin, frame) => {
6278
if (frame && frame.postMessage) {
63-
frame.postMessage({ ...rawMessage, codesandbox: true }, '*');
79+
frame.postMessage({ ...rawMessage, codesandbox: true }, origin);
6480
}
6581
});
6682
}
6783

6884
function eventListener(e: MessageEvent) {
6985
const { data } = e;
7086

71-
if (data && data.codesandbox) {
87+
if (data && data.codesandbox && (parentOrigin === null || e.origin === parentOrigin)) {
7288
notifyListeners(data, e.source);
7389
}
7490
}
@@ -78,9 +94,17 @@ function eventListener(e: MessageEvent) {
7894
*
7995
* @param frame
8096
*/
81-
export function registerFrame(frame: Window) {
82-
if (bundlers.indexOf(frame) === -1) {
83-
bundlers.push(frame);
97+
export function registerFrame(frame: Window, origin: string) {
98+
if (!bundlers.has(frame)) {
99+
bundlers.set(frame, origin);
100+
101+
frame.postMessage(
102+
{
103+
type: 'register-frame',
104+
origin: document.location.origin,
105+
},
106+
origin
107+
);
84108
}
85109
}
86110

0 commit comments

Comments
 (0)