Executive brief
KaTeX is a popular JavaScript library for rendering mathematical expressions. Attackers can craft malicious mathematical expressions using the \edef command that cause the library to enter a near-infinite loop and consume excessive memory, despite the maxExpand safety limit being configured. This can render a web application unresponsive, disrupting service for all users viewing the affected content.
Technical details
KaTeX implements a maxExpand option to prevent infinite recursion in macro expansion from consuming all memory and causing stack overflow. The vulnerability exists because maxExpand counts "a single macro expanding to any number of tokens" as one expansion unit. The \edef (expand-and-define) TeX command can exponentially multiply tokens using only a linear number of expansions—for example, by repeatedly doubling a definition. An attacker providing untrusted mathematical expressions can exploit this to bypass maxExpand protection, causing denial of service via memory overflow, main thread blocking, or stack overflow. The vulnerability requires the attacker to be able to supply rendering input to a KaTeX instance. The fix, released in KaTeX v0.16.10, counts every expanded token within \edef as a separate expansion against the maxExpand limit.
Affected products
- KaTeX KaTeX >= v0.10.0-beta, < 0.16.10
Timeline
- 2024-03-25: disclosed: GHSA-64fm-8hw2-v72w published
- 2024-03-25: patched: KaTeX v0.16.10 released with fix