-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathfullAccessHost.ts
308 lines (269 loc) · 11.3 KB
/
fullAccessHost.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* fullAccessHost.ts
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*
* Implementation of host where it is allowed to run external executables.
*/
import * as child_process from 'child_process';
import { CancellationToken } from 'vscode-languageserver';
import { PythonPathResult } from '../analyzer/pythonPathUtils';
import { OperationCanceledException, onCancellationRequested, throwIfCancellationRequested } from './cancellationUtils';
import { PythonPlatform } from './configOptions';
import { assertNever } from './debug';
import { HostKind, NoAccessHost, ScriptOutput } from './host';
import { getAnyExtensionFromPath, normalizePath } from './pathUtils';
import { PythonVersion } from './pythonVersion';
import { ServiceKeys } from './serviceKeys';
import { ServiceProvider } from './serviceProvider';
import { Uri } from './uri/uri';
import { isDirectory } from './uri/uriUtils';
// preventLocalImports removes the working directory from sys.path.
// The -c flag adds it automatically, which can allow some stdlib
// modules (like json) to be overridden by other files (like json.py).
const removeCwdFromSysPath = [
'import os, os.path, sys',
'normalize = lambda p: os.path.normcase(os.path.normpath(p))',
'cwd = normalize(os.getcwd())',
'orig_sys_path = [p for p in sys.path if p != ""]',
'sys.path[:] = [p for p in sys.path if p != "" and normalize(p) != cwd]',
];
const extractSys = [
...removeCwdFromSysPath,
'import sys, json',
'json.dump(dict(path=orig_sys_path, prefix=sys.prefix), sys.stdout)',
].join('; ');
const extractVersion = [
...removeCwdFromSysPath,
'import sys, json',
'json.dump(tuple(sys.version_info), sys.stdout)',
].join('; ');
export class LimitedAccessHost extends NoAccessHost {
override get kind(): HostKind {
return HostKind.LimitedAccess;
}
override getPythonPlatform(logInfo?: string[]): PythonPlatform | undefined {
if (process.platform === 'darwin') {
return PythonPlatform.Darwin;
} else if (process.platform === 'linux') {
return PythonPlatform.Linux;
} else if (process.platform === 'win32') {
return PythonPlatform.Windows;
}
return undefined;
}
}
export class FullAccessHost extends LimitedAccessHost {
constructor(protected serviceProvider: ServiceProvider) {
super();
}
override get kind(): HostKind {
return HostKind.FullAccess;
}
static createHost(kind: HostKind, serviceProvider: ServiceProvider) {
switch (kind) {
case HostKind.NoAccess:
return new NoAccessHost();
case HostKind.LimitedAccess:
return new LimitedAccessHost();
case HostKind.FullAccess:
return new FullAccessHost(serviceProvider);
default:
assertNever(kind);
}
}
override getPythonSearchPaths(pythonPath?: Uri, logInfo?: string[]): PythonPathResult {
const importFailureInfo = logInfo ?? [];
let result = this._executePythonInterpreter(pythonPath?.getFilePath(), (p) =>
this._getSearchPathResultFromInterpreter(p, importFailureInfo)
);
if (!result) {
result = {
paths: [],
prefix: undefined,
};
}
importFailureInfo.push(`Received ${result.paths.length} paths from interpreter`);
result.paths.forEach((path) => {
importFailureInfo.push(` ${path}`);
});
return result;
}
override getPythonVersion(pythonPath?: Uri, logInfo?: string[]): PythonVersion | undefined {
const importFailureInfo = logInfo ?? [];
try {
const execOutput = this._executePythonInterpreter(pythonPath?.getFilePath(), (p) =>
this._executeCodeInInterpreter(p, ['-I'], extractVersion)
);
const versionJson: any[] = JSON.parse(execOutput!);
if (!Array.isArray(versionJson) || versionJson.length < 5) {
importFailureInfo.push(`Python version ${execOutput} from interpreter is unexpected format`);
return undefined;
}
const version = PythonVersion.create(
versionJson[0],
versionJson[1],
versionJson[2],
versionJson[3],
versionJson[4]
);
if (version === undefined) {
importFailureInfo.push(`Python version ${execOutput} from interpreter is unsupported`);
return undefined;
}
return version;
} catch {
importFailureInfo.push('Unable to get Python version from interpreter');
return undefined;
}
}
override runScript(
pythonPath: Uri | undefined,
script: Uri,
args: string[],
cwd: Uri,
token: CancellationToken
): Promise<ScriptOutput> {
// If it is already cancelled, don't bother to run script.
throwIfCancellationRequested(token);
// What to do about conda here?
return new Promise<{ stdout: string; stderr: string }>((resolve, reject) => {
let stdout = '';
let stderr = '';
const commandLineArgs = ['-I', script.getFilePath(), ...args];
const child = this._executePythonInterpreter(pythonPath?.getFilePath(), (p) =>
child_process.spawn(p, commandLineArgs, {
cwd: cwd.getFilePath(),
shell: this.shouldUseShellToRunInterpreter(p),
})
);
const tokenWatch = onCancellationRequested(token, () => {
if (child) {
try {
if (child.pid && child.exitCode === null) {
if (process.platform === 'win32') {
// Windows doesn't support SIGTERM, so execute taskkill to kill the process
child_process.execSync(`taskkill /pid ${child.pid} /T /F > NUL 2>&1`);
} else {
process.kill(child.pid);
}
}
} catch {
// Ignore.
}
}
reject(new OperationCanceledException());
});
if (child) {
child.stdout.on('data', (d) => (stdout = stdout.concat(d)));
child.stderr.on('data', (d) => (stderr = stderr.concat(d)));
child.on('error', (e) => {
tokenWatch.dispose();
reject(e);
});
child.on('exit', () => {
tokenWatch.dispose();
resolve({ stdout, stderr });
});
} else {
tokenWatch.dispose();
reject(new Error(`Cannot start python interpreter with script ${script}`));
}
});
}
protected shouldUseShellToRunInterpreter(interpreterPath: string): boolean {
// Windows bat/cmd files must me executed with the shell due to the following breaking change:
// https://nodejs.org/en/blog/vulnerability/april-2024-security-releases-2#command-injection-via-args-parameter-of-child_processspawn-without-shell-option-enabled-on-windows-cve-2024-27980---high
return (
process.platform === 'win32' &&
!!getAnyExtensionFromPath(interpreterPath, ['.bat', '.cmd'], /* ignoreCase */ true)
);
}
private _executePythonInterpreter<T>(
pythonPath: string | undefined,
execute: (path: string) => T | undefined
): T | undefined {
if (pythonPath) {
return execute(pythonPath);
} else {
let result: T | undefined;
try {
// On non-Windows platforms, always default to python3 first. We want to
// avoid this on Windows because it might invoke a script that displays
// a dialog box indicating that python can be downloaded from the app store.
if (process.platform !== 'win32') {
result = execute('python3');
}
} catch {
// Ignore failure on python3
}
if (result !== undefined) {
return result;
}
// On some platforms, 'python3' might not exist. Try 'python' instead.
return execute('python');
}
}
/**
* Executes a chunk of Python code via the provided interpreter and returns the output.
* @param interpreterPath Path to interpreter.
* @param commandLineArgs Command line args for interpreter other than the code to execute.
* @param code Code to execute.
*/
private _executeCodeInInterpreter(interpreterPath: string, commandLineArgs: string[], code: string): string {
const useShell = this.shouldUseShellToRunInterpreter(interpreterPath);
if (useShell) {
code = '"' + code + '"';
}
commandLineArgs.push('-c', code);
const execOutput = child_process.execFileSync(interpreterPath, commandLineArgs, {
encoding: 'utf8',
shell: useShell,
});
return execOutput;
}
private _getSearchPathResultFromInterpreter(
interpreterPath: string,
importFailureInfo: string[]
): PythonPathResult | undefined {
const result: PythonPathResult = {
paths: [],
prefix: undefined,
};
try {
importFailureInfo.push(`Executing interpreter: '${interpreterPath}'`);
const execOutput = this._executeCodeInInterpreter(interpreterPath, [], extractSys);
const caseDetector = this.serviceProvider.get(ServiceKeys.caseSensitivityDetector);
// Parse the execOutput. It should be a JSON-encoded array of paths.
try {
const execSplit = JSON.parse(execOutput);
for (let execSplitEntry of execSplit.path) {
execSplitEntry = execSplitEntry.trim();
if (execSplitEntry) {
const normalizedPath = normalizePath(execSplitEntry);
const normalizedUri = Uri.file(normalizedPath, caseDetector);
// Skip non-existent paths and broken zips/eggs.
if (
this.serviceProvider.fs().existsSync(normalizedUri) &&
isDirectory(this.serviceProvider.fs(), normalizedUri)
) {
result.paths.push(normalizedUri);
} else {
importFailureInfo.push(`Skipping '${normalizedPath}' because it is not a valid directory`);
}
}
}
result.prefix = Uri.file(execSplit.prefix, caseDetector);
if (result.paths.length === 0) {
importFailureInfo.push(`Found no valid directories`);
}
} catch (err) {
importFailureInfo.push(`Could not parse output: '${execOutput}'`);
throw err;
}
} catch {
return undefined;
}
return result;
}
}