Secure the shebang patcher against binaries

This commit is contained in:
Valentin Niess
2020-04-05 00:00:06 +02:00
parent 15a34b71ab
commit 27ad66b8af

View File

@@ -37,14 +37,18 @@ def patch_pip_install():
path = os.path.join(sys.prefix, 'bin', exe)
if (not os.path.isfile(path)) or (not os.access(path, os.X_OK)) or \
exe.startswith('python') or os.path.islink(path):
exe.startswith('python') or os.path.islink(path) or \
exe.endswith('.pyc') or exe.endswith('.pyo'):
continue
with open(path, 'r') as f:
header = f.read(2)
if header != '#!':
continue
content = f.read()
try:
with open(path, 'r') as f:
header = f.read(2)
if header != '#!':
continue
content = f.read()
except:
continue
shebang, body = content.split(os.linesep, 1)
shebang = shebang.split()