From 7eb355846f496786e4b8b8a2e5b3763d65e96cf8 Mon Sep 17 00:00:00 2001 From: Pine Wu Date: Mon, 16 Sep 2019 15:10:45 -0700 Subject: [PATCH] Address link protection feedbacks --- .../contrib/url/common/trustedDomains.ts | 57 ++++++------------- .../trustedDomainsFileSystemProvider.ts | 29 ++++++++-- .../contrib/url/common/url.contribution.ts | 10 +--- 3 files changed, 41 insertions(+), 55 deletions(-) diff --git a/src/vs/workbench/contrib/url/common/trustedDomains.ts b/src/vs/workbench/contrib/url/common/trustedDomains.ts index 6b3e5a22d0275..aa0e71c56a7c5 100644 --- a/src/vs/workbench/contrib/url/common/trustedDomains.ts +++ b/src/vs/workbench/contrib/url/common/trustedDomains.ts @@ -6,7 +6,6 @@ import { URI } from 'vs/base/common/uri'; import { localize } from 'vs/nls'; import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; -import { INotificationService } from 'vs/platform/notification/common/notification'; import { IProductService } from 'vs/platform/product/common/productService'; import { IQuickInputService, IQuickPickItem } from 'vs/platform/quickinput/common/quickInput'; import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage'; @@ -27,38 +26,6 @@ export const configureTrustedDomainSettingsCommand = { } }; -export const toggleLinkProtection = { - id: 'workbench.action.toggleLinkProtection', - description: { - description: localize( - 'trustedDomain.toggleLinkProtectionForTrustedDomains', - 'Toggle Link Protection for Trusted Domains' - ), - args: [] - }, - handler: async (accessor: ServicesAccessor) => { - const storageService = accessor.get(IStorageService); - const notificationService = accessor.get(INotificationService); - const trustedDomains = readTrustedDomains(storageService, accessor.get(IProductService)); - if (trustedDomains.indexOf('*') === -1) { - storageService.store( - 'http.linkProtectionTrustedDomains', - JSON.stringify(trustedDomains.concat(['*'])), - StorageScope.GLOBAL - ); - notificationService.info(localize('trustedDomain.linkProtectionDisabled', 'Link Protection disabled')); - } else { - storageService.store( - 'http.linkProtectionTrustedDomains', - JSON.stringify(trustedDomains.filter(x => x !== '*')), - StorageScope.GLOBAL - ); - notificationService.info(localize('trustedDomain.linkProtectionEnabled', 'Link Protection enabled')); - } - return; - } -}; - export async function configureOpenerTrustedDomainsHandler( trustedDomains: string[], domainToConfigure: string, @@ -66,18 +33,28 @@ export async function configureOpenerTrustedDomainsHandler( storageService: IStorageService, editorService: IEditorService ) { - const openAllLinksItem: IQuickPickItem = { - type: 'item', - label: localize('trustedDomain.trustAllAndOpenLink', 'Disable Link Protection and open link'), - id: '*', - picked: trustedDomains.indexOf('*') !== -1 - }; + const parsedDomainToConfigure = URI.parse(domainToConfigure); + const toplevelDomainSegements = domainToConfigure.split('.'); + const domainEnd = toplevelDomainSegements.slice(toplevelDomainSegements.length - 2).join('.'); + const topLevelDomain = parsedDomainToConfigure.scheme + '://' + '*.' + domainEnd; + const trustDomainAndOpenLinkItem: IQuickPickItem = { type: 'item', label: localize('trustedDomain.trustDomainAndOpenLink', 'Trust {0} and open link', domainToConfigure), id: domainToConfigure, picked: true }; + const trustSubDomainAndOpenLinkItem: IQuickPickItem = { + type: 'item', + label: localize('trustedDomain.trustSubDomainAndOpenLink', 'Trust all domains ending in {0} and open link', domainEnd), + id: topLevelDomain + }; + const openAllLinksItem: IQuickPickItem = { + type: 'item', + label: localize('trustedDomain.trustAllAndOpenLink', 'Disable Link Protection and open link'), + id: '*', + picked: trustedDomains.indexOf('*') !== -1 + }; const configureTrustedDomainItem: IQuickPickItem = { type: 'item', label: localize('trustedDomain.configureTrustedDomains', 'Configure Trusted Domains'), @@ -85,7 +62,7 @@ export async function configureOpenerTrustedDomainsHandler( }; const pickedResult = await quickInputService.pick( - [openAllLinksItem, trustDomainAndOpenLinkItem, configureTrustedDomainItem], + [trustDomainAndOpenLinkItem, trustSubDomainAndOpenLinkItem, openAllLinksItem, configureTrustedDomainItem], { activeItem: trustDomainAndOpenLinkItem } diff --git a/src/vs/workbench/contrib/url/common/trustedDomainsFileSystemProvider.ts b/src/vs/workbench/contrib/url/common/trustedDomainsFileSystemProvider.ts index 405205d5b719a..b5d53fb63310e 100644 --- a/src/vs/workbench/contrib/url/common/trustedDomainsFileSystemProvider.ts +++ b/src/vs/workbench/contrib/url/common/trustedDomainsFileSystemProvider.ts @@ -31,11 +31,29 @@ const TRUSTED_DOMAINS_STAT: IStat = { size: 0 }; -const CONFIG_HELP_TEXT = `// Configure trusted domains by editing and saving this file -// You can use \`*\` to match subdomains. For example https://*.visualstudio.com would match: -// - https://code.visualstudio.com -// - https://update.code.visualstudio.com +const CONFIG_HELP_TEXT = `// You can run "Configure Trusted Domains" command to edit trusted domains settings in this JSON file. +// The setting is updated upon saving this file. +// Links that match one of the entries can be opened without link protection. +// +// Example entries include: +// - "microsoft.com" +// - "*.microsoft.com": Match all domains ending in "microsoft.com" +// - "*": Match all domains +// +// By default, VS Code whitelists certain localhost and domains such as "code.visualstudio.com" `; +const CONFIG_PLACEHOLDER_TEXT = `[ + // "microsoft.com" +] +`; + +function computeTrustedDomainContent(trustedDomains: string[]) { + if (trustedDomains.length === 0) { + return CONFIG_HELP_TEXT + CONFIG_PLACEHOLDER_TEXT; + } + + return CONFIG_HELP_TEXT + JSON.stringify(trustedDomains, null, 2); +} export class TrustedDomainsFileSystemProvider implements IFileSystemProvider, IWorkbenchContribution { readonly capabilities = FileSystemProviderCapabilities.FileReadWrite; @@ -64,9 +82,8 @@ export class TrustedDomainsFileSystemProvider implements IFileSystemProvider, IW } } catch (err) { } - const trustedDomainsContent = CONFIG_HELP_TEXT + JSON.stringify(trustedDomains, null, 2); - + const trustedDomainsContent = computeTrustedDomainContent(trustedDomains); const buffer = VSBuffer.fromString(trustedDomainsContent).buffer; return Promise.resolve(buffer); } diff --git a/src/vs/workbench/contrib/url/common/url.contribution.ts b/src/vs/workbench/contrib/url/common/url.contribution.ts index 0d108724f0b1e..f51bd6d3f03b5 100644 --- a/src/vs/workbench/contrib/url/common/url.contribution.ts +++ b/src/vs/workbench/contrib/url/common/url.contribution.ts @@ -14,7 +14,7 @@ import { Registry } from 'vs/platform/registry/common/platform'; import { IURLService } from 'vs/platform/url/common/url'; import { Extensions as ActionExtensions, IWorkbenchActionRegistry } from 'vs/workbench/common/actions'; import { Extensions as WorkbenchExtensions, IWorkbenchContributionsRegistry } from 'vs/workbench/common/contributions'; -import { configureTrustedDomainSettingsCommand, toggleLinkProtection } from 'vs/workbench/contrib/url/common/trustedDomains'; +import { configureTrustedDomainSettingsCommand } from 'vs/workbench/contrib/url/common/trustedDomains'; import { OpenerValidatorContributions } from 'vs/workbench/contrib/url/common/trustedDomainsValidator'; import { TrustedDomainsFileSystemProvider } from 'vs/workbench/contrib/url/common/trustedDomainsFileSystemProvider'; @@ -56,14 +56,6 @@ MenuRegistry.appendMenuItem(MenuId.CommandPalette, { title: configureTrustedDomainSettingsCommand.description.description } }); -CommandsRegistry.registerCommand(toggleLinkProtection); -MenuRegistry.appendMenuItem(MenuId.CommandPalette, { - command: { - id: toggleLinkProtection.id, - title: toggleLinkProtection.description.description - } -}); - Registry.as(WorkbenchExtensions.Workbench).registerWorkbenchContribution( OpenerValidatorContributions,