trustedDomainsValidator.ts 5.8 KB
Newer Older
P
Pine Wu 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { Schemas } from 'vs/base/common/network';
import Severity from 'vs/base/common/severity';
P
Pine Wu 已提交
8
import { equalsIgnoreCase, startsWith } from 'vs/base/common/strings';
P
Pine Wu 已提交
9 10 11 12
import { URI } from 'vs/base/common/uri';
import { localize } from 'vs/nls';
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { IOpenerService } from 'vs/platform/opener/common/opener';
13
import { IProductService } from 'vs/platform/product/common/productService';
P
Pine Wu 已提交
14
import { IQuickInputService } from 'vs/platform/quickinput/common/quickInput';
P
Pine Wu 已提交
15
import { IStorageService } from 'vs/platform/storage/common/storage';
P
Pine Wu 已提交
16
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
P
Pine Wu 已提交
17 18 19 20
import {
	configureOpenerTrustedDomainsHandler,
	readTrustedDomains
} from 'vs/workbench/contrib/url/common/trustedDomains';
P
Pine Wu 已提交
21
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
P
Pine Wu 已提交
22 23 24 25 26 27 28

export class OpenerValidatorContributions implements IWorkbenchContribution {
	constructor(
		@IOpenerService private readonly _openerService: IOpenerService,
		@IStorageService private readonly _storageService: IStorageService,
		@IDialogService private readonly _dialogService: IDialogService,
		@IProductService private readonly _productService: IProductService,
P
Pine Wu 已提交
29 30
		@IQuickInputService private readonly _quickInputService: IQuickInputService,
		@IEditorService private readonly _editorService: IEditorService
P
Pine Wu 已提交
31 32 33 34 35 36 37 38 39 40 41 42
	) {
		this._openerService.registerValidator({ shouldOpen: r => this.validateLink(r) });
	}

	async validateLink(resource: URI): Promise<boolean> {
		const { scheme, authority } = resource;

		if (!equalsIgnoreCase(scheme, Schemas.http) && !equalsIgnoreCase(scheme, Schemas.https)) {
			return true;
		}

		const domainToOpen = `${scheme}://${authority}`;
P
Pine Wu 已提交
43 44
		const { defaultTrustedDomains, trustedDomains } = readTrustedDomains(this._storageService, this._productService);
		const allTrustedDomains = [...defaultTrustedDomains, ...trustedDomains];
P
Pine Wu 已提交
45

P
Pine Wu 已提交
46
		if (isURLDomainTrusted(resource, allTrustedDomains)) {
P
Pine Wu 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
			return true;
		} else {
			const { choice } = await this._dialogService.show(
				Severity.Info,
				localize(
					'openExternalLinkAt',
					'Do you want {0} to open the external website?\n{1}',
					this._productService.nameShort,
					resource.toString(true)
				),
				[
					localize('openLink', 'Open Link'),
					localize('cancel', 'Cancel'),
					localize('configureTrustedDomains', 'Configure Trusted Domains')
				],
				{
					cancelId: 1
				}
			);

			// Open Link
			if (choice === 0) {
				return true;
			}
			// Configure Trusted Domains
			else if (choice === 2) {
				const pickedDomains = await configureOpenerTrustedDomainsHandler(
P
Pine Wu 已提交
74
					trustedDomains,
P
Pine Wu 已提交
75 76
					domainToOpen,
					this._quickInputService,
P
Pine Wu 已提交
77 78
					this._storageService,
					this._editorService
P
Pine Wu 已提交
79 80 81 82 83 84
				);
				// Trust all domains
				if (pickedDomains.indexOf('*') !== -1) {
					return true;
				}
				// Trust current domain
P
Pine Wu 已提交
85
				if (isURLDomainTrusted(resource, pickedDomains)) {
P
Pine Wu 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
					return true;
				}
				return false;
			}

			return false;
		}
	}
}

const rLocalhost = /^localhost(:\d+)?$/i;
const r127 = /^127.0.0.1(:\d+)?$/;

function isLocalhostAuthority(authority: string) {
	return rLocalhost.test(authority) || r127.test(authority);
}

/**
 * Check whether a domain like https://www.microsoft.com matches
 * the list of trusted domains.
 *
 * - Schemes must match
 * - There's no subdomain matching. For example https://microsoft.com doesn't match https://www.microsoft.com
P
Pine Wu 已提交
109
 * - Star matches all subdomains. For example https://*.microsoft.com matches https://www.microsoft.com and https://foo.bar.microsoft.com
P
Pine Wu 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
 */
export function isURLDomainTrusted(url: URI, trustedDomains: string[]) {
	if (isLocalhostAuthority(url.authority)) {
		return true;
	}

	const domain = `${url.scheme}://${url.authority}`;

	for (let i = 0; i < trustedDomains.length; i++) {
		if (trustedDomains[i] === '*') {
			return true;
		}

		if (trustedDomains[i] === domain) {
			return true;
		}

P
Pine Wu 已提交
127 128 129 130
		let parsedTrustedDomain;
		if (/^https?:\/\//.test(trustedDomains[i])) {
			parsedTrustedDomain = URI.parse(trustedDomains[i]);
			if (url.scheme !== parsedTrustedDomain.scheme) {
P
Pine Wu 已提交
131
				continue;
P
Pine Wu 已提交
132 133 134 135 136 137
			}
		} else {
			parsedTrustedDomain = URI.parse('https://' + trustedDomains[i]);
		}

		if (url.authority === parsedTrustedDomain.authority) {
P
Pine Wu 已提交
138
			return pathMatches(url.path, parsedTrustedDomain.path);
P
Pine Wu 已提交
139 140
		}

P
Pine Wu 已提交
141
		if (trustedDomains[i].indexOf('*') !== -1) {
P
Pine Wu 已提交
142

P
Pine Wu 已提交
143 144 145 146 147 148 149 150 151
			let reversedAuthoritySegments = url.authority.split('.').reverse();
			const reversedTrustedDomainAuthoritySegments = parsedTrustedDomain.authority.split('.').reverse();

			if (
				reversedTrustedDomainAuthoritySegments.length < reversedAuthoritySegments.length &&
				reversedTrustedDomainAuthoritySegments[reversedTrustedDomainAuthoritySegments.length - 1] === '*'
			) {
				reversedAuthoritySegments = reversedAuthoritySegments.slice(0, reversedTrustedDomainAuthoritySegments.length);
			}
P
Pine Wu 已提交
152

P
Pine Wu 已提交
153 154 155 156 157
			const authorityMatches = reversedAuthoritySegments.every((val, i) => {
				return reversedTrustedDomainAuthoritySegments[i] === '*' || val === reversedTrustedDomainAuthoritySegments[i];
			});

			if (authorityMatches && pathMatches(url.path, parsedTrustedDomain.path)) {
P
Pine Wu 已提交
158
				return true;
P
Pine Wu 已提交
159 160 161 162 163 164
			}
		}
	}

	return false;
}
P
Pine Wu 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

function pathMatches(open: string, rule: string) {
	if (rule === '/') {
		return true;
	}

	const openSegments = open.split('/');
	const ruleSegments = rule.split('/');
	for (let i = 0; i < ruleSegments.length; i++) {
		if (ruleSegments[i] !== openSegments[i]) {
			return false;
		}
	}

	return true;
}