Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why are annotation and semantics MathML tags forbidden? #673

Closed
edemaine opened this issue Apr 22, 2022 · 4 comments
Closed

Why are annotation and semantics MathML tags forbidden? #673

edemaine opened this issue Apr 22, 2022 · 4 comments

Comments

@edemaine
Copy link

This issue proposes a feature, to move the <annotation> and <semantics> MathML tags from the forbidden to allowed list.

Background & Context

KaTeX generates MathML with <annotation> and <semantics> tags, which include the original LaTeX source code.

Is there a security risk I'm missing in semantics and annotation?

Desired behavior

Input

Example generated by KaTeX on input x:

<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><mi>x</mi></mrow><annotation encoding="application/x-tex">x</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height: 0.4306em;"></span><span class="mord mathnormal">x</span></span></span></span>

Given output

<span class="katex"><span class="katex-mathml"><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>x</mi></mrow>x</math></span><span aria-hidden="true" class="katex-html"><span class="base"><span style="height: 0.4306em;" class="strut"></span><span class="mord mathnormal">x</span></span></span></span>

Expected output

Same as input.

@is2ei
Copy link
Contributor

is2ei commented Apr 24, 2022

@edemaine

It seems the reason is partly explained in the code below.

https://github.com/cure53/DOMPurify/blob/main/src/tags.js#L197-L260

// List of SVG elements that are disallowed by default.
// We still need to know them so that we can do namespace
// checks properly in case one wants to add them to
// allow-list.
...
// Similarly to SVG, we want to know all MathML elements,
// even those that we disallow by default.
export const mathMlDisallowed = freeze([
  'maction',
  'maligngroup',
  'malignmark',
  'mlongdiv',
  'mscarries',
  'mscarry',
  'msgroup',
  'mstack',
  'msline',
  'msrow',
  'semantics',
  'annotation',
  'annotation-xml',
  'mprescripts',
  'none',
]);

@cure53
Copy link
Owner

cure53 commented Apr 24, 2022

They cause mXSS and hence need to be handled with great care. See here https://github.com/cure53/DOMPurify/blob/main/test/fixtures/expect.js#L1094

You can of course allow those tags via config if this is not a risk for you, but by default, we have to prohibit them, sadly.

@cure53 cure53 closed this as completed Apr 24, 2022
@edemaine
Copy link
Author

Thank you for the explanation! Definitely makes sense not to allow them in general then. This may not be the right forum, but I'd like to know more about the vector, in order to protect my site but allow the form generated by KaTeX.

  1. Do you know whether the same exploit is possible with <annotation> as opposed to <annotation-xml>?
  2. Perhaps it's safe to allow <annotation encoding="application/x-tex">, i.e., for me to check/force the encoding attribute? MDN says only certain encodings are rendered, and this is not among them, so I'm hoping it's safe in this situation. (Perhaps any encoding that's not text/html or maybe image/svg+xml is safe.)

@cure53
Copy link
Owner

cure53 commented Apr 25, 2022

That is a good question and we'd probably have to try it out and see what browsers do in that regard.

(Perhaps any encoding that's not text/html or maybe image/svg+xml is safe.)

My guess is that you are right here, but I frankly don't know for sure :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants