Skip to content

Commit 1e5f320

Browse files
authored
cherry-pick: fix: replace unsafe option for Popen (#2317) (#2319)
1 parent 44d6454 commit 1e5f320

3 files changed

Lines changed: 6 additions & 10 deletions

File tree

apiserver/paasng/paasng/platform/sourcectl/package/client.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ def read_file(self, filename) -> bytes:
144144
"""
145145
with generate_temp_dir() as temp_dir:
146146
p = subprocess.Popen(
147-
f'tar -xf "{self.filepath.absolute()}" -C "{temp_dir.absolute()}" "{filename}"',
148-
shell=True,
147+
["tar", "-xf", str(self.filepath.absolute()), "-C", str(temp_dir.absolute()), filename],
149148
stdout=subprocess.PIPE,
150149
stderr=subprocess.PIPE,
151150
encoding="utf-8",
@@ -204,8 +203,7 @@ def list(self, tarfile_like: bool = True) -> List[str]:
204203
might be corrupt.
205204
"""
206205
p = subprocess.Popen(
207-
f'tar -tf "{self.filepath.absolute()}"',
208-
shell=True,
206+
["tar", "-tf", str(self.filepath.absolute())],
209207
stdout=subprocess.PIPE,
210208
stderr=subprocess.PIPE,
211209
encoding="utf-8",

apiserver/paasng/paasng/platform/sourcectl/utils.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ def compress_directory(source_path, target_path):
151151
# Add "GZIP=-n" to disable gzip timestamp
152152
# see: https://serverfault.com/questions/110208/different-md5sums-for-same-tar-contents
153153
p = subprocess.Popen(
154-
f"GZIP=-n tar --exclude=.svn -czf {target_path} -C {source_path} .",
155-
shell=True,
154+
["tar", "--exclude=.svn", "-czf", str(target_path), "-C", str(source_path), "."],
156155
stdout=subprocess.PIPE,
157156
stderr=subprocess.PIPE,
157+
env={"GZIP": "-n"},
158158
encoding="utf-8",
159159
)
160160
_, stderr = p.communicate()
@@ -168,8 +168,7 @@ def uncompress_directory(source_path, target_path):
168168
target_path = os.path.abspath(target_path)
169169
# -m, --touch don't extract file modified time
170170
p = subprocess.Popen(
171-
f'tar -m -xf "{source_path}" -C "{target_path}"',
172-
shell=True,
171+
["tar", "-m", "-xf", str(source_path), "-C", str(target_path)],
173172
stdout=subprocess.PIPE,
174173
stderr=subprocess.PIPE,
175174
encoding="utf-8",

apiserver/paasng/paasng/utils/basic.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ def sha256_checksum(file_path):
6767
possible_names = ["sha256sum", "gsha256sum"]
6868
for sha256_bin in possible_names:
6969
p = subprocess.Popen(
70-
"%s %s" % (sha256_bin, file_path),
71-
shell=True,
70+
[sha256_bin, str(file_path)],
7271
stdout=subprocess.PIPE,
7372
stderr=subprocess.PIPE,
7473
encoding="utf-8",

0 commit comments

Comments
 (0)