Skip to content

Commit 0375939

Browse files
committed
Permit usage of javaagent (no prompt) if resource not in workspace
- Fixes #1965 Signed-off-by: Roland Grunberg <rgrunber@redhat.com>
1 parent 8c4fa79 commit 0375939

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/javaServerStarter.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as os from 'os';
66
import * as fs from 'fs';
77
import { StreamInfo, Executable, ExecutableOptions } from 'vscode-languageclient/node';
88
import { RequirementsData } from './requirements';
9-
import { getJavaEncoding, IS_WORKSPACE_VMARGS_ALLOWED, getKey, getJavaagentFlag } from './settings';
9+
import { getJavaEncoding, IS_WORKSPACE_VMARGS_ALLOWED, getKey, getJavaagentFlag, isInWorkspaceFolder } from './settings';
1010
import { logger } from './log';
1111
import { getJavaConfiguration, deleteDirectory, ensureExists, getTimestamp } from './utils';
1212
import { workspace, ExtensionContext } from 'vscode';
@@ -65,11 +65,12 @@ function prepareParams(requirements: RequirementsData, javaConfiguration, worksp
6565
}
6666
let vmargsCheck = workspace.getConfiguration().inspect('java.jdt.ls.vmargs').workspaceValue;
6767
if (vmargsCheck !== undefined) {
68+
const isWorkspaceTrusted = (workspace as any).isTrusted; // keep compatibility for old engines < 1.56.0
6869
const agentFlag = getJavaagentFlag(vmargsCheck);
69-
if (agentFlag !== null) {
70+
if (agentFlag !== null && (isWorkspaceTrusted === undefined || !isWorkspaceTrusted)) {
7071
const keyVmargs = getKey(IS_WORKSPACE_VMARGS_ALLOWED, context.storagePath, vmargsCheck);
7172
const key = context.globalState.get(keyVmargs);
72-
if (key !== true) {
73+
if (key !== true && (workspace.workspaceFolders && isInWorkspaceFolder(agentFlag, workspace.workspaceFolders))) {
7374
vmargsCheck = workspace.getConfiguration().inspect('java.jdt.ls.vmargs').globalValue;
7475
}
7576
}

src/settings.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
import * as path from 'path';
4-
import { window, Uri, workspace, WorkspaceConfiguration, commands, ConfigurationTarget, env, ExtensionContext, TextEditor, Range, Disposable } from 'vscode';
4+
import { window, Uri, workspace, WorkspaceConfiguration, commands, ConfigurationTarget, env, ExtensionContext, TextEditor, Range, Disposable, WorkspaceFolder } from 'vscode';
55
import { Commands } from './commands';
66
import { getJavaConfiguration } from './utils';
77

@@ -141,11 +141,12 @@ export async function checkJavaPreferences(context: ExtensionContext) {
141141
}
142142
const vmargs = workspace.getConfiguration().inspect('java.jdt.ls.vmargs').workspaceValue;
143143
if (vmargs !== undefined) {
144+
const isWorkspaceTrusted = (workspace as any).isTrusted; // keep compatibility for old engines < 1.56.0
144145
const agentFlag = getJavaagentFlag(vmargs);
145-
if (agentFlag !== null) {
146+
if (agentFlag !== null && (isWorkspaceTrusted === undefined || !isWorkspaceTrusted)) {
146147
const keyVmargs = getKey(IS_WORKSPACE_VMARGS_ALLOWED, context.storagePath, vmargs);
147148
const vmargsVerified = globalState.get(keyVmargs);
148-
if (vmargsVerified === undefined || vmargsVerified === null) {
149+
if ((vmargsVerified === undefined || vmargsVerified === null) && (workspace.workspaceFolders && isInWorkspaceFolder(agentFlag, workspace.workspaceFolders))) {
149150
await window.showErrorMessage(`Security Warning! The java.jdt.ls.vmargs variable defined in ${env.appName} settings includes the (${agentFlag}) javagent preference. Do you allow it to be used?`, disallow, allow).then(async selection => {
150151
if (selection === allow) {
151152
globalState.update(keyVmargs, true);
@@ -187,6 +188,10 @@ export function getJavaagentFlag(vmargs) {
187188
return agentFlag;
188189
}
189190

191+
export function isInWorkspaceFolder(loc: string, workspaceFolders: readonly WorkspaceFolder[]) {
192+
return !path.isAbsolute(loc) || workspaceFolders.some(dir => loc.startsWith(dir.uri.fsPath));
193+
}
194+
190195
export enum ServerMode {
191196
STANDARD = 'Standard',
192197
LIGHTWEIGHT = 'LightWeight',

0 commit comments

Comments
 (0)