Stored XSS in Mermaid Sandboxed Frame
GitLab's Mermaid diagram rendering allowed an attacker to inject HTML that bypassed Mermaid's DOM sanitisation, enabling JavaScript execution in a sandboxed iframe. An attacker could craft a malicious Mermaid diagram in a repository's README that, when clicked by a victim, could perform actions like stealing session tokens, though the sandbox and CSP limited the impact.
GitLab
Originally disclosed on GitLab
- Fixed in 18 days
- Disclosed after 18 days
HackerOne report #1529593 by debugit on 2022-04-03, assigned to GitLab Team:
Report | Attachments | How To Reproduce
Report
Summary
GitLab's Mermaid configuration allows an attacker to inject HTML in the rendered Markdown. Although, Mermaid has inbuild sanitization of DOM to prevent JS execution.This can be bypassed
Steps to reproduce
- Go to any repository such as
https://localhost/<project>/<repository> - Edit the README.md of the repository and put the following and commit the changes:
```mermaid
graph TD
A[Test] --> L["<a href='javascript#colon;document.write(atob`PGEgaHJlZj0jIG9uY2xpY2s9Ino9d2luZG93Lm9wZW4oJ2h0dHBzOi8vbG9jYWxob3N0Jyk7Ij5DTElDSyBNZQ==`)'>CLICK</a>"]
```
- On latest Safari browser, Click the hyperlink, which will rewrite the DOM by calling
document.writeand on clicking again will result in awindow.openas POC for XSS. Since theallow-modalsflag is not present we can't usealertor similar functions.

Note: This is also reproducible on Gitlab.com. However, along with Sandboxed Iframe, CSP Kicks in making the issue almost inexploitable for the time being.
What is the current bug behavior?
Mermaid markdown editor is supposed to sanitize the DOM for XSS. However, it was bypassed.
What is the expected correct behavior?
JavaScript should not have executed
Output of checks
This bug also exists on GitLab.com.
CSP Violation:

Results of GitLab environment info
GitLab information
Version: 14.9.2-ee
Revision: 3034418fb31
Directory: /opt/gitlab/embedded/service/gitlab-rails
DB Adapter: PostgreSQL
DB Version: 12.7
URL: https://gitlab.example.com
HTTP Clone URL: https://gitlab.example.com/some-group/some-project.git
SSH Clone URL: [email protected]:some-group/some-project.git
Elasticsearch: no
Geo: no
Using LDAP: no
Using Omniauth: yes
Omniauth Providers:
GitLab Shell
Version: 13.24.0
Repository storage paths:
- default: /var/opt/gitlab/git-data/repositories
GitLab Shell path: /opt/gitlab/embedded/service/gitlab-shell
Impact
The attacker-supplied code can perform a wide variety of actions, such as stealing the victim's session token or login credentials, logging their keystrokes, etc. Since the Iframe is sandboxed there is not a lot that could be done. Therefore, setting the severity to Low
Attachments
Warning: Attachments received through HackerOne, please exercise caution!
How To Reproduce
Please add reproducibility information to this section:
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
Likely the Mermaid sanitisation did not properly handle certain HTML entities or URL schemes, allowing a crafted link to execute JavaScript.
Impact
The attacker-supplied code could perform actions such as stealing session tokens or logging keystrokes, but the sandboxed iframe and CSP on GitLab.com made the issue almost inexploitable.
What the attacker needed
Victim uses Safari; victim clicks a link in a Mermaid diagram; attacker can edit a repository's README
How it was found
- Go to any repository in GitLab.
- Edit the README.md file and insert a Mermaid code block containing a malicious link.
- Commit the changes.
- On the latest Safari browser, click the hyperlink in the rendered diagram.
- The link rewrites the DOM using document.write, and clicking again opens a new window as proof of concept.
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 is a tool that turns text into diagrams. GitLab uses it to display diagrams in Markdown files. The report says that Mermaid tries to clean up the diagram to stop bad code from running, but the cleaning can be tricked. So an attacker could put harmful code in a diagram, and when someone clicks it, that code might run. However, GitLab also uses other protections that make it hard to do real damage.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.