Skip to content

Commit

Permalink
plugin-ext: attach security cookie to webviews
Browse files Browse the repository at this point in the history
When on Electron, we use a cookie to tell if an http client can use the
backend services. Cookies are bound to domains to which requests will be
made, and also subdomains in certain situations. Because we are using
`localhost` the cookie cannot be shared across subdomains. This commit
works around this limitation by passing the security token from
electron-main process to the renderer-process via a global variable,
and the token is then set inside a cookie for each new webview endpoint
created by the plugin system in the frontend.

Signed-off-by: Paul Maréchal <[email protected]>
  • Loading branch information
paul-marechal committed May 25, 2020
1 parent 46255a2 commit 4dad2c2
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ const electronSecurityToken = {
value: v4(),
};
// Make it easy for renderer process to fetch the ElectronSecurityToken:
global[ElectronSecurityToken] = electronSecurityToken;
app.on('ready', () => {
// Explicitly set the app name to have better menu items on macOS. ("About", "Hide", and "Quit")
Expand Down Expand Up @@ -322,19 +325,26 @@ app.on('ready', () => {
})
}
const loadMainWindow = (port) => {
if (!mainWindow.isDestroyed()) {
mainWindow.webContents.session.cookies.set({
const setElectronSecurityToken = port => {
return new Promise((resolve, reject) => {
electron.session.defaultSession.cookies.set({
url: \`http://localhost:\${port}/\`,
name: ElectronSecurityToken,
value: JSON.stringify(electronSecurityToken),
httpOnly: true,
}, error => {
if (error) {
console.error(error);
reject(error);
} else {
mainWindow.loadURL('file://' + join(__dirname, '../../lib/index.html') + '?port=' + port);
resolve();
}
});
})
}
const loadMainWindow = port => {
if (!mainWindow.isDestroyed()) {
mainWindow.loadURL('file://' + join(__dirname, '../../lib/index.html') + '?port=' + port);
}
};
Expand All @@ -354,7 +364,8 @@ app.on('ready', () => {
// But when in debugging we want to run everything in the same process to make things easier.
if (noBackendFork) {
process.env[ElectronSecurityToken] = JSON.stringify(electronSecurityToken);
require(mainPath).then(address => {
require(mainPath).then(async (address) => {
await setElectronSecurityToken(address.port);
loadMainWindow(address.port);
}).catch((error) => {
console.error(error);
Expand All @@ -367,7 +378,8 @@ app.on('ready', () => {
const cp = fork(mainPath, process.argv.slice(devMode ? 2 : 1), { env: Object.assign({
[ElectronSecurityToken]: JSON.stringify(electronSecurityToken),
}, process.env) });
cp.on('message', (address) => {
cp.on('message', async (address) => {
await setElectronSecurityToken(address.port);
loadMainWindow(address.port);
});
cp.on('error', (error) => {
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"backendElectron": "lib/electron-node/keyboard/electron-backend-keyboard-module"
},
{
"frontendElectron": "lib/electron-browser/token/electron-token-frontend-module",
"backendElectron": "lib/electron-node/token/electron-token-backend-module"
}
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/********************************************************************************
* Copyright (C) 2020 Ericsson and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import * as electron from 'electron';
import { ContainerModule } from 'inversify';
import { ElectronSecurityToken } from '../../electron-common/electron-token';

export default new ContainerModule(bind => {
bind(ElectronSecurityToken).toConstantValue(electron.remote.getGlobal(ElectronSecurityToken));
});
5 changes: 3 additions & 2 deletions packages/plugin-ext/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
{
"backend": "lib/plugin-ext-backend-module",
"backendElectron": "lib/plugin-ext-backend-electron-module",
"frontend": "lib/plugin-ext-frontend-module"
"frontend": "lib/plugin-ext-frontend-module",
"frontendElectron": "lib/plugin-ext-frontend-electron-module"
}
],
"keywords": [
Expand Down Expand Up @@ -87,4 +88,4 @@
"nyc": {
"extends": "../../configs/nyc.json"
}
}
}
31 changes: 12 additions & 19 deletions packages/plugin-ext/src/main/browser/plugin-ext-frontend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import '../../../src/main/style/status-bar.css';
import '../../../src/main/browser/style/index.css';

import { ContainerModule } from 'inversify';
import { interfaces, ContainerModule } from 'inversify';
import {
FrontendApplicationContribution, WidgetFactory, bindViewContribution,
ViewContainerIdentifier, ViewContainer, createTreeContainer, TreeImpl, TreeWidget, TreeModelImpl, LabelProviderContribution
Expand Down Expand Up @@ -62,16 +62,17 @@ import { LanguagesMainFactory, OutputChannelRegistryFactory } from '../../common
import { LanguagesMainImpl } from './languages-main';
import { OutputChannelRegistryMainImpl } from './output-channel-registry-main';
import { InPluginFileSystemWatcherManager } from './in-plugin-filesystem-watcher-manager';
import { WebviewWidget, WebviewWidgetIdentifier, WebviewWidgetExternalEndpoint } from './webview/webview';
import { WebviewWidget } from './webview/webview';
import { WebviewEnvironment } from './webview/webview-environment';
import { WebviewThemeDataProvider } from './webview/webview-theme-data-provider';
import { bindWebviewPreferences } from './webview/webview-preferences';
import { WebviewResourceLoader, WebviewResourceLoaderPath } from '../common/webview-protocol';
import { WebviewResourceCache } from './webview/webview-resource-cache';
import { PluginIconThemeService, PluginIconThemeFactory, PluginIconThemeDefinition, PluginIconTheme } from './plugin-icon-theme-service';
import { PluginTreeViewNodeLabelProvider } from './view/plugin-tree-view-node-label-provider';
import { WebviewWidgetFactory } from './webview/webview-widget-factory';

export default new ContainerModule((bind, unbind, isBound, rebind) => {
export const bindMainFrontend: interfaces.ContainerModuleCallBack = (bind, unbind, isBound, rebind) => {

bind(LanguagesMainImpl).toSelf().inTransientScope();
bind(LanguagesMainFactory).toFactory(context => (rpc: RPCProtocol) => {
Expand Down Expand Up @@ -162,21 +163,8 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
).inSingletonScope();
bind(WebviewResourceCache).toSelf().inSingletonScope();
bind(WebviewWidget).toSelf();
bind(WidgetFactory).toDynamicValue(({ container }) => ({
id: WebviewWidget.FACTORY_ID,
createWidget: async (identifier: WebviewWidgetIdentifier) => {
const externalEndpoint = await container.get(WebviewEnvironment).externalEndpoint();
let endpoint = externalEndpoint.replace('{{uuid}}', identifier.id);
if (endpoint[endpoint.length - 1] === '/') {
endpoint = endpoint.slice(0, endpoint.length - 1);
}

const child = container.createChild();
child.bind(WebviewWidgetIdentifier).toConstantValue(identifier);
child.bind(WebviewWidgetExternalEndpoint).toConstantValue(endpoint);
return child.get(WebviewWidget);
}
})).inSingletonScope();
bind(WebviewWidgetFactory).toDynamicValue(ctx => new WebviewWidgetFactory(ctx.container)).inSingletonScope();
bind(WidgetFactory).toService(WebviewWidgetFactory);

bind(PluginViewWidget).toSelf();
bind(WidgetFactory).toDynamicValue(({ container }) => ({
Expand Down Expand Up @@ -228,4 +216,9 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
rebind(DebugSessionContributionRegistry).toService(PluginDebugSessionContributionRegistry);

bind(ViewColumnService).toSelf().inSingletonScope();
});
};

/**
* @deprecated
*/
export default new ContainerModule(bindMainFrontend);
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/********************************************************************************
* Copyright (C) 2020 Ericsson and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { interfaces } from 'inversify';
import { WebviewWidget, WebviewWidgetIdentifier, WebviewWidgetExternalEndpoint } from './webview';
import { WebviewEnvironment } from './webview-environment';

export class WebviewWidgetFactory {

readonly id = WebviewWidget.FACTORY_ID;

protected readonly container: interfaces.Container;

constructor(container: interfaces.Container) {
this.container = container;
}

async createWidget(identifier: WebviewWidgetIdentifier): Promise<WebviewWidget> {
const externalEndpoint = await this.container.get(WebviewEnvironment).externalEndpoint();
let endpoint = externalEndpoint.replace('{{uuid}}', identifier.id);
if (endpoint[endpoint.length - 1] === '/') {
endpoint = endpoint.slice(0, endpoint.length - 1);
}
const child = this.container.createChild();
child.bind(WebviewWidgetIdentifier).toConstantValue(identifier);
child.bind(WebviewWidgetExternalEndpoint).toConstantValue(endpoint);
return child.get(WebviewWidget);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/********************************************************************************
* Copyright (C) 2020 Ericsson and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { interfaces } from 'inversify';
import { WebviewWidgetFactory } from '../browser/webview/webview-widget-factory';
import { ElectronWebviewWidgetFactory } from './webview/electron-webview-widget-factory';

export const bindElectronFrontend: interfaces.ContainerModuleCallBack = (bind, unbind, isBound, rebind) => {
rebind(WebviewWidgetFactory).toDynamicValue(ctx => new ElectronWebviewWidgetFactory(ctx.container)).inSingletonScope();
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/********************************************************************************
* Copyright (C) 2020 Ericsson and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { remote } from 'electron';
import { ElectronSecurityToken } from '@theia/core/lib/electron-common/electron-token';
import { WebviewWidgetFactory } from '../../browser/webview/webview-widget-factory';
import { WebviewWidgetIdentifier, WebviewWidget } from '../../browser/webview/webview';

export class ElectronWebviewWidgetFactory extends WebviewWidgetFactory {

async createWidget(identifier: WebviewWidgetIdentifier): Promise<WebviewWidget> {
const widget = await super.createWidget(identifier);
this.attachElectronSecurityCookie(widget.externalEndpoint);
return widget;
}

/**
* Attach the ElectronSecurityToken to a cookie that will be sent with each webview request.
*
* @param endpoint cookie's target url
*/
protected attachElectronSecurityCookie(endpoint: string): Promise<void> {
return new Promise((resolve, reject) => {
remote.session.defaultSession!.cookies.set({
url: endpoint,
name: ElectronSecurityToken,
value: JSON.stringify(this.container.get(ElectronSecurityToken)),
httpOnly: true,
}, error => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
}

}
24 changes: 24 additions & 0 deletions packages/plugin-ext/src/plugin-ext-frontend-electron-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/********************************************************************************
* Copyright (C) 2020 Ericsson and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { ContainerModule } from 'inversify';
import { bindMainFrontend } from './main/browser/plugin-ext-frontend-module';
import { bindElectronFrontend } from './main/electron-browser/plugin-ext-frontend-electron-module';

export default new ContainerModule((bind, unbind, isBound, rebind) => {
bindMainFrontend(bind, unbind, isBound, rebind);
bindElectronFrontend(bind, unbind, isBound, rebind);
});
7 changes: 5 additions & 2 deletions packages/plugin-ext/src/plugin-ext-frontend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import mainModule from './main/browser/plugin-ext-frontend-module';
import { ContainerModule } from 'inversify';
import { bindMainFrontend } from './main/browser/plugin-ext-frontend-module';

export default mainModule;
export default new ContainerModule((bind, unbind, isBound, rebind) => {
bindMainFrontend(bind, unbind, isBound, rebind);
});

0 comments on commit 4dad2c2

Please sign in to comment.