Security

What a document can and cannot do, and how to compile documents you did not write.

mLaTeX treats every document as untrusted input. Unlike a TeX distribution, it has no way to run code a document supplies outside mLaTeX's own language, so compiling a stranger's document is far less risky than with pdfLaTeX and --shell-escape.

What a document cannot do

Run programs

\write18, --shell-escape and every other way of running a program do not exist. A write to stream 18 is logged and nothing more.

Write files

\openout and \write to a file do nothing, and filecontents keeps its file in memory for that one run. The only file mLaTeX writes is the PDF, where the command line or the host program says.

Load TeX code from disk

.sty and .cls files are never read. Packages are compiled in, handed in by a program, or written as name/main.mtex folders in mLaTeX's own language.

Read outside its folders

Inputs, packages, pictures and bibliographies are read only from the document's own folder and the folders given with -I. Absolute paths and paths that climb out with .. are refused. A document handed in from memory can read only the folders and files its program provided.

Leave state behind

References, contents and bibliographies are resolved in memory, so no .aux file is left for a later run to trust.

What you need to handle

Network access

A document can make HTTP and HTTPS requests from the machine that compiles it, with \includegraphics{https://...}, \httpget{url} and \httppost{url}{body}. For documents you did not write, turn the network off:

mlatex --offline upload.tex
session.host.offline = true;
latex->offline(session, 1);

Each request is then refused and reported as an error, such as \httpget of 'https://example.com' refused: the run is offline, before anything is sent. A document cannot reach internal services or send data out. For defence in depth, also run mLaTeX somewhere without network access. The WebAssembly module makes no requests at all.

Time and memory

As with TeX, a document can loop forever or use a large amount of memory, and mLaTeX limits neither. A service that compiles untrusted documents should enforce its own timeout and memory limit: a job object on Windows, cgroups or ulimit on Linux and macOS.

Your own commands

A command your program defines runs with your program's privileges, and the document chooses its arguments. Validate them as you would any other untrusted input.

Libraries

Fonts and pictures are read by HarfBuzz, FreeType, libpng, libjpeg-turbo, libwebp and zlib. Keep them up to date through your vcpkg baseline or your system's packages.

A service that compiles uploads

mlatex -q --offline --halt-on-error -o out upload.tex

Run it with a time limit and a memory limit, in a folder that holds only the upload and what it needs, and serve out/upload.pdf only when the exit status is 0.

Reporting a vulnerability

Please report security issues privately, not in public issues: through GitHub's private vulnerability reporting on the repository, or as described in the project's SECURITY.md.