-
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.
mini-browser, webview: warn if unsecure
Add a new `FrontendApplicationConfiguration` field `securityWarnings` that drives the binding of guards in different modules, as well as adding/removing preferences. When enabled, these modules will do checks for known configuration issues that may cause security vulnerabilities. When disabled, applications will run like they used to, skipping checks. Check for unsecure host patterns when deploying `mini-browser` and `webview` content. `{{hostname}}` is known to cause vulnerabilities in applications, so we currently check for those by default. New preferences: `mini-browser.previewFile.preventUnsecure: 'ask' | 'alwaysOpen' | 'alwaysPrevent'` Theia will prompt the user before loading the local content into the preview iframe. You can either open, prevent, always open, or always prevent. `mini-browser.warnIfUnsecure: boolean` Theia will prompt a warning upon starting the frontend if the configured host pattern is unsecure. `webview.warnIfUnsecure: boolean` Theia will prompt a warning upon starting the frontend if the configured host pattern is unsecure.
- Loading branch information
1 parent
2880525
commit a307f85
Showing
12 changed files
with
352 additions
and
22 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
23 changes: 23 additions & 0 deletions
23
packages/mini-browser/src/browser/mini-browser-configuration.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,23 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2021 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 | ||
********************************************************************************/ | ||
|
||
export const MiniBrowserConfiguration = Symbol('MiniBrowserConfiguration'); | ||
export interface MiniBrowserConfiguration { | ||
/** | ||
* The host pattern used to serve mini-browser content. | ||
*/ | ||
hostPattern?: string | ||
} |
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
113 changes: 113 additions & 0 deletions
113
packages/mini-browser/src/browser/mini-browser-guard.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,113 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2021 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 { MessageService } from '@theia/core'; | ||
import { PreferenceService, PreferenceScope } from '@theia/core/lib/browser'; | ||
import { inject, injectable } from '@theia/core/shared/inversify'; | ||
import { MiniBrowserPreferences, IMiniBrowserPreferences } from './mini-browser-preferences'; | ||
import { MiniBrowserConfiguration } from './mini-browser-configuration'; | ||
|
||
/** | ||
* Checks for known security issues with the mini-browser. | ||
* Can be controlled through preferences. | ||
*/ | ||
@injectable() | ||
export class MiniBrowserGuard { | ||
|
||
@inject(MessageService) | ||
protected messageService: MessageService; | ||
|
||
@inject(PreferenceService) | ||
protected preferenceService: PreferenceService; | ||
|
||
@inject(MiniBrowserConfiguration) | ||
protected miniBrowserConfiguration: MiniBrowserConfiguration; | ||
|
||
@inject(MiniBrowserPreferences) | ||
protected miniBrowserPreferences: MiniBrowserPreferences; | ||
|
||
async onSetHostPattern(hostPattern: string): Promise<void> { | ||
if (this.miniBrowserPreferences['mini-browser.warnIfUnsecure']) { | ||
if (this.isHostPatternUnsecure(hostPattern)) { | ||
this.messageService.warn( | ||
'`mini-browser` is currently configured to serve `file:` resources on the same origin as the application, this is known to be unsecure. ' + | ||
`Current pattern: \`${hostPattern}\``, | ||
{ timeout: 5000 }, | ||
/* actions: */ 'Ok', 'Don\'t show again', | ||
).then(action => { | ||
if (action === 'Don\'t show again') { | ||
this.setMiniBrowserPreference('mini-browser.warnIfUnsecure', false); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Will throw if the location should not be opened, according to the current configurations. | ||
*/ | ||
async onFileLocationMap(location: string): Promise<void> { | ||
if (this.isHostPatternUnsecure(this.miniBrowserConfiguration.hostPattern!)) { | ||
if (this.miniBrowserPreferences['mini-browser.previewFile.preventUnsecure'] === 'alwaysPrevent') { | ||
throw this.preventOpeningLocation(location); | ||
} | ||
if (this.miniBrowserPreferences['mini-browser.previewFile.preventUnsecure'] === 'ask') { | ||
await this.askOpenFileUnsecurely(location); | ||
} | ||
} | ||
} | ||
|
||
protected isHostPatternUnsecure(hostPattern: string): boolean { | ||
return hostPattern === '{{hostname}}'; | ||
} | ||
|
||
protected async askOpenFileUnsecurely(location: string): Promise<void> { | ||
const action = await this.messageService.warn( | ||
'You are about to open a local file with the same origin as this application, this unsecure and the displayed document might access this application services. ' + | ||
`File: \`${location}\``, | ||
/* actions: */ 'Open', 'Always Open', 'Prevent', 'Always Prevent' | ||
); | ||
switch (action) { | ||
case 'Always Prevent': | ||
this.setMiniBrowserPreference('mini-browser.previewFile.preventUnsecure', 'alwaysPrevent'); | ||
case 'Prevent': | ||
case undefined: | ||
throw this.preventOpeningLocation(location); | ||
case 'Always Open': | ||
this.setMiniBrowserPreference('mini-browser.previewFile.preventUnsecure', 'alwaysPrevent'); | ||
case 'Open': | ||
return; | ||
} | ||
} | ||
|
||
protected preventOpeningLocation(location: string): Error { | ||
const message = `Prevented opening ${location}.`; | ||
this.messageService.warn( | ||
`${message} See the \`mini-browser.previewFile.preventUnsecure\` preference to control this behavior.`, | ||
{ timeout: 10_000 }, | ||
/* actions: */ 'Ok' | ||
); | ||
return new Error(message); | ||
} | ||
|
||
protected setMiniBrowserPreference<K extends keyof IMiniBrowserPreferences>( | ||
preference: K, | ||
value: IMiniBrowserPreferences[K], | ||
scope: PreferenceScope = PreferenceScope.User | ||
): void { | ||
this.preferenceService.set(preference, value, scope); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
packages/mini-browser/src/browser/mini-browser-preferences.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,51 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2021 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 { PreferenceSchema, PreferenceProxy } from '@theia/core/lib/browser'; | ||
import { FrontendApplicationConfigProvider } from '@theia/core/lib/browser/frontend-application-config-provider'; | ||
|
||
const frontendConfig = FrontendApplicationConfigProvider.get(); | ||
|
||
export const MiniBrowserPreferencesSchema: PreferenceSchema = { | ||
properties: {} | ||
}; | ||
|
||
if (frontendConfig.securityWarnings) { | ||
MiniBrowserPreferencesSchema.properties['mini-browser.previewFile.preventUnsecure'] = { | ||
scope: 'application', | ||
description: 'What to do when you open a resource with the mini-browser in an unsecure manner.', | ||
enum: [ | ||
'ask', | ||
'alwaysOpen', | ||
'alwaysPrevent', | ||
], | ||
default: 'ask' | ||
}; | ||
MiniBrowserPreferencesSchema.properties['mini-browser.warnIfUnsecure'] = { | ||
scope: 'application', | ||
type: 'boolean', | ||
description: 'Warns users that the mini-browser is currently deployed unsecurely.', | ||
default: true, | ||
}; | ||
} | ||
|
||
export interface IMiniBrowserPreferences { | ||
'mini-browser.previewFile.preventUnsecure'?: 'ask' | 'alwaysOpen' | 'alwaysPrevent' | ||
'mini-browser.warnIfUnsecure'?: boolean | ||
} | ||
|
||
export const MiniBrowserPreferences = Symbol('GitPreferences'); | ||
export type MiniBrowserPreferences = PreferenceProxy<IMiniBrowserPreferences>; |
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
Oops, something went wrong.