From 2e9ed4340092ab9a879d2e07b4ad8e52be31188f Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Tue, 8 Feb 2022 08:53:36 +0100 Subject: [PATCH] safeguard init exthost message --- .../workbench/api/worker/extensionHostWorker.ts | 17 +++++++++++++++-- .../browser/webWorkerExtensionHost.ts | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/vs/workbench/api/worker/extensionHostWorker.ts b/src/vs/workbench/api/worker/extensionHostWorker.ts index 676722cea9721..f439c2929ff5e 100644 --- a/src/vs/workbench/api/worker/extensionHostWorker.ts +++ b/src/vs/workbench/api/worker/extensionHostWorker.ts @@ -215,12 +215,25 @@ function connectToRenderer(protocol: IMessagePassingProtocol): Promise nativeClose(); +interface IInitMessage { + readonly type: 'vscode.init'; + readonly data: ReadonlyMap; +} + +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) { + 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( @@ -228,7 +241,7 @@ export function create(): { onmessage: (message: any) => void } { data.initData, hostUtil, null, - messagePorts + message.data ); onTerminate = (reason: string) => extHostMain.terminate(reason); diff --git a/src/vs/workbench/services/extensions/browser/webWorkerExtensionHost.ts b/src/vs/workbench/services/extensions/browser/webWorkerExtensionHost.ts index b33a75f0b0aaa..55ee8c884d5ec 100644 --- a/src/vs/workbench/services/extensions/browser/webWorkerExtensionHost.ts +++ b/src/vs/workbench/services/extensions/browser/webWorkerExtensionHost.ts @@ -188,7 +188,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;