Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: fix origin checks problems #8865

Merged
merged 1 commit into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
],
"env": {
"NODE_ENV": "development",
"THEIA_WEBVIEW_EXTERNAL_ENDPOINT": "${env:THEIA_WEBVIEW_EXTERNAL_ENDPOINT}"
"THEIA_WEBVIEW_EXTERNAL_ENDPOINT": "${env:THEIA_WEBVIEW_EXTERNAL_ENDPOINT}",
"THEIA_MINI_BROWSER_HOST_PATTERN": "${env:THEIA_MINI_BROWSER_HOST_PATTERN}"
},
"sourceMaps": true,
"outFiles": [
Expand Down Expand Up @@ -110,7 +111,8 @@
],
"env": {
"NODE_ENV": "development",
"THEIA_WEBVIEW_EXTERNAL_ENDPOINT": "${env:THEIA_WEBVIEW_EXTERNAL_ENDPOINT}"
"THEIA_WEBVIEW_EXTERNAL_ENDPOINT": "${env:THEIA_WEBVIEW_EXTERNAL_ENDPOINT}",
"THEIA_MINI_BROWSER_HOST_PATTERN": "${env:THEIA_MINI_BROWSER_HOST_PATTERN}"
},
"sourceMaps": true,
"outFiles": [
Expand Down Expand Up @@ -173,7 +175,8 @@
],
"env": {
"THEIA_DEFAULT_PLUGINS": "local-dir:${workspaceFolder}/plugins",
"THEIA_WEBVIEW_EXTERNAL_ENDPOINT": "${env:THEIA_WEBVIEW_EXTERNAL_ENDPOINT}"
"THEIA_WEBVIEW_EXTERNAL_ENDPOINT": "${env:THEIA_WEBVIEW_EXTERNAL_ENDPOINT}",
"THEIA_MINI_BROWSER_HOST_PATTERN": "${env:THEIA_MINI_BROWSER_HOST_PATTERN}"
},
"stopOnEntry": false,
"sourceMaps": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class MiniBrowserEnvironment implements FrontendApplicationContribution {

getEndpoint(uuid: string, hostname?: string): Endpoint {
return new Endpoint({
path: MiniBrowserEndpoint.PATH,
host: this._hostPattern
.replace('{{uuid}}', uuid)
.replace('{{hostname}}', hostname || this.getDefaultHostname()),
Expand Down
1 change: 1 addition & 0 deletions packages/mini-browser/src/common/mini-browser-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* will be replace by a random uuid value.
*/
export namespace MiniBrowserEndpoint {
export const PATH = '/mini-browser';
export const HOST_PATTERN_ENV = 'THEIA_MINI_BROWSER_HOST_PATTERN';
export const HOST_PATTERN_DEFAULT = '{{uuid}}.mini-browser.{{hostname}}';
}
2 changes: 1 addition & 1 deletion packages/mini-browser/src/node/mini-browser-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class MiniBrowserEndpoint implements BackendApplicationContribution, Mini
protected async attachRequestHandler(app: Application): Promise<void> {
const miniBrowserApp = express();
miniBrowserApp.get('*', async (request, response) => this.response(await this.getUri(request), response));
app.use(vhost(await this.getVirtualHostRegExp(), miniBrowserApp));
app.use(MiniBrowserEndpointNS.PATH, vhost(await this.getVirtualHostRegExp(), miniBrowserApp));
}

protected async response(uri: string, response: Response): Promise<Response> {
Expand Down
9 changes: 7 additions & 2 deletions packages/mini-browser/src/node/mini-browser-ws-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,23 @@ export class MiniBrowserWsRequestValidator implements WsRequestValidatorContribu

protected miniBrowserHostRe: RegExp;

protected serveSameOrigin: boolean = false;

@postConstruct()
protected postConstruct(): void {
const pattern = process.env[MiniBrowserEndpoint.HOST_PATTERN_ENV] || MiniBrowserEndpoint.HOST_PATTERN_DEFAULT;
if (pattern === '{{hostname}}') {
this.serveSameOrigin = true;
}
const vhostRe = pattern
.replace('.', '\\.')
.replace(/\./g, '\\.')
.replace('{{uuid}}', '.+')
.replace('{{hostname}}', '.+');
this.miniBrowserHostRe = new RegExp(vhostRe, 'i');
}

async allowWsUpgrade(request: http.IncomingMessage): Promise<boolean> {
if (request.headers.origin) {
if (request.headers.origin && !this.serveSameOrigin) {
const origin = url.parse(request.headers.origin);
if (origin.host && this.miniBrowserHostRe.test(origin.host)) {
// If the origin comes from the WebViews, refuse:
Expand Down
22 changes: 16 additions & 6 deletions packages/plugin-ext/src/main/node/plugin-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export class PluginApiContribution implements BackendApplicationContribution, Ws

protected webviewExternalEndpointRegExp: RegExp;

protected serveSameOrigin: boolean = false;

@postConstruct()
protected postConstruct(): void {
const webviewExternalEndpoint = this.webviewExternalEndpoint();
Expand All @@ -45,7 +47,7 @@ export class PluginApiContribution implements BackendApplicationContribution, Ws
}

allowWsUpgrade(request: http.IncomingMessage): MaybePromise<boolean> {
if (request.headers.origin) {
if (request.headers.origin && !this.serveSameOrigin) {
const origin = url.parse(request.headers.origin);
if (origin.host && this.webviewExternalEndpointRegExp.test(origin.host)) {
// If the origin comes from the WebViews, refuse:
Expand All @@ -55,17 +57,25 @@ export class PluginApiContribution implements BackendApplicationContribution, Ws
return true;
}

/**
* Returns a RegExp pattern matching the expected WebView endpoint's host.
*/
protected webviewExternalEndpoint(): string {
protected webviewExternalEndpointPattern(): string {
let endpointPattern;
if (environment.electron.is()) {
endpointPattern = WebviewExternalEndpoint.defaultPattern;
} else {
endpointPattern = process.env[WebviewExternalEndpoint.pattern] || WebviewExternalEndpoint.defaultPattern;
}
return `^${endpointPattern
if (endpointPattern === '{{hostname}}') {
this.serveSameOrigin = true;
}
return endpointPattern;
}

/**
* Returns a RegExp pattern matching the expected WebView endpoint's host.
*/
protected webviewExternalEndpoint(): string {
return `^${this.webviewExternalEndpointPattern()
.replace(/\./g, '\\.')
.replace('{{uuid}}', '.+')
.replace('{{hostname}}', '.+')}$`;
}
Expand Down