Security
TeX that comes from users is untrusted input. Temml escapes what it writes into markup, and commands that can load a resource or set an HTML attribute are blocked unless you allow them.
Commands that need trust
Section titled “Commands that need trust”| Command | Trust context |
|---|---|
\includegraphics |
Command, Url, Protocol |
\class |
Command, Class |
\id |
Command, Id |
\style |
Command, Style |
\data |
Command, Attributes |
Trust is false by default. A blocked command is a parse error: error markup, or a ParseError with ThrowOnError. Trust = true allows all of them, which is only right for TeX you wrote yourself. Strict disables \class, \id, \style, and \data even when trusted.
Allow some of them
Section titled “Allow some of them”Set TrustHandler to decide per command. When it is set, it decides instead of Trust.
var selective = new TemmlOptions{ // Allow images over https only. Every other command that needs trust stays blocked. TrustHandler = context => context.Command == @"\includegraphics" && context.Protocol == "https",};
Console.WriteLine( TemmlRenderer.RenderToString(@"\includegraphics{https://example.com/plot.png}", selective));For a URL, Protocol is the lowercased scheme, or _relative when the URL has none. A URL whose scheme cannot be read is rejected before the handler runs. List the protocols you allow; do not list the ones you refuse, so that javascript: and data: stay out.
\style accepts any CSS, which can cover other parts of your page. \id and \class can collide with names your page or scripts use.
In numbered and tagged equations, \label sets an element id from the TeX, and it needs no trust. Keep that in mind if your scripts look elements up by id.
The port is stricter than upstream about the characters it accepts in \includegraphics and \data values.
Resource limits
Section titled “Resource limits”MaxSizecaps sizes such as those in\rule{500em}{500em}. It is unlimited by default.MaxExpandcaps macro expansions, 1000 by default, which stops recursive macros.- Input nested too deeply is rejected with the parse error
Maximum nesting depth exceeded, so it cannot overflow the stack. - Limit the length of the input yourself. Temml has no option for it.
var options = new TemmlOptions{ Trust = false, // the default MaxSize = (10, 100), // cap user-specified sizes: 10 em, 100 pt MaxExpand = 100, // cap macro expansions};
Console.WriteLine(TemmlRenderer.RenderToString(@"\rule{500em}{500em}", options));Macros
Section titled “Macros”\gdef writes into the Macros dictionary of the options. Do not share one dictionary between users, or one user can redefine commands in another user’s math. DefineMacro and DefineSymbol change global tables, so never pass user input to them.
Where the markup goes
Section titled “Where the markup goes”The output is HTML markup. Insert it into element content only, not into an attribute or a script. If your site runs a sanitizer afterwards, it has to keep the MathML elements and the class and style attributes, or the math will look wrong.
Reporting a problem
Section titled “Reporting a problem”Report a vulnerability privately through the security advisories of the repository.
MIT · Copyright 2020 Ron Kok · unofficial community port of Temml