-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core,mini-browser: server enhancements
# mini-browser: serve on separate origin The mini-browser currently hosts its services on the same origin than Theia's main origin. This commit makes the `mini-browser` serve on its own origin: `mini-browser.{{hostname}}` by default. Can be configured with a `THEIA_MINI_BROWSER_HOST_PATTERN` environment variable. # core: validate ws upgrade origin Hosting the `mini-browser` on its own origin prevents cross-origin requests from happening easily, but WebSockets don't benefit from the same protection. We need to allow/disallow upgrade requests in the backend application ourselves. This means that in order to know who to refuse, we need to know where we are hosted. This is done by specifying the `THEIA_HOSTS` environment variable. If left empty, no check will be done. But if set, nothing besides what is written in this var will be allowed. See `BackendApplicationHosts` to get the hosts extracted from this var. Note that the latter is not set when running Electron, since there's no need to deal with arbitrary origins, everything should be local. No check is done on the HTTP(s) endpoints because we'll rely on the browser's SOP and CORS policies to take effect. A new contribution point is added: `WsRequestValidatorContribution`. An extender can implement this contribution to allow or not WebSocket connections. Internally used to filter origins and Electron's token as well as checking the origin of requests to prevent some type of XSS. Signed-off-by: Paul Maréchal <paul.marechal@ericsson.com>
- Loading branch information
1 parent
e209832
commit 2a9e3d4
Showing
32 changed files
with
614 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/core/src/electron-main/electron-security-token-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/******************************************************************************** | ||
* 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 { session } from 'electron'; | ||
import { inject, injectable } from 'inversify'; | ||
import { ElectronSecurityToken } from '../electron-common/electron-token'; | ||
|
||
@injectable() | ||
export class ElectronSecurityTokenService { | ||
|
||
@inject(ElectronSecurityToken) | ||
protected readonly electronSecurityToken: ElectronSecurityToken; | ||
|
||
async setElectronSecurityTokenCookie(url: string): Promise<void> { | ||
await session.defaultSession.cookies.set({ | ||
url, | ||
name: ElectronSecurityToken, | ||
value: JSON.stringify(this.electronSecurityToken), | ||
httpOnly: true | ||
}); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/core/src/electron-node/hosting/electron-backend-hosting-module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { WsRequestValidatorContribution } from '../../node/ws-request-validators'; | ||
import { ElectronWsOriginValidator } from './electron-ws-origin-validator'; | ||
|
||
export default new ContainerModule(bind => { | ||
bind(ElectronWsOriginValidator).toSelf().inSingletonScope(); | ||
bind(WsRequestValidatorContribution).toService(ElectronWsOriginValidator); | ||
}); |
29 changes: 29 additions & 0 deletions
29
packages/core/src/electron-node/hosting/electron-ws-origin-validator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/******************************************************************************** | ||
* 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 http from 'http'; | ||
import { injectable } from 'inversify'; | ||
import { WsRequestValidatorContribution } from '../../node/ws-request-validators'; | ||
|
||
@injectable() | ||
export class ElectronWsOriginValidator implements WsRequestValidatorContribution { | ||
|
||
allowWsUpgrade(request: http.IncomingMessage): boolean { | ||
// On Electron the main page is served from the `file` protocol. | ||
// We don't expect the requests to come from anywhere else. | ||
return request.headers.origin === 'file://'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/core/src/node/hosting/backend-application-hosts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/******************************************************************************** | ||
* 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 { injectable, postConstruct } from 'inversify'; | ||
|
||
/** | ||
* **Important: This component is not bound on Electron.** | ||
* | ||
* Component handling the different hosts the Theia backend should be reachable at. | ||
* | ||
* Hosts should be set through the `THEIA_HOSTS` environment variable as a comma-separated list of hosts. | ||
* | ||
* If you do not set this variable, we'll consider that we don't know where the application is hosted at. | ||
*/ | ||
@injectable() | ||
export class BackendApplicationHosts { | ||
|
||
protected readonly _hosts = new Set<string>(); | ||
/** | ||
* Set of domains that the application is supposed to be reachable at. | ||
* If the set is empty it means that we don't know where we are hosted. | ||
* You can check for this with `.areHostsKnown()`. | ||
*/ | ||
get hosts(): ReadonlySet<string> { | ||
return this._hosts; | ||
} | ||
|
||
@postConstruct() | ||
protected postConstruct(): void { | ||
const theiaHostsEnv = process.env['THEIA_HOSTS']; | ||
if (theiaHostsEnv) { | ||
theiaHostsEnv.split(',').forEach(host => { | ||
const trimmed = host.trim(); | ||
if (trimmed.length > 0) { | ||
this._hosts.add(trimmed); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
areHostsKnown(): boolean { | ||
return this._hosts.size > 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/******************************************************************************** | ||
* 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 { WsRequestValidatorContribution } from '../ws-request-validators'; | ||
import { BackendApplicationHosts } from './backend-application-hosts'; | ||
import { WsOriginValidator } from './ws-origin-validator'; | ||
|
||
export default new ContainerModule(bind => { | ||
bind(BackendApplicationHosts).toSelf().inSingletonScope(); | ||
bind(WsOriginValidator).toSelf().inSingletonScope(); | ||
bind(WsRequestValidatorContribution).toService(WsOriginValidator); | ||
}); |
Oops, something went wrong.