-
Notifications
You must be signed in to change notification settings - Fork 28k
/
trustedDomainsValidator.ts
209 lines (183 loc) · 8.58 KB
/
trustedDomainsValidator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*---------------------------------------------------------------------------------------------
* 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';
import { URI } from 'vs/base/common/uri';
import { localize } from 'vs/nls';
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { IOpenerService, matchesScheme, OpenOptions } from 'vs/platform/opener/common/opener';
import { IProductService } from 'vs/platform/product/common/productService';
import { IQuickInputService } from 'vs/platform/quickinput/common/quickInput';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { configureOpenerTrustedDomainsHandler, readAuthenticationTrustedDomains, readStaticTrustedDomains, readWorkspaceTrustedDomains } from 'vs/workbench/contrib/url/browser/trustedDomains';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IdleValue } from 'vs/base/common/async';
import { IAuthenticationService } from 'vs/workbench/services/authentication/common/authentication';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { testUrlMatchesGlob } from 'vs/workbench/contrib/url/common/urlGlob';
import { IWorkspaceTrustManagementService } from 'vs/platform/workspace/common/workspaceTrust';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
export class OpenerValidatorContributions implements IWorkbenchContribution {
private _readWorkspaceTrustedDomainsResult: IdleValue<Promise<string[]>>;
private _readAuthenticationTrustedDomainsResult: IdleValue<Promise<string[]>>;
constructor(
@IOpenerService private readonly _openerService: IOpenerService,
@IStorageService private readonly _storageService: IStorageService,
@IDialogService private readonly _dialogService: IDialogService,
@IProductService private readonly _productService: IProductService,
@IQuickInputService private readonly _quickInputService: IQuickInputService,
@IEditorService private readonly _editorService: IEditorService,
@IClipboardService private readonly _clipboardService: IClipboardService,
@ITelemetryService private readonly _telemetryService: ITelemetryService,
@IInstantiationService private readonly _instantiationService: IInstantiationService,
@IAuthenticationService private readonly _authenticationService: IAuthenticationService,
@IWorkspaceContextService private readonly _workspaceContextService: IWorkspaceContextService,
@IConfigurationService private readonly _configurationService: IConfigurationService,
@IWorkspaceTrustManagementService private readonly _workspaceTrustService: IWorkspaceTrustManagementService,
) {
this._openerService.registerValidator({ shouldOpen: (uri, options) => this.validateLink(uri, options) });
this._readAuthenticationTrustedDomainsResult = new IdleValue(() =>
this._instantiationService.invokeFunction(readAuthenticationTrustedDomains));
this._authenticationService.onDidRegisterAuthenticationProvider(() => {
this._readAuthenticationTrustedDomainsResult?.dispose();
this._readAuthenticationTrustedDomainsResult = new IdleValue(() =>
this._instantiationService.invokeFunction(readAuthenticationTrustedDomains));
});
this._readWorkspaceTrustedDomainsResult = new IdleValue(() =>
this._instantiationService.invokeFunction(readWorkspaceTrustedDomains));
this._workspaceContextService.onDidChangeWorkspaceFolders(() => {
this._readWorkspaceTrustedDomainsResult?.dispose();
this._readWorkspaceTrustedDomainsResult = new IdleValue(() =>
this._instantiationService.invokeFunction(readWorkspaceTrustedDomains));
});
}
async validateLink(resource: URI | string, openOptions?: OpenOptions): Promise<boolean> {
if (!matchesScheme(resource, Schemas.http) && !matchesScheme(resource, Schemas.https)) {
return true;
}
if (openOptions?.fromWorkspace && this._workspaceTrustService.isWorkspaceTrusted() && !this._configurationService.getValue('workbench.trustedDomains.promptInTrustedWorkspace')) {
return true;
}
const originalResource = resource;
if (typeof resource === 'string') {
resource = URI.parse(resource);
}
const { scheme, authority, path, query, fragment } = resource;
const domainToOpen = `${scheme}://${authority}`;
const [workspaceDomains, userDomains] = await Promise.all([this._readWorkspaceTrustedDomainsResult.value, this._readAuthenticationTrustedDomainsResult.value]);
const { defaultTrustedDomains, trustedDomains, } = this._instantiationService.invokeFunction(readStaticTrustedDomains);
const allTrustedDomains = [...defaultTrustedDomains, ...trustedDomains, ...userDomains, ...workspaceDomains];
if (isURLDomainTrusted(resource, allTrustedDomains)) {
return true;
} else {
let formattedLink = `${scheme}://${authority}${path}`;
const linkTail = `${query ? '?' + query : ''}${fragment ? '#' + fragment : ''}`;
const remainingLength = Math.max(0, 60 - formattedLink.length);
const linkTailLengthToKeep = Math.min(Math.max(5, remainingLength), linkTail.length);
if (linkTailLengthToKeep === linkTail.length) {
formattedLink += linkTail;
} else {
// keep the first char ? or #
// add ... and keep the tail end as much as possible
formattedLink += linkTail.charAt(0) + '...' + linkTail.substring(linkTail.length - linkTailLengthToKeep + 1);
}
const { choice } = await this._dialogService.show(
Severity.Info,
localize(
'openExternalLinkAt',
'Do you want {0} to open the external website?',
this._productService.nameShort
),
[
localize('open', 'Open'),
localize('copy', 'Copy'),
localize('cancel', 'Cancel'),
localize('configureTrustedDomains', 'Configure Trusted Domains')
],
{
detail: typeof originalResource === 'string' ? originalResource : formattedLink,
cancelId: 2
}
);
// Open Link
if (choice === 0) {
return true;
}
// Copy Link
else if (choice === 1) {
this._clipboardService.writeText(typeof originalResource === 'string' ? originalResource : resource.toString(true));
}
// Configure Trusted Domains
else if (choice === 3) {
const pickedDomains = await configureOpenerTrustedDomainsHandler(
trustedDomains,
domainToOpen,
resource,
this._quickInputService,
this._storageService,
this._editorService,
this._telemetryService,
);
// Trust all domains
if (pickedDomains.indexOf('*') !== -1) {
return true;
}
// Trust current domain
if (isURLDomainTrusted(resource, pickedDomains)) {
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);
}
/**
* Case-normalize some case-insensitive URLs, such as github.
*/
function normalizeURL(url: string | URI): string {
const caseInsensitiveAuthorities = ['github.com'];
try {
const parsed = typeof url === 'string' ? URI.parse(url, true) : url;
if (caseInsensitiveAuthorities.includes(parsed.authority)) {
return parsed.with({ path: parsed.path.toLowerCase() }).toString(true);
} else {
return parsed.toString(true);
}
} catch { return url.toString(); }
}
/**
* 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
* - Star matches all subdomains. For example https://*.microsoft.com matches https://www.microsoft.com and https://foo.bar.microsoft.com
*/
export function isURLDomainTrusted(url: URI, trustedDomains: string[]) {
url = URI.parse(normalizeURL(url));
trustedDomains = trustedDomains.map(normalizeURL);
if (isLocalhostAuthority(url.authority)) {
return true;
}
for (let i = 0; i < trustedDomains.length; i++) {
if (trustedDomains[i] === '*') {
return true;
}
if (testUrlMatchesGlob(url, trustedDomains[i])) {
return true;
}
}
return false;
}