Skip to content

Commit ff7ed3e

Browse files
Quote packages in pip install to avoid shell expansion (#529)
* Quote packages in pip install to avoid shell expansion Without quotes, pip installs with versioning that includes characters '<', '>', or '|' result in shell expansion and creation of unwanted files Signed-off-by: Dylan Eustice <deustice@nvidia.com> * Update unit tests to expect quotes * Use shlex_quotes from six.moves instead of f-string and update unit tests Signed-off-by: Dylan Eustice <deustice@nvidia.com> --------- Signed-off-by: Dylan Eustice <deustice@nvidia.com>
1 parent 2d490e7 commit ff7ed3e

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

hpccm/building_blocks/pip.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from packaging.version import Version
2525
import logging
2626
import posixpath
27+
from six.moves import shlex_quote
2728

2829
import hpccm.config
2930
import hpccm.templates.rm
@@ -165,6 +166,8 @@ def __instructions(self):
165166
posixpath.basename(self.__requirements))]))
166167

167168
if self.__packages:
169+
# Quote the packages to avoid shell expansion
170+
quoted_packages = [shlex_quote(pkg) for pkg in self.__packages]
168171
cmds.append('{0} install {1}'.format(self.__pip,
169-
' '.join(self.__packages)))
172+
' '.join(quoted_packages)))
170173
self += shell(commands=cmds)

test/test_pip.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,12 @@ def test_upgrade(self):
166166
rm -rf /var/lib/apt/lists/*
167167
RUN pip --no-cache-dir install --upgrade "pip < 21.0" && \
168168
pip --no-cache-dir install hpccm''')
169+
170+
@ubuntu
171+
@docker
172+
def test_package_with_version(self):
173+
"""package with version specifier is quoted"""
174+
p = pip(ospackages=[], packages=['hpccm>=1.0'])
175+
self.assertEqual(str(p),
176+
r"""# pip
177+
RUN pip --no-cache-dir install 'hpccm>=1.0'""")

0 commit comments

Comments
 (0)