|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (C) 2021 Ericsson and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the Eclipse |
| 10 | + * Public License v. 2.0 are satisfied: GNU General Public License, version 2 |
| 11 | + * with the GNU Classpath Exception which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + ********************************************************************************/ |
| 16 | + |
| 17 | +import { MessageService } from '@theia/core'; |
| 18 | +import { PreferenceService, PreferenceScope } from '@theia/core/lib/browser'; |
| 19 | +import { inject, injectable } from '@theia/core/shared/inversify'; |
| 20 | +import { MiniBrowserPreferences, IMiniBrowserPreferences } from './mini-browser-preferences'; |
| 21 | +import { MiniBrowserConfiguration } from './mini-browser-configuration'; |
| 22 | + |
| 23 | +/** |
| 24 | + * Checks for known security issues with the mini-browser. |
| 25 | + * Can be controlled through preferences. |
| 26 | + */ |
| 27 | +@injectable() |
| 28 | +export class MiniBrowserGuard { |
| 29 | + |
| 30 | + @inject(MessageService) |
| 31 | + protected messageService: MessageService; |
| 32 | + |
| 33 | + @inject(PreferenceService) |
| 34 | + protected preferenceService: PreferenceService; |
| 35 | + |
| 36 | + @inject(MiniBrowserConfiguration) |
| 37 | + protected miniBrowserConfiguration: MiniBrowserConfiguration; |
| 38 | + |
| 39 | + @inject(MiniBrowserPreferences) |
| 40 | + protected miniBrowserPreferences: MiniBrowserPreferences; |
| 41 | + |
| 42 | + async onSetHostPattern(hostPattern: string): Promise<void> { |
| 43 | + if (this.miniBrowserPreferences['mini-browser.warnIfUnsecure']) { |
| 44 | + if (this.isHostPatternUnsecure(hostPattern)) { |
| 45 | + this.messageService.warn( |
| 46 | + '`mini-browser` is currently configured to serve `file:` resources on the same origin as the application, this is known to be unsecure. ' + |
| 47 | + `Current pattern: \`${hostPattern}\``, |
| 48 | + { timeout: 5000 }, |
| 49 | + /* actions: */ 'Ok', 'Don\'t show again', |
| 50 | + ).then(action => { |
| 51 | + if (action === 'Don\'t show again') { |
| 52 | + this.setMiniBrowserPreference('mini-browser.warnIfUnsecure', false); |
| 53 | + } |
| 54 | + }); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Will throw if the location should not be opened, according to the current configurations. |
| 61 | + */ |
| 62 | + async onFileLocationMap(location: string): Promise<void> { |
| 63 | + if (this.isHostPatternUnsecure(this.miniBrowserConfiguration.hostPattern!)) { |
| 64 | + if (this.miniBrowserPreferences['mini-browser.previewFile.preventUnsecure'] === 'alwaysPrevent') { |
| 65 | + throw this.preventOpeningLocation(location); |
| 66 | + } |
| 67 | + if (this.miniBrowserPreferences['mini-browser.previewFile.preventUnsecure'] === 'ask') { |
| 68 | + await this.askOpenFileUnsecurely(location); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + protected isHostPatternUnsecure(hostPattern: string): boolean { |
| 74 | + return hostPattern === '{{hostname}}'; |
| 75 | + } |
| 76 | + |
| 77 | + protected async askOpenFileUnsecurely(location: string): Promise<void> { |
| 78 | + const action = await this.messageService.warn( |
| 79 | + '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. ' + |
| 80 | + `File: \`${location}\``, |
| 81 | + /* actions: */ 'Open', 'Always Open', 'Prevent', 'Always Prevent' |
| 82 | + ); |
| 83 | + switch (action) { |
| 84 | + case 'Always Prevent': |
| 85 | + this.setMiniBrowserPreference('mini-browser.previewFile.preventUnsecure', 'alwaysPrevent'); |
| 86 | + case 'Prevent': |
| 87 | + case undefined: |
| 88 | + throw this.preventOpeningLocation(location); |
| 89 | + case 'Always Open': |
| 90 | + this.setMiniBrowserPreference('mini-browser.previewFile.preventUnsecure', 'alwaysPrevent'); |
| 91 | + case 'Open': |
| 92 | + return; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + protected preventOpeningLocation(location: string): Error { |
| 97 | + const message = `Prevented opening ${location}.`; |
| 98 | + this.messageService.warn( |
| 99 | + `${message} See the \`mini-browser.previewFile.preventUnsecure\` preference to control this behavior.`, |
| 100 | + { timeout: 10_000 }, |
| 101 | + /* actions: */ 'Ok' |
| 102 | + ); |
| 103 | + return new Error(message); |
| 104 | + } |
| 105 | + |
| 106 | + protected setMiniBrowserPreference<K extends keyof IMiniBrowserPreferences>( |
| 107 | + preference: K, |
| 108 | + value: IMiniBrowserPreferences[K], |
| 109 | + scope: PreferenceScope = PreferenceScope.User |
| 110 | + ): void { |
| 111 | + this.preferenceService.set(preference, value, scope); |
| 112 | + } |
| 113 | +} |
0 commit comments