Skip to content

Increase maxExpand option for KaTeX / LaTeX / math rendering

What does this MR do and why?

Currently the KaTeX maxExpand is set to 20, which is very low. This inhibits complex math from being displayed. For example, just using \mod e three times, by itself, is enough to go over the limit.

```math
\mod e
\mod e
\mod e
```

The default for the option is 1000, which was changed by this PR.

We have a limit on the number of characters in a particular block (MAX_MATH_CHARS) and a limit on the amount of time all math blocks can be rendered (MAX_RENDER_TIME_MS).

A math block of 142 \mod e is just under the 1000 character limit. 143 actually puts is over the expansion limit. Rendering is fast.

Because we have both a character and rendering time limit across all blocks, it should be safe to increase the maxExpand to 1000. In addition we can add an error message indicating to the user to use multiple blocks if the expansion limit is reached.

Interestingly, the pathological case detailed in gitlab-foss#61410 (closed), which uses \sqrt, is not effected by the expansion limit. character and rendering time limits continue to protect against DoS.

How to set up and validate locally

  1. Paste the following markdown in a description or comment and click Preview. The math formula should display

    ```math
    \begin{aligned}
       ed &= 1 \mod \phi(N)\\
     \implies ed &= k \phi(N) + 1\\
     \implies k \phi(N) + 1 &= 0 \mod e\\
     \implies k (N + 1 - p - q) + 1 &= 0 \mod e\\
     \implies 2k \left(\frac{N + 1}2 + \frac{-p-q}2\right) + 1 &= 0 \mod e
    \end{aligned}
    ```
  2. Use the file expansion_limit.md to insert into an issue/comment, you should get the error There were too many expansions, consider using multiple math blocks

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #328103 (closed)

Edited by Brett Walker

Merge request reports