GHSL-2022-036: Arbitrary CSS injection in mermaid.js - CVE-2022-31108
mermaid.js did not sanitise theme variables such as textColor, allowing an attacker to inject arbitrary CSS into the generated diagram. This could let them exfiltrate sensitive information via CSS selectors that trigger HTTP requests, or alter the page to mislead users.
Originally disclosed on GitHub Security Lab
An attacker is able to inject arbitrary CSS into the generated graph allowing them to change the styling of elements outside of the generated graph, and potentially exfiltrate sensitive information by using specially crafted CSS selectors.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The getStyles function inserted theme variables directly into a CSS template without sanitising them, allowing an attacker to break out of the intended property value and add new CSS rules.
Impact
This could lead to information disclosure via CSS selectors that generate HTTP requests, and could let an attacker change the document to trick a user into unintended actions such as clicking a link.
What the attacker needed
Attacker controls mermaid graph definition; victim views the rendered diagram
The fix
The issue was fixed in commit 0ae1bdb by sanitising theme variables before they are inserted into CSS.
How it was found
- Create an HTML page that loads mermaid.js and calls mermaid.render with a graph definition.
- Include a themeVariables directive in the graph that sets textColor to a value containing a closing brace and a new CSS rule.
- Insert the rendered SVG into the page; the injected CSS rule then applies to elements outside the diagram.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
mermaid.js is a tool that turns text into diagrams on a web page. It let users set colours and other style options, but it did not check those values, so someone could sneak in extra CSS rules. CSS is the language that controls how web pages look; injected rules can change other parts of the page or make the browser send requests that leak information.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.