Skip to content

Commit 64855d1

Browse files
authored
run subshells in bundletool_experimental using subprocess API (#2866)
Fixes #2843 use `subprocess.check_call` instead of `os.system` to support stronger security guarantees and block environment variables from the running environment from affecting subprocesses. Fixes usage of rules_apple when using rules_python 1.7.0+.
1 parent 7bc20cc commit 64855d1

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

tools/bundletool/bundletool_experimental.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import os
5353
import shlex
5454
import shutil
55+
import subprocess
5556
import sys
5657
import zipfile
5758
from ctypes import CDLL, c_char_p, c_int, get_errno
@@ -290,10 +291,10 @@ def _post_process_bundle(self, bundle_root, post_processor):
290291
# Configure the TREE_ARTIFACT_OUTPUT environment variable to the path of the
291292
# bundle, but keep the work_dir for compatibility with the bundletool post
292293
# processing.
293-
exit_code = os.system('TREE_ARTIFACT_OUTPUT="%s" %s "%s"' %
294-
(bundle_root, post_processor, work_dir))
295-
if exit_code:
296-
raise PostProcessorError(exit_code)
294+
try:
295+
subprocess.check_call((post_processor, work_dir), env={"TREE_ARTIFACT_OUTPUT": bundle_root})
296+
except subprocess.CalledProcessError as e:
297+
raise PostProcessorError(e.returncode) from e
297298

298299
def _sign_bundle(self, bundle_root, command_lines):
299300
"""Executes the signing command lines on the bundle.
@@ -303,11 +304,12 @@ def _sign_bundle(self, bundle_root, command_lines):
303304
command_lines: A newline-separated list of command lines that should be
304305
executed in the bundle to sign it.
305306
"""
306-
exit_code = os.system(
307-
'WORK_DIR=%s\n%s' % (shlex.quote(bundle_root), command_lines)
308-
)
309-
if exit_code:
310-
raise CodeSignError(exit_code)
307+
for command in command_lines.splitlines():
308+
argv = [arg.replace('$WORK_DIR', bundle_root) for arg in shlex.split(command)]
309+
try:
310+
subprocess.check_call(argv, env={})
311+
except subprocess.CalledProcessError as e:
312+
raise CodeSignError(e.returncode) from e
311313

312314

313315
def _main(control_path):

0 commit comments

Comments
 (0)