Skip to content

Commit 3e3f506

Browse files
committed
src/util: resolveToolsGopath ignores toolsGopath from untrusted workspace
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]>
1 parent e1a6cb4 commit 3e3f506

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/config.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ const SECURITY_SENSITIVE_CONFIG: string[] = [
1111
'goroot', 'gopath', 'toolsGopath', 'alternateTools', 'inferGopath'
1212
];
1313

14-
let defaultConfig: Configuration = null;
15-
1614
// Initialize the singleton defaultConfig and register related commands.
1715
// Prompt if workspace configuration was found but had to be ignored until
1816
// the user has to explicitly opt in to trust the workspace.
1917
export async function initConfig(ctx: vscode.ExtensionContext) {
2018
const isTrusted = getFromWorkspaceState(WORKSPACE_IS_TRUSTED_KEY, false);
21-
defaultConfig = new Configuration(isTrusted, vscode.workspace.getConfiguration);
19+
if (isTrusted !== defaultConfig.workspaceIsTrusted) {
20+
defaultConfig.toggleWorkspaceIsTrusted();
21+
}
2222
ctx.subscriptions.push(
2323
vscode.commands.registerCommand('go.workspace.isTrusted.toggle', toggleWorkspaceIsTrusted)
2424
);
@@ -66,24 +66,35 @@ async function toggleWorkspaceIsTrusted() {
6666
// Go extension configuration for a workspace.
6767
export class Configuration {
6868
constructor(
69-
private workspaceIsTrusted = false,
69+
private _workspaceIsTrusted = false,
7070
private getConfiguration = vscode.workspace.getConfiguration) { }
7171

7272
public toggleWorkspaceIsTrusted() {
73-
this.workspaceIsTrusted = !this.workspaceIsTrusted;
74-
return this.workspaceIsTrusted;
73+
this._workspaceIsTrusted = !this._workspaceIsTrusted;
74+
return this._workspaceIsTrusted;
7575
}
7676

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

8585
return new WrappedConfiguration(cfg);
8686
}
87+
88+
public workspaceIsTrusted(): boolean {
89+
return this._workspaceIsTrusted;
90+
}
91+
}
92+
93+
const defaultConfig = new Configuration();
94+
95+
// Returns the workspace Configuration used by the extension.
96+
export function DefaultConfig() {
97+
return defaultConfig;
8798
}
8899

89100
// wrappedConfiguration wraps vscode.WorkspaceConfiguration.
@@ -141,5 +152,5 @@ function getConfig(section: string, uri?: vscode.Uri) {
141152
uri = null;
142153
}
143154
}
144-
return defaultConfig ? defaultConfig.get(section, uri) : new Configuration().get(section, uri);
155+
return defaultConfig.get(section, uri);
145156
}

src/util.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import semver = require('semver');
1111
import util = require('util');
1212
import vscode = require('vscode');
1313
import { NearestNeighborDict, Node } from './avlTree';
14-
import { getGoConfig } from './config';
14+
import { DefaultConfig, getGoConfig } from './config';
1515
import { extensionId } from './const';
1616
import { toolExecutionEnvironment } from './goEnv';
1717
import { languageClient } from './goLanguageServer';
@@ -478,14 +478,19 @@ function resolveToolsGopath(): string {
478478
return toolsGopathForWorkspace;
479479
}
480480

481-
// If any of the folders in multi root have toolsGopath set, use it.
481+
if (DefaultConfig().workspaceIsTrusted() === false) {
482+
return toolsGopathForWorkspace;
483+
}
484+
485+
// If any of the folders in multi root have toolsGopath set and the workspace is trusted, use it.
482486
for (const folder of vscode.workspace.workspaceFolders) {
483487
let toolsGopathFromConfig = <string>getGoConfig(folder.uri).inspect('toolsGopath').workspaceFolderValue;
484488
toolsGopathFromConfig = resolvePath(toolsGopathFromConfig, folder.uri.fsPath);
485489
if (toolsGopathFromConfig) {
486490
return toolsGopathFromConfig;
487491
}
488492
}
493+
return toolsGopathForWorkspace;
489494
}
490495

491496
// getBinPath returns the path to the tool.

0 commit comments

Comments
 (0)