From 2952342eb863b25501477a717028c5f98fc850ca Mon Sep 17 00:00:00 2001 From: Artem Zatsarynnyi Date: Sun, 29 Dec 2019 15:48:10 +0200 Subject: [PATCH 1/3] Use a dedicated server as webview domain Signed-off-by: Artem Zatsarynnyi --- .../src/browser/che-webview-environment.ts | 14 ++++---- .../src/common/che-server-common.ts | 1 + .../src/node/plugin-service.ts | 35 ++++++++++--------- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/extensions/eclipse-che-theia-plugin-ext/src/browser/che-webview-environment.ts b/extensions/eclipse-che-theia-plugin-ext/src/browser/che-webview-environment.ts index 211215bbe..9dad90fce 100644 --- a/extensions/eclipse-che-theia-plugin-ext/src/browser/che-webview-environment.ts +++ b/extensions/eclipse-che-theia-plugin-ext/src/browser/che-webview-environment.ts @@ -13,7 +13,7 @@ import { CheApiService } from '../common/che-protocol'; import { EnvVariablesServer } from '@theia/core/lib/common/env-variables'; import { WebviewEnvironment } from '@theia/plugin-ext/lib/main/browser/webview/webview-environment'; import { WebviewExternalEndpoint } from '@theia/plugin-ext/lib/main/common/webview-protocol'; -import { getUrlDomain, SERVER_TYPE_ATTR, SERVER_IDE_ATTR_VALUE } from '../common/che-server-common'; +import { getUrlDomain, SERVER_TYPE_ATTR, SERVER_WEBVIEWS_ATTR_VALUE } from '../common/che-server-common'; @injectable() export class CheWebviewEnvironment extends WebviewEnvironment { @@ -27,13 +27,13 @@ export class CheWebviewEnvironment extends WebviewEnvironment { @postConstruct() protected async init(): Promise { try { - const variable = await this.environments.getValue(WebviewExternalEndpoint.pattern); - const ideServer = await this.cheApi.findUniqueServerByAttribute(SERVER_TYPE_ATTR, SERVER_IDE_ATTR_VALUE); - let domain; - if (ideServer && ideServer.url) { - domain = getUrlDomain(ideServer.url); + const webviewExternalEndpointPattern = await this.environments.getValue(WebviewExternalEndpoint.pattern); + const webviewServer = await this.cheApi.findUniqueServerByAttribute(SERVER_TYPE_ATTR, SERVER_WEBVIEWS_ATTR_VALUE); + let webviewDomain: string | undefined; + if (webviewServer && webviewServer.url) { + webviewDomain = getUrlDomain(webviewServer.url); } - const hostName = variable && variable.value || domain || WebviewExternalEndpoint.pattern; + const hostName = webviewExternalEndpointPattern && webviewExternalEndpointPattern.value || webviewDomain || WebviewExternalEndpoint.pattern; this.externalEndpointHost.resolve(hostName.replace('{{hostname}}', window.location.host || 'localhost')); } catch (e) { this.externalEndpointHost.reject(e); diff --git a/extensions/eclipse-che-theia-plugin-ext/src/common/che-server-common.ts b/extensions/eclipse-che-theia-plugin-ext/src/common/che-server-common.ts index 3038e3860..3cc99ce58 100644 --- a/extensions/eclipse-che-theia-plugin-ext/src/common/che-server-common.ts +++ b/extensions/eclipse-che-theia-plugin-ext/src/common/che-server-common.ts @@ -21,4 +21,5 @@ export function getUrlDomain(routeUrl: string): string { export const SERVER_TYPE_ATTR = 'type'; export const SERVER_IDE_ATTR_VALUE = 'ide'; +export const SERVER_WEBVIEWS_ATTR_VALUE = 'webview'; export const SERVER_IDE_DEV_ATTR_VALUE = 'ide-dev'; diff --git a/extensions/eclipse-che-theia-plugin-ext/src/node/plugin-service.ts b/extensions/eclipse-che-theia-plugin-ext/src/node/plugin-service.ts index d1dc8c795..5c3570525 100644 --- a/extensions/eclipse-che-theia-plugin-ext/src/node/plugin-service.ts +++ b/extensions/eclipse-che-theia-plugin-ext/src/node/plugin-service.ts @@ -23,7 +23,7 @@ import { injectable, inject } from 'inversify'; import { WebviewExternalEndpoint } from '@theia/plugin-ext/lib/main/common/webview-protocol'; import { PluginApiContribution } from '@theia/plugin-ext/lib/main/node/plugin-service'; import { CheApiService } from '../common/che-protocol'; -import { getUrlDomain, SERVER_TYPE_ATTR, SERVER_IDE_ATTR_VALUE } from '../common/che-server-common'; +import { getUrlDomain, SERVER_TYPE_ATTR, SERVER_WEBVIEWS_ATTR_VALUE } from '../common/che-server-common'; import { Deferred } from '@theia/core/lib/common/promise-util'; const pluginPath = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE) + './theia/plugins/'; @@ -46,22 +46,23 @@ export class PluginApiContributionIntercepted extends PluginApiContribution { const pluginExtModulePath = path.dirname(require.resolve('@theia/plugin-ext/package.json')); const webviewStaticResources = path.join(pluginExtModulePath, 'src/main/browser/webview/pre'); - this.cheApi.findUniqueServerByAttribute(SERVER_TYPE_ATTR, SERVER_IDE_ATTR_VALUE).then(server => { - let domain; - if (server.url) { - domain = getUrlDomain(server.url); - } - const hostName = this.handleAliases(process.env[WebviewExternalEndpoint.pattern] || domain || WebviewExternalEndpoint.pattern); - webviewApp.use('/webview', serveStatic(webviewStaticResources)); - - console.log(`Configuring to accept webviews on '${hostName}' hostname.`); - app.use(vhost(new RegExp(hostName, 'i'), webviewApp)); - - this.waitWebviewEndpoint.resolve(); - }).catch(err => { - console.log('Unable to configure webview domain: ', err); - this.waitWebviewEndpoint.resolve(); - }); + this.cheApi.findUniqueServerByAttribute(SERVER_TYPE_ATTR, SERVER_WEBVIEWS_ATTR_VALUE).then(server => { + let domain; + if (server.url) { + domain = getUrlDomain(server.url); + } + const hostName = this.handleAliases(process.env[WebviewExternalEndpoint.pattern] || domain || WebviewExternalEndpoint.pattern); + webviewApp.use('/webview', serveStatic(webviewStaticResources)); + + console.log(`Configuring to accept webviews on '${hostName}' hostname.`); + app.use(vhost(new RegExp(hostName, 'i'), webviewApp)); + + this.waitWebviewEndpoint.resolve(); + }) + .catch(err => { + console.error('Security problem: Unable to configure separate webviews domain: ', err); + this.waitWebviewEndpoint.resolve(); + }); } async onStart(): Promise { From 76d16e9146392d1147adda2a220cdbc4a22ddf32 Mon Sep 17 00:00:00 2001 From: Artem Zatsarynnyi Date: Tue, 4 Feb 2020 17:37:34 +0200 Subject: [PATCH 2/3] Fix Vlad's notes Signed-off-by: Artem Zatsarynnyi --- .../src/node/plugin-service.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/extensions/eclipse-che-theia-plugin-ext/src/node/plugin-service.ts b/extensions/eclipse-che-theia-plugin-ext/src/node/plugin-service.ts index 5c3570525..a1432f2de 100644 --- a/extensions/eclipse-che-theia-plugin-ext/src/node/plugin-service.ts +++ b/extensions/eclipse-che-theia-plugin-ext/src/node/plugin-service.ts @@ -25,6 +25,7 @@ import { PluginApiContribution } from '@theia/plugin-ext/lib/main/node/plugin-se import { CheApiService } from '../common/che-protocol'; import { getUrlDomain, SERVER_TYPE_ATTR, SERVER_WEBVIEWS_ATTR_VALUE } from '../common/che-server-common'; import { Deferred } from '@theia/core/lib/common/promise-util'; +import { ILogger } from '@theia/core/lib/common/logger'; const pluginPath = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE) + './theia/plugins/'; @@ -34,6 +35,9 @@ export class PluginApiContributionIntercepted extends PluginApiContribution { @inject(CheApiService) private cheApi: CheApiService; + @inject(ILogger) + protected readonly logger: ILogger; + private waitWebviewEndpoint = new Deferred(); configure(app: express.Application): void { @@ -53,10 +57,10 @@ export class PluginApiContributionIntercepted extends PluginApiContribution { } const hostName = this.handleAliases(process.env[WebviewExternalEndpoint.pattern] || domain || WebviewExternalEndpoint.pattern); webviewApp.use('/webview', serveStatic(webviewStaticResources)); - - console.log(`Configuring to accept webviews on '${hostName}' hostname.`); + + this.logger.info(`Configuring to accept webviews on '${hostName}' hostname.`); app.use(vhost(new RegExp(hostName, 'i'), webviewApp)); - + this.waitWebviewEndpoint.resolve(); }) .catch(err => { From e7ed8b594653a50960c84cad185767b6752be177 Mon Sep 17 00:00:00 2001 From: Artem Zatsarynnyi Date: Tue, 4 Feb 2020 17:51:26 +0200 Subject: [PATCH 3/3] Fix Vlad's notes Signed-off-by: Artem Zatsarynnyi --- .../eclipse-che-theia-plugin-ext/src/node/plugin-service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/eclipse-che-theia-plugin-ext/src/node/plugin-service.ts b/extensions/eclipse-che-theia-plugin-ext/src/node/plugin-service.ts index a1432f2de..d629b944f 100644 --- a/extensions/eclipse-che-theia-plugin-ext/src/node/plugin-service.ts +++ b/extensions/eclipse-che-theia-plugin-ext/src/node/plugin-service.ts @@ -64,7 +64,7 @@ export class PluginApiContributionIntercepted extends PluginApiContribution { this.waitWebviewEndpoint.resolve(); }) .catch(err => { - console.error('Security problem: Unable to configure separate webviews domain: ', err); + this.logger.error('Security problem: Unable to configure separate webviews domain: ', err); this.waitWebviewEndpoint.resolve(); }); }