Skip to content

Commit

Permalink
safeguard init exthost message (#142486)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomoreno committed Feb 8, 2022
1 parent 050f4a0 commit 194935b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class WebWorkerExtensionHost extends Disposable implements IExtensionHost

// Send over message ports for extension API
const messagePorts = this._environmentService.options?.messagePorts ?? new Map();
iframe.contentWindow!.postMessage(messagePorts, '*', [...messagePorts.values()]);
iframe.contentWindow!.postMessage({ type: 'vscode.init', data: messagePorts }, '*', [...messagePorts.values()]);

port.onmessage = (event) => {
const { data } = event;
Expand Down
17 changes: 15 additions & 2 deletions src/vs/workbench/services/extensions/worker/extensionHostWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,20 +216,33 @@ function connectToRenderer(protocol: IMessagePassingProtocol): Promise<IRenderer

let onTerminate = (reason: string) => nativeClose();

interface IInitMessage {
readonly type: 'vscode.init';
readonly data: ReadonlyMap<string, MessagePort>;
}

function isInitMessage(a: any): a is IInitMessage {
return !!a && typeof a === 'object' && a.type === 'vscode.init' && a.data instanceof Map;
}

export function create(): { onmessage: (message: any) => void } {
performance.mark(`code/extHost/willConnectToRenderer`);
const res = new ExtensionWorker();

return {
onmessage(messagePorts: ReadonlyMap<string, MessagePort>) {
onmessage(message: any) {
if (!isInitMessage(message)) {
return; // silently ignore foreign messages
}

connectToRenderer(res.protocol).then(data => {
performance.mark(`code/extHost/didWaitForInitData`);
const extHostMain = new ExtensionHostMain(
data.protocol,
data.initData,
hostUtil,
null,
messagePorts
message.data
);

onTerminate = (reason: string) => extHostMain.terminate(reason);
Expand Down

0 comments on commit 194935b

Please sign in to comment.