Skip to content

Commit ec47d3e

Browse files
committed
Escape bin_path
1 parent 215dc14 commit ec47d3e

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

readthedocs/doc_builder/environments.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ class DockerBuildCommand(BuildCommand):
270270
Build command to execute in docker container
271271
"""
272272

273+
bash_escape_re = re.compile(
274+
r"([\t\ \!\"\#\$\&\'\(\)\*\:\;\<\>\?\@"
275+
r'\[\\\]\^\`\{\|\}\~])'
276+
)
277+
273278
def __init__(self, *args, escape_command=True, **kwargs):
274279
"""
275280
Override default to extend behavior.
@@ -345,19 +350,16 @@ def get_wrapped_command(self):
345350
``escape_command=True`` in the init method this escapes a good majority
346351
of those characters.
347352
"""
348-
bash_escape_re = re.compile(
349-
r"([\t\ \!\"\#\$\&\'\(\)\*\:\;\<\>\?\@"
350-
r'\[\\\]\^\`\{\|\}\~])',
351-
)
352353
prefix = ''
353354
if self.bin_path:
354-
prefix += 'PATH={}:$PATH '.format(self.bin_path)
355+
bin_path = self._escape_command(self.bin_path)
356+
prefix += f'PATH={bin_path}:$PATH '
355357

356358
command = (
357-
' '.join([
358-
bash_escape_re.sub(r'\\\1', part) if self.escape_command else part
359+
' '.join(
360+
self._escape_command(part) if self.escape_command else part
359361
for part in self.command
360-
])
362+
)
361363
)
362364
return (
363365
"/bin/sh -c '{prefix}{cmd}'".format(
@@ -366,6 +368,10 @@ def get_wrapped_command(self):
366368
)
367369
)
368370

371+
def _escape_command(self, cmd):
372+
r"""Escape the command by prefixing suspicious chars with `\`."""
373+
return self.bash_escape_re.sub(r'\\\1', cmd)
374+
369375

370376
class BaseEnvironment:
371377

0 commit comments

Comments
 (0)