In general, achieving security purely through escaping command sequences is hard to do without drastically reducing expressivity, since it there is no principled way to distinguish safe cs's from unsafe ones: Tex is just not a clean enough programming language to allow this. I'd say abandon this approach in favour of eliminating the existence of security holes.
Veger's summary of the security holes in Latex conforms with mine: i.e., the issues are shell escapes and file creation.overwriting, though he has missed a shell escape vulnerability. Some additional points follow, then some recommendations:
- It is not enough to avoid actively invoking
--shell-escape
, since it can be implicitly enabled in texmf.cnf. You should explicitly pass --no-shell-escape
to override texmf.cnf;
\write18
is a primitive of Etex, not Knuth's Tex. So you can avoid Latexes that implement it (which, unfortunately, is most of them);
- If you are using Dvips, there is another risk:
\special
commands can create .dvi files that ask dvips to execute shell commands. So you should, if you use dvips, pass the -R2
command to forbid invoking of shell commands;
- texmf.cnf allows you to specify where Tex can create files;
- You might not be able to avoid disabling creation of fonts if you want your clients much freedom in which fonts they may create. Take a look at the notes on security for Kpathsea; the default behaviour seems reasonable to me, but you could have a per user font tree, to prevent one user stepping on another users toes.
Options:
- Sandbox your client's Latex invocations, and allow them freedom to misbehave in the sandbox;
- Trust in kpathsea's defaults, and forbid shell escapes in latex and any other executables used to build the PDF output;
- Drastically reduce expressivity, forbidding your clients the ability to create font files or any new client-specified files. Run latex as a process that can only write to certain already existing files;
- You can create a format file in which the
\write18
cs, and the file creation css, are not bound, and only macros that invoke them safely, such as for font/toc/bbl creation, exist. This means you have to decide what functionality your clients have: they would not be able to freely choose which packages they import, but must make use of the choices you have imposed on them. Depending on what kind of 'templates' you have in mind, this could be a good option, allowing use of packages that use shell escapes, but you will need to audit the Tex/Latex code that goes into your format file.
Postscript
There's a TUGBoat article, Server side PDF generation based on LATEX templates, addressing another take on the question to the one I have taken, namely generating PDFs from form input using Latex.