Skip to content

Commit

Permalink
Address link protection feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
octref committed Sep 16, 2019
1 parent 7a9dfd3 commit 7eb3558
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 55 deletions.
57 changes: 17 additions & 40 deletions src/vs/workbench/contrib/url/common/trustedDomains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -27,65 +26,43 @@ 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,
quickInputService: IQuickInputService,
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'),
id: 'configure'
};

const pickedResult = await quickInputService.pick(
[openAllLinksItem, trustDomainAndOpenLinkItem, configureTrustedDomainItem],
[trustDomainAndOpenLinkItem, trustSubDomainAndOpenLinkItem, openAllLinksItem, configureTrustedDomainItem],
{
activeItem: trustDomainAndOpenLinkItem
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
10 changes: 1 addition & 9 deletions src/vs/workbench/contrib/url/common/url.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench).registerWorkbenchContribution(
OpenerValidatorContributions,
Expand Down

0 comments on commit 7eb3558

Please sign in to comment.