|
4 | 4 | *--------------------------------------------------------*/
|
5 | 5 |
|
6 | 6 | import vscode = require('vscode');
|
| 7 | +import { getFromWorkspaceState, updateWorkspaceState } from './stateUtils'; |
7 | 8 |
|
| 9 | +const WORKSPACE_IS_TRUSTED_KEY = 'WORKSPACE_IS_TRUSTED_KEY'; |
8 | 10 | const SECURITY_SENSITIVE_CONFIG: string[] = [
|
9 | 11 | 'goroot', 'gopath', 'toolsGopath', 'alternateTools'
|
10 | 12 | ];
|
11 | 13 |
|
| 14 | +let defaultConfig: Configuration = null; |
| 15 | + |
| 16 | +// Initialize the singleton defaultConfig and register related commands. |
| 17 | +// Prompt if workspace configuration was found but had to be ignored until |
| 18 | +// the user has to explicitly opt in to trust the workspace. |
| 19 | +export async function initConfig(ctx: vscode.ExtensionContext) { |
| 20 | + const isTrusted = getFromWorkspaceState(WORKSPACE_IS_TRUSTED_KEY, false); |
| 21 | + defaultConfig = new Configuration(isTrusted, vscode.workspace.getConfiguration); |
| 22 | + ctx.subscriptions.push( |
| 23 | + vscode.commands.registerCommand('go.workspace.isTrusted.toggle', defaultConfig.toggleWorkspaceIsTrusted) |
| 24 | + ); |
| 25 | + |
| 26 | + if (isTrusted) { |
| 27 | + return; |
| 28 | + } |
| 29 | + const ignored = ignoredWorkspaceConfig(vscode.workspace.getConfiguration('go'), SECURITY_SENSITIVE_CONFIG); |
| 30 | + if (ignored.length === 0) { |
| 31 | + return; |
| 32 | + } |
| 33 | + const ignoredSettings = ignored.map((x) => `"go.${x}"`).join(','); |
| 34 | + const val = await vscode.window.showWarningMessage( |
| 35 | + `Some workspace/folder-level settings (${ignoredSettings}) from the untrusted workspace are disabled ` + |
| 36 | + `by default. If this workspace is trusted, explicitly enable the workspace/folder-level settings ` + |
| 37 | + `by running the "Go: Toggle Workspace Trust Flag" command.`, |
| 38 | + 'OK', |
| 39 | + 'Trust This Workspace', |
| 40 | + 'More Info'); |
| 41 | + switch (val) { |
| 42 | + case 'Trust This Workspace': |
| 43 | + await defaultConfig.toggleWorkspaceIsTrusted(); |
| 44 | + break; |
| 45 | + case 'More Info': |
| 46 | + vscode.env.openExternal( |
| 47 | + vscode.Uri.parse(`https://github.com/golang/vscode-go/blob/master/docs/settings.md#security`)); |
| 48 | + break; |
| 49 | + default: |
| 50 | + break; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +function ignoredWorkspaceConfig(cfg: vscode.WorkspaceConfiguration, keys: string[]) { |
| 55 | + return keys.filter((key) => { |
| 56 | + const inspect = cfg.inspect(key); |
| 57 | + return inspect.workspaceValue !== undefined || inspect.workspaceFolderValue !== undefined; |
| 58 | + }); |
| 59 | +} |
| 60 | + |
12 | 61 | // Go extension configuration for a workspace.
|
13 | 62 | export class Configuration {
|
14 | 63 | constructor(
|
15 |
| - private isTrustedWorkspace: boolean, |
| 64 | + private workspaceIsTrusted: boolean, |
16 | 65 | private getConfiguration: typeof vscode.workspace.getConfiguration) { }
|
17 | 66 |
|
| 67 | + public async toggleWorkspaceIsTrusted() { |
| 68 | + this.workspaceIsTrusted = !this.workspaceIsTrusted; |
| 69 | + await updateWorkspaceState(WORKSPACE_IS_TRUSTED_KEY, this.workspaceIsTrusted); |
| 70 | + } |
| 71 | + |
18 | 72 | // returns a Proxied vscode.WorkspaceConfiguration, which prevents
|
19 | 73 | // from using the workspace configuration if the workspace is untrusted.
|
20 | 74 | public get<T>(uri?: vscode.Uri): vscode.WorkspaceConfiguration {
|
21 | 75 | const cfg = this.getConfiguration('go', uri);
|
22 |
| - if (this.isTrustedWorkspace) { |
| 76 | + if (this.workspaceIsTrusted) { |
23 | 77 | return cfg;
|
24 | 78 | }
|
25 | 79 |
|
|
0 commit comments