TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to program a web interface for automated generation of calculus tests. The idea is to take some input from user, say number of problems and name of instructor, and then by means of php generate the latex file, process it with pdflatex and let user download it.

The procedure seems to be quite straightforward. But what if somebody enters for instructor name something like "Mr. Mackey \input{/etc/hosts}" ?

looks like one can retrieve any file accessible to web server.

How to prevent this? May be there are natural php ways? I am very new to php, and would appreciate any advice.

share|improve this question

migrated from stackoverflow.com Dec 14 '12 at 16:42

This question came from our site for professional and enthusiast programmers.

basically the same things you'd do to prevent SQL injection attacks need to be done to prevent Latex injection attacks - escape any latex metachars. – Marc B Dec 12 '12 at 21:29
Try the mentioned \include and you will see it fail. Please do a man pdftex and read the comments in texmf.cnf. And of course use chroot and ulimit. – Martin Schröder Dec 13 '12 at 7:46
David's answer here might help as well. :) – Paulo Cereda Dec 14 '12 at 16:48
So as not to be reinventing the wheel, you might want to look into LON-CAPA. – Alan Munn Dec 14 '12 at 16:54
Under bash, a command line such as openin_any=p pdflatex file works as well. – egreg Dec 14 '12 at 16:59

As Paulo wrote, David's answer here is the right solution.

Here's a magazine article which talks about some of the risks. There's an associated research article as well.

share|improve this answer
I am using at this point a simple routine that checks that the only latex expressions in the entered name are accents... I will use openin_any=p too. Thanks a lot. – user1898918 Dec 14 '12 at 20:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.