Skip to content

Commit

Permalink
src/util: resolveToolsGopath ignores toolsGopath from untrusted works…
Browse files Browse the repository at this point in the history
…pace

And initialize the default config object when the module loads,
and reuse it.

For #1094

Change-Id: Ia80d703487f6a717dc7aed6d52baeb73c4b49707
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/283257
Trust: Hyang-Ah Hana Kim <[email protected]>
Reviewed-by: Suzy Mueller <[email protected]>
  • Loading branch information
hyangah committed Jan 15, 2021
1 parent e1a6cb4 commit 3e3f506
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
27 changes: 19 additions & 8 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ const SECURITY_SENSITIVE_CONFIG: string[] = [
'goroot', 'gopath', 'toolsGopath', 'alternateTools', 'inferGopath'
];

let defaultConfig: Configuration = null;

// Initialize the singleton defaultConfig and register related commands.
// Prompt if workspace configuration was found but had to be ignored until
// the user has to explicitly opt in to trust the workspace.
export async function initConfig(ctx: vscode.ExtensionContext) {
const isTrusted = getFromWorkspaceState(WORKSPACE_IS_TRUSTED_KEY, false);
defaultConfig = new Configuration(isTrusted, vscode.workspace.getConfiguration);
if (isTrusted !== defaultConfig.workspaceIsTrusted) {
defaultConfig.toggleWorkspaceIsTrusted();
}
ctx.subscriptions.push(
vscode.commands.registerCommand('go.workspace.isTrusted.toggle', toggleWorkspaceIsTrusted)
);
Expand Down Expand Up @@ -66,24 +66,35 @@ async function toggleWorkspaceIsTrusted() {
// Go extension configuration for a workspace.
export class Configuration {
constructor(
private workspaceIsTrusted = false,
private _workspaceIsTrusted = false,
private getConfiguration = vscode.workspace.getConfiguration) { }

public toggleWorkspaceIsTrusted() {
this.workspaceIsTrusted = !this.workspaceIsTrusted;
return this.workspaceIsTrusted;
this._workspaceIsTrusted = !this._workspaceIsTrusted;
return this._workspaceIsTrusted;
}

// returns a Proxied vscode.WorkspaceConfiguration, which prevents
// from using the workspace configuration if the workspace is untrusted.
public get<T>(section: string, uri?: vscode.Uri): vscode.WorkspaceConfiguration {
const cfg = this.getConfiguration(section, uri);
if (section !== 'go' || this.workspaceIsTrusted) {
if (section !== 'go' || this._workspaceIsTrusted) {
return cfg;
}

return new WrappedConfiguration(cfg);
}

public workspaceIsTrusted(): boolean {
return this._workspaceIsTrusted;
}
}

const defaultConfig = new Configuration();

// Returns the workspace Configuration used by the extension.
export function DefaultConfig() {
return defaultConfig;
}

// wrappedConfiguration wraps vscode.WorkspaceConfiguration.
Expand Down Expand Up @@ -141,5 +152,5 @@ function getConfig(section: string, uri?: vscode.Uri) {
uri = null;
}
}
return defaultConfig ? defaultConfig.get(section, uri) : new Configuration().get(section, uri);
return defaultConfig.get(section, uri);
}
9 changes: 7 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import semver = require('semver');
import util = require('util');
import vscode = require('vscode');
import { NearestNeighborDict, Node } from './avlTree';
import { getGoConfig } from './config';
import { DefaultConfig, getGoConfig } from './config';
import { extensionId } from './const';
import { toolExecutionEnvironment } from './goEnv';
import { languageClient } from './goLanguageServer';
Expand Down Expand Up @@ -478,14 +478,19 @@ function resolveToolsGopath(): string {
return toolsGopathForWorkspace;
}

// If any of the folders in multi root have toolsGopath set, use it.
if (DefaultConfig().workspaceIsTrusted() === false) {
return toolsGopathForWorkspace;
}

// If any of the folders in multi root have toolsGopath set and the workspace is trusted, use it.
for (const folder of vscode.workspace.workspaceFolders) {
let toolsGopathFromConfig = <string>getGoConfig(folder.uri).inspect('toolsGopath').workspaceFolderValue;
toolsGopathFromConfig = resolvePath(toolsGopathFromConfig, folder.uri.fsPath);
if (toolsGopathFromConfig) {
return toolsGopathFromConfig;
}
}
return toolsGopathForWorkspace;
}

// getBinPath returns the path to the tool.
Expand Down

0 comments on commit 3e3f506

Please sign in to comment.