Skip to content

Commit 976352e

Browse files
committed
src/sage/misc/latex.py: replace tmp_dir()
These bits of code only use one temporary file, but they call a function _run_latex_() that writes its output file to the same directory as its input file. Since we don't want the output path to be predictable (for security reasons), we have to "hide" the one temporary file inside of a temporary directory that will only be writable by the Sage user. In other words: standard tempfile.TemporaryDirecrory() replacement. Issue: #36322
1 parent 2f1a76d commit 976352e

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/sage/misc/latex.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
import re
3131
import shutil
3232
from subprocess import call, PIPE
33+
from tempfile import TemporaryDirectory
3334

3435
from sage.misc.cachefunc import cached_function, cached_method
35-
from sage.misc.temporary_file import tmp_dir
3636
from sage.structure.sage_object import SageObject
3737

3838
from sage.misc.lazy_import import lazy_import
@@ -1103,7 +1103,8 @@ def eval(self, x, globals, strip=False, filename=None, debug=None,
11031103
filename = 'sage%s' % random.randint(1, 100) # to defeat browser caches
11041104
else:
11051105
filename = os.path.splitext(filename)[0] # get rid of extension
1106-
base = tmp_dir()
1106+
1107+
base = TemporaryDirectory()
11071108
orig_base, filename = os.path.split(os.path.abspath(filename))
11081109
if len(filename.split()) > 1:
11091110
raise ValueError("filename must contain no spaces")
@@ -1134,13 +1135,14 @@ def eval(self, x, globals, strip=False, filename=None, debug=None,
11341135
engine = self.__engine
11351136
e = _run_latex_(os.path.join(base, filename + ".tex"), debug=debug,
11361137
density=density, engine=engine, png=True)
1138+
result = None
11371139
if e.find("Error") == -1:
11381140
shutil.copy(os.path.join(base, filename + ".png"),
11391141
os.path.join(orig_base, filename + ".png"))
1140-
shutil.rmtree(base)
1141-
return ''
1142-
else:
1143-
return
1142+
result = ''
1143+
1144+
base.cleanup()
1145+
return result
11441146

11451147
def blackboard_bold(self, t=None):
11461148
r"""nodetex
@@ -1918,7 +1920,7 @@ def view(objects, title='Sage', debug=False, sep='', tiny=False,
19181920
if pdflatex or (viewer == "pdf" and engine == "latex"):
19191921
engine = "pdflatex"
19201922
# command line or notebook with viewer
1921-
tmp = tmp_dir('sage_viewer')
1923+
tmp = TemporaryDirectory()
19221924
tex_file = os.path.join(tmp, "sage.tex")
19231925
with open(tex_file, 'w') as file:
19241926
file.write(s)
@@ -1931,6 +1933,7 @@ def view(objects, title='Sage', debug=False, sep='', tiny=False,
19311933
viewer = dvi_viewer()
19321934
else:
19331935
print("Latex error")
1936+
tmp.cleanup()
19341937
return
19351938
output_file = os.path.join(tmp, "sage." + suffix)
19361939
# this should get changed if we switch the stuff in misc.viewer to
@@ -1939,6 +1942,8 @@ def view(objects, title='Sage', debug=False, sep='', tiny=False,
19391942
print('viewer: "{}"'.format(viewer))
19401943
call('%s %s' % (viewer, output_file), shell=True,
19411944
stdout=PIPE, stderr=PIPE)
1945+
1946+
tmp.cleanup()
19421947
return
19431948

19441949

@@ -1990,7 +1995,7 @@ def png(x, filename, density=150, debug=False,
19901995
# path name for permanent png output
19911996
abs_path_to_png = os.path.abspath(filename)
19921997
# temporary directory to store stuff
1993-
tmp = tmp_dir('sage_viewer')
1998+
tmp = TemporaryDirectory()
19941999
tex_file = os.path.join(tmp, "sage.tex")
19952000
png_file = os.path.join(tmp, "sage.png")
19962001
# write latex string to file
@@ -2004,6 +2009,8 @@ def png(x, filename, density=150, debug=False,
20042009
shutil.copy(png_file, abs_path_to_png)
20052010
else:
20062011
print("Latex error")
2012+
2013+
tmp.cleanup()
20072014
if debug:
20082015
return s
20092016
return

0 commit comments

Comments
 (0)