Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a4f57ab

Browse files
authored
Merge pull request #31545 from ksunden/backport-of-pr-31282-on-v3.10.x
Backport PR #31282 on branch v3.10.x (SEC: Block shell escapes in latex and ps commands)
2 parents 063288d + acc6024 commit a4f57ab

4 files changed

Lines changed: 11 additions & 8 deletions

File tree

lib/matplotlib/backends/backend_pgf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def _setup_latex_process(self, *, expect_reply=True):
281281
# it.
282282
try:
283283
self.latex = subprocess.Popen(
284-
[mpl.rcParams["pgf.texsystem"], "-halt-on-error"],
284+
[mpl.rcParams["pgf.texsystem"], "-halt-on-error", "-no-shell-escape"],
285285
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
286286
encoding="utf-8", cwd=self.tmpdir)
287287
except FileNotFoundError as err:
@@ -848,7 +848,7 @@ def print_pdf(self, fname_or_fh, *, metadata=None, **kwargs):
848848
texcommand = mpl.rcParams["pgf.texsystem"]
849849
cbook._check_and_log_subprocess(
850850
[texcommand, "-interaction=nonstopmode", "-halt-on-error",
851-
"figure.tex"], _log, cwd=tmpdir)
851+
"-no-shell-escape", "figure.tex"], _log, cwd=tmpdir)
852852
with ((tmppath / "figure.pdf").open("rb") as orig,
853853
cbook.open_file_cm(fname_or_fh, "wb") as dest):
854854
shutil.copyfileobj(orig, dest) # copy file contents to target
@@ -965,7 +965,7 @@ def _run_latex(self):
965965
tex_source.write_bytes(self._file.getvalue())
966966
cbook._check_and_log_subprocess(
967967
[texcommand, "-interaction=nonstopmode", "-halt-on-error",
968-
tex_source],
968+
"-no-shell-escape", tex_source],
969969
_log, cwd=tmpdir)
970970
shutil.move(tex_source.with_suffix(".pdf"), self._output_name)
971971

lib/matplotlib/backends/backend_ps.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,8 +1257,9 @@ def _convert_psfrags(tmppath, psfrags, paper_width, paper_height, orientation):
12571257

12581258
with TemporaryDirectory() as tmpdir:
12591259
psfile = os.path.join(tmpdir, "tmp.ps")
1260+
# -R1 is a security flag used to prevent shell command execution
12601261
cbook._check_and_log_subprocess(
1261-
['dvips', '-q', '-R0', '-o', psfile, dvifile], _log)
1262+
['dvips', '-q', '-R1', '-o', psfile, dvifile], _log)
12621263
shutil.move(psfile, tmppath)
12631264

12641265
# check if the dvips created a ps in landscape paper. Somehow,
@@ -1302,7 +1303,7 @@ def gs_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
13021303

13031304
cbook._check_and_log_subprocess(
13041305
[mpl._get_executable_info("gs").executable,
1305-
"-dBATCH", "-dNOPAUSE", "-r%d" % dpi, "-sDEVICE=ps2write",
1306+
"-dBATCH", "-dNOPAUSE", "-dSAFER", "-r%d" % dpi, "-sDEVICE=ps2write",
13061307
*paper_option, f"-sOutputFile={psfile}", tmpfile],
13071308
_log)
13081309

@@ -1346,6 +1347,7 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
13461347
# happy (https://ghostscript.com/doc/9.56.1/Use.htm#MS_Windows).
13471348
cbook._check_and_log_subprocess(
13481349
["ps2pdf",
1350+
"-dSAFER",
13491351
"-dAutoFilterColorImages#false",
13501352
"-dAutoFilterGrayImages#false",
13511353
"-sAutoRotatePages#None",

lib/matplotlib/testing/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ def _check_for_pgf(texsystem):
164164
""", encoding="utf-8")
165165
try:
166166
subprocess.check_call(
167-
[texsystem, "-halt-on-error", str(tex_path)], cwd=tmpdir,
167+
[texsystem, "-halt-on-error", "-no-shell-escape",
168+
str(tex_path)], cwd=tmpdir,
168169
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
169170
except (OSError, subprocess.CalledProcessError):
170171
return False

lib/matplotlib/texmanager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ def make_dvi(cls, tex, fontsize):
291291
Path(tmpdir, "file.tex").write_text(
292292
cls._get_tex_source(tex, fontsize), encoding='utf-8')
293293
cls._run_checked_subprocess(
294-
["latex", "-interaction=nonstopmode", "--halt-on-error",
295-
"file.tex"], tex, cwd=tmpdir)
294+
["latex", "-interaction=nonstopmode", "-halt-on-error",
295+
"-no-shell-escape", "file.tex"], tex, cwd=tmpdir)
296296
Path(tmpdir, "file.dvi").replace(dvifile)
297297
# Also move the tex source to the main cache directory, but
298298
# only for backcompat.

0 commit comments

Comments
 (0)