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

Skip to content

Commit dec7e56

Browse files
Block shell escapes in latex and dvips commands
1 parent 9d83ca6 commit dec7e56

4 files changed

Lines changed: 13 additions & 10 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
@@ -1256,8 +1256,9 @@ def _convert_psfrags(tmppath, psfrags, paper_width, paper_height, orientation):
12561256

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

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

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

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

lib/matplotlib/tests/test_dviread.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ def test_dviread(tmp_path, engine, monkeypatch):
6969
shutil.copy(dirpath / "test.tex", tmp_path)
7070
shutil.copy(cbook._get_data_path("fonts/ttf/DejaVuSans.ttf"), tmp_path)
7171
cmd, fmt = {
72-
"pdflatex": (["latex"], "dvi"),
73-
"xelatex": (["xelatex", "-no-pdf"], "xdv"),
74-
"lualatex": (["lualatex", "-output-format=dvi"], "dvi"),
72+
"pdflatex": (["latex", "--no-shell-escape"], "dvi"),
73+
"xelatex": (["xelatex", "-no-pdf", "--no-shell-escape"], "xdv"),
74+
"lualatex": (["lualatex", "-output-format=dvi", "--no-shell-escape"], "dvi"),
7575
}[engine]
7676
if shutil.which(cmd[0]) is None:
7777
pytest.skip(f"{cmd[0]} is not available")
@@ -119,7 +119,8 @@ def test_dviread_pk(tmp_path):
119119
\end{document}
120120
""")
121121
subprocess_run_for_testing(
122-
["latex", "test.tex"], cwd=tmp_path, check=True, capture_output=True)
122+
["latex", "--no-shell-escape", "test.tex"],
123+
cwd=tmp_path, check=True, capture_output=True)
123124
with dr.Dvi(tmp_path / "test.dvi", None) as dvi:
124125
pages = [*dvi]
125126
data = [

lib/matplotlib/texmanager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def make_dvi(cls, tex, fontsize):
300300
cls._get_tex_source(tex, fontsize), encoding='utf-8')
301301
cls._run_checked_subprocess(
302302
["latex", "-interaction=nonstopmode", "--halt-on-error",
303-
"file.tex"], tex, cwd=tmpdir)
303+
"--no-shell-escape", "file.tex"], tex, cwd=tmpdir)
304304
Path(tmpdir, "file.dvi").replace(dvipath)
305305
# Also move the tex source to the main cache directory, but
306306
# only for backcompat.

0 commit comments

Comments
 (0)