Skip to content

Commit 1e33952

Browse files
committed
improve command for getting sys path to resolve security issue
1 parent ac66899 commit 1e33952

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

src/client/common/utils/pythonUtils.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import { execFileSync } from 'child_process';
22

33
export function getSysPath(pythonCmd = 'python3'): string[] {
4+
// cleanSysPathCommand removes the working directory from sys.path.
5+
// The -c flag adds it automatically, which can allow some stdlib
6+
// modules (like json) to be overridden by other files (like json.py).
7+
const cleanSysPathCommand = [
8+
'import os, os.path, sys',
9+
'normalize = lambda p: os.path.normcase(os.path.normpath(p))',
10+
'cwd = normalize(os.getcwd())',
11+
'orig_sys_path = [p for p in sys.path if p != ""]',
12+
'sys.path[:] = [p for p in sys.path if p != "" and normalize(p) != cwd]',
13+
'import sys, json',
14+
'print(json.dumps(sys.path))',
15+
].join('; ');
416
try {
5-
const out = execFileSync(pythonCmd, ['-c', 'import sys, json; print(json.dumps(sys.path))'], {
17+
const out = execFileSync(pythonCmd, ['-c', cleanSysPathCommand], {
618
encoding: 'utf-8',
719
});
820
return JSON.parse(out);

0 commit comments

Comments
 (0)