Skip to content

Commit

Permalink
Implement workspace trust for php-language-features
Browse files Browse the repository at this point in the history
  • Loading branch information
roblourens committed Apr 21, 2021
1 parent 6437567 commit 27cf6a3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
5 changes: 5 additions & 0 deletions extensions/php-language-features/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
"engines": {
"vscode": "0.10.x"
},
"enableProposedApi": true,
"activationEvents": [
"onLanguage:php"
],
"main": "./out/phpMain",
"categories": [
"Programming Languages"
],
"workspaceTrust": {
"request": "onDemand",
"description": "The extension requires workspace trust when the `php.validate.executablePath` setting will load a version of PHP in the workspace."
},
"contributes": {
"configuration": {
"title": "%configuration.title%",
Expand Down
63 changes: 40 additions & 23 deletions extensions/php-language-features/src/features/validationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ export default class PHPValidationProvider {
vscode.commands.executeCommand('setContext', 'php.untrustValidationExecutableContext', true);
}

const trustEnabled = vscode.workspace.getConfiguration().get('security.workspace.trust.enabled');
if (trustEnabled) {
vscode.workspace.requestWorkspaceTrust();
}

this.delayers = Object.create(null);
if (this.pauseValidation) {
this.pauseValidation = oldExecutable === this.config.executable;
Expand Down Expand Up @@ -173,9 +178,6 @@ export default class PHPValidationProvider {
return;
}

interface MessageItem extends vscode.MessageItem {
id: string;
}

let trigger = () => {
let key = textDocument.uri.toString();
Expand All @@ -187,35 +189,50 @@ export default class PHPValidationProvider {
delayer.trigger(() => this.doValidate(textDocument));
};

if (this.config!.executableIsUserDefined !== undefined && !this.config!.executableIsUserDefined) {
const trustEnabled = vscode.workspace.getConfiguration().get('security.workspace.trust.enabled');
if (trustEnabled) {
if (vscode.workspace.isTrusted) {
trigger();
}
} else if (this.config!.executableIsUserDefined !== undefined && !this.config!.executableIsUserDefined) {
const checkedExecutablePath = this.workspaceStore.get<string | undefined>(Setting.CheckedExecutablePath, undefined);
if (!checkedExecutablePath || checkedExecutablePath !== this.config!.executable) {
const selected = await vscode.window.showInformationMessage<MessageItem>(
localize('php.useExecutablePath', 'Do you allow {0} (defined as a workspace setting) to be executed to lint PHP files?', this.config!.executable),
{
title: localize('php.yes', 'Allow'),
id: 'yes'
},
{
title: localize('php.no', 'Disallow'),
isCloseAffordance: true,
id: 'no'
}
);

if (!selected || selected.id === 'no') {
this.pauseValidation = true;
} else if (selected.id === 'yes') {
if (await this.showCustomTrustDialog()) {
this.workspaceStore.update(Setting.CheckedExecutablePath, this.config!.executable);
vscode.commands.executeCommand('setContext', 'php.untrustValidationExecutableContext', true);
trigger();
} else {
this.pauseValidation = true;
return;
}
}

return;
trigger();
}
}

private async showCustomTrustDialog(): Promise<boolean> {
interface MessageItem extends vscode.MessageItem {
id: string;
}

const selected = await vscode.window.showInformationMessage<MessageItem>(
localize('php.useExecutablePath', 'Do you allow {0} (defined as a workspace setting) to be executed to lint PHP files?', this.config!.executable),
{
title: localize('php.yes', 'Allow'),
id: 'yes'
},
{
title: localize('php.no', 'Disallow'),
isCloseAffordance: true,
id: 'no'
}
);

if (selected && selected.id === 'yes') {
return true;
}

trigger();
return false;
}

private doValidate(textDocument: vscode.TextDocument): Promise<void> {
Expand Down
1 change: 1 addition & 0 deletions extensions/php-language-features/src/typings/refs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

/// <reference path='../../../../src/vs/vscode.d.ts'/>
/// <reference types='@types/node'/>
/// <reference path='../../../../src/vs/vscode.proposed.d.ts'/>

0 comments on commit 27cf6a3

Please sign in to comment.