Skip to content

Commit edbf03b

Browse files
committed
Simplifies alias search path sanitisation.
Fixes CVE-2026-5271
1 parent 4f10964 commit edbf03b

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/manage/aliasutils.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@
1010

1111
DEFAULT_SITE_DIRS = ["Lib\\site-packages", "Scripts"]
1212

13+
# Our script removes sys.path[0] if empty to avoid trivial search path hijacks.
14+
# In virtually all cases it should be the directory where our scripts are
15+
# generated, which has no importable packages (unless there are unauthorised
16+
# modifications, which are out of scope for our security threat model).
17+
# We don't try to be any more clever, since we don't know what kind of
18+
# interpreter we are running inside - this script may be generated for any
19+
# arbitrary executable installed by PyManager, and so it's possible that
20+
# sys.path[0] is already sanitised or entirely unrelated.
21+
1322
SCRIPT_CODE = """import sys
1423
15-
# Clear sys.path[0] if it contains this script.
16-
# Be careful to use the most compatible Python code possible.
1724
try:
18-
if sys.path[0]:
19-
if sys.argv[0].startswith(sys.path[0]):
20-
sys.path[0] = ""
21-
else:
22-
open(sys.path[0] + "/" + sys.argv[0], "rb").close()
23-
sys.path[0] = ""
24-
except OSError:
25-
pass
25+
if not sys.path[0]:
26+
del sys.path[0]
2627
except AttributeError:
2728
pass
2829
except IndexError:

0 commit comments

Comments
 (0)