SEC: Block shell escapes in latex and ps commands by scottshambaugh · Pull Request #31282 · matplotlib/matplotlib · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/matplotlib/backends/backend_pgf.py
6 changes: 4 additions & 2 deletions lib/matplotlib/backends/backend_ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,8 +1256,9 @@ def _convert_psfrags(tmppath, psfrags, paper_width, paper_height, orientation):

with TemporaryDirectory() as tmpdir:
psfile = os.path.join(tmpdir, "tmp.ps")
# -R1 is a security flag used to prevent shell command execution
cbook._check_and_log_subprocess(
['dvips', '-q', '-R0', '-o', psfile, dvifile], _log)
['dvips', '-q', '-R1', '-o', psfile, dvifile], _log)
Comment thread
scottshambaugh marked this conversation as resolved.
shutil.move(psfile, tmppath)

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

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

Expand Down Expand Up @@ -1345,6 +1346,7 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
# happy (https://ghostscript.com/doc/9.56.1/Use.htm#MS_Windows).
cbook._check_and_log_subprocess(
["ps2pdf",
"-dSAFER",
"-dAutoFilterColorImages#false",
"-dAutoFilterGrayImages#false",
"-sAutoRotatePages#None",
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ def _check_for_pgf(texsystem):
""", encoding="utf-8")
try:
subprocess.check_call(
[texsystem, "-halt-on-error", str(tex_path)], cwd=tmpdir,
[texsystem, "-halt-on-error", "-no-shell-escape",
str(tex_path)], cwd=tmpdir,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except (OSError, subprocess.CalledProcessError):
return False
Expand Down
9 changes: 5 additions & 4 deletions lib/matplotlib/tests/test_dviread.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ def test_dviread(tmp_path, engine, monkeypatch):
shutil.copy(dirpath / "test.tex", tmp_path)
shutil.copy(cbook._get_data_path("fonts/ttf/DejaVuSans.ttf"), tmp_path)
cmd, fmt = {
"pdflatex": (["latex"], "dvi"),
"xelatex": (["xelatex", "-no-pdf"], "xdv"),
"lualatex": (["lualatex", "-output-format=dvi"], "dvi"),
"pdflatex": (["latex", "-no-shell-escape"], "dvi"),
"xelatex": (["xelatex", "-no-pdf", "-no-shell-escape"], "xdv"),
"lualatex": (["lualatex", "-output-format=dvi", "-no-shell-escape"], "dvi"),
}[engine]
if shutil.which(cmd[0]) is None:
pytest.skip(f"{cmd[0]} is not available")
Expand Down Expand Up @@ -119,7 +119,8 @@ def test_dviread_pk(tmp_path):
\end{document}
""")
subprocess_run_for_testing(
["latex", "test.tex"], cwd=tmp_path, check=True, capture_output=True)
["latex", "-no-shell-escape", "test.tex"],
cwd=tmp_path, check=True, capture_output=True)
with dr.Dvi(tmp_path / "test.dvi", None) as dvi:
pages = [*dvi]
data = [
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/texmanager.py
Loading