Skip to content

Commit d058e4e

Browse files
authored
launch lightweight LS in untrusted workspaces (#1975)
Signed-off-by: Yan Zhang <yanzh@microsoft.com>
1 parent 6fb91c3 commit d058e4e

4 files changed

Lines changed: 52 additions & 12 deletions

File tree

package-lock.json

Lines changed: 15 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010
"bugs": "https://github.com/redhat-developer/vscode-java/issues",
1111
"preview": true,
1212
"enableProposedApi": false,
13+
"capabilities": {
14+
"untrustedWorkspaces": {
15+
"supported": "limited",
16+
"restrictedConfigurations": [
17+
"java.home",
18+
"java.jdt.ls.vmargs"
19+
]
20+
}
21+
},
1322
"engines": {
1423
"vscode": "^1.53.2"
1524
},

src/apiManager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { goToDefinitionCommand, goToDefinitionProvider } from "./goToDefinition"
77
import { commands, Uri } from "vscode";
88
import { Commands } from "./commands";
99
import { Emitter } from "vscode-languageclient";
10-
import { ServerMode, getJavaServerMode } from "./settings";
10+
import { ServerMode } from "./settings";
1111
import { registerHoverCommand } from "./hoverAction";
1212

1313
class ApiManager {
@@ -17,7 +17,7 @@ class ApiManager {
1717
private onDidServerModeChangeEmitter: Emitter<ServerMode> = new Emitter<ServerMode>();
1818
private onDidProjectsImportEmitter: Emitter<Uri[]> = new Emitter<Uri[]>();
1919

20-
public initialize(requirements: RequirementsData): void {
20+
public initialize(requirements: RequirementsData, serverMode: ServerMode): void {
2121
const getDocumentSymbols: getDocumentSymbolsCommand = getDocumentSymbolsProvider();
2222
const goToDefinition: goToDefinitionCommand = goToDefinitionProvider();
2323

@@ -48,7 +48,7 @@ class ApiManager {
4848
getClasspaths,
4949
isTestFile,
5050
onDidClasspathUpdate,
51-
serverMode: getJavaServerMode(),
51+
serverMode,
5252
onDidServerModeChange,
5353
onDidProjectsImport,
5454
};

src/extension.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
146146
const workspacePath = path.resolve(storagePath + '/jdt_ws');
147147
const syntaxServerWorkspacePath = path.resolve(storagePath + '/ss_ws');
148148

149-
const serverMode = getJavaServerMode();
149+
let serverMode = getJavaServerMode();
150+
const isWorkspaceTrusted = (workspace as any).isTrusted; // TODO: use workspace.isTrusted directly when other clients catch up to adopt 1.56.0
151+
if (isWorkspaceTrusted !== undefined && !isWorkspaceTrusted) { // keep compatibility for old engines < 1.56.0
152+
serverMode = ServerMode.LIGHTWEIGHT;
153+
}
150154
commands.executeCommand('setContext', 'java:serverMode', serverMode);
151155
const isDebugModeByClientPort = !!process.env['SYNTAXLS_CLIENT_PORT'] || !!process.env['JDTLS_CLIENT_PORT'];
152156
const requireSyntaxServer = (serverMode !== ServerMode.STANDARD) && (!isDebugModeByClientPort || !!process.env['SYNTAXLS_CLIENT_PORT']);
@@ -211,7 +215,7 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
211215
outputChannelName: extensionName
212216
};
213217

214-
apiManager.initialize(requirements);
218+
apiManager.initialize(requirements, serverMode);
215219

216220
if (requireSyntaxServer) {
217221
if (process.env['SYNTAXLS_CLIENT_PORT']) {
@@ -274,6 +278,16 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
274278
* @param force force to switch server mode without asking
275279
*/
276280
commands.registerCommand(Commands.SWITCH_SERVER_MODE, async (switchTo: ServerMode, force: boolean = false) => {
281+
const isWorkspaceTrusted = (workspace as any).isTrusted;
282+
if (isWorkspaceTrusted !== undefined && !isWorkspaceTrusted) { // keep compatibility for old engines < 1.56.0
283+
const button = "Manage Workspace Trust";
284+
const choice = await window.showInformationMessage("For security concern, Java language server cannot be switched to Standard mode in untrusted workspaces.", button);
285+
if (choice === button) {
286+
commands.executeCommand("workbench.action.manageTrust");
287+
}
288+
return;
289+
}
290+
277291
const clientStatus: ClientStatus = standardClient.getClientStatus();
278292
if (clientStatus === ClientStatus.Starting || clientStatus === ClientStatus.Started) {
279293
return;
@@ -333,6 +347,15 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
333347
if (requireStandardServer) {
334348
startStandardServer(context, requirements, clientOptions, workspacePath, resolve);
335349
}
350+
351+
const onDidGrantWorkspaceTrust = (workspace as any).onDidGrantWorkspaceTrust;
352+
if (onDidGrantWorkspaceTrust !== undefined) { // keep compatibility for old engines < 1.56.0
353+
context.subscriptions.push(onDidGrantWorkspaceTrust(() => {
354+
if (getJavaServerMode() !== ServerMode.LIGHTWEIGHT) {
355+
commands.executeCommand(Commands.SWITCH_SERVER_MODE, ServerMode.STANDARD, true);
356+
}
357+
}));
358+
}
336359
});
337360
});
338361
}

0 commit comments

Comments
 (0)