ReDoS in syntax highlighting due to Rouge
GitLab uses the Rouge Ruby gem for syntax highlighting, which contains regular expressions with cubic worst-case complexity. An attacker can craft input that causes catastrophic backtracking, leading to CPU exhaustion and denial of service.
- Triaged in 12 days
- Fixed in 2 months
- Paid in 2 months
- Disclosed after 4 months
Summary
Gitlab is using the ruby gem "rouge" which has a ReDoS vulnerability. In rouge, the lexers used to parse programming languages rely heavily on regular expressions. Some of the regular expressions have cubic worst-case complexity and are vulnerable to Regular Expression Denial of Service (ReDoS). By crafting malicious input, an attacker can cause Denial of Service.
In Gitlab, rouge is used for syntax highlighting when viewing source code files and when rendering markdown (issues, comments, wiki pages, etc.).
We first reported the vulnerability to rouge on 9 March 2021. As it remains unfixed and Gitlab is vulnerable, we are reporting here for your information.
Rouge bug in detail
The vulnerable lexer regular expressions are below. Line numbers refer to the latest rouge version (3.26.0).
Factor
lib/rouge/lexers/factor.rb line 246
Pattern: """\s+.*?\s+"""
As the two \s+ groups and the .* group match spaces, a long string of spaces with no final """ will cause catastrophic backtracking.
GHC Core
lib/rouge/lexers/ghc_core.rb line 20
Pattern: ^Result size of .+\s*.*}
Again, .+ \s* and .* all match spaces, so by not ending in a }, the regex will backtrack.
Ceylon
lib/rouge/lexers/ceylon.rb line 54
Pattern: .*``.*``.*"
The three .* groups match backticks as well, so if a long string of backticks doesn't end in a ", backtracking will occur. To cause ReDoS, an initial double quote is required.
The Factor and Ceylon regexes have been fixed on master (https://github.com/rouge-ruby/rouge/commit/78af25c2dd69be8ce0a83eb368ddcafe7cc294c4) but a new version has not been released. GHC Core has not been fixed.
Recipes for creating source code files which cause ReDoS:
GHC Core (.dump-cse): 'Result size of ' + ' ' * 3456
Factor (.factor): '"""' + ' ' * 3456
Ceylon (.ceylon): '"' + '`' * 3456
As the worst-case complexity is cubic, doubling the length of the repeating part (spaces or backticks) makes processing take 8 times as long.
Steps to reproduce
To test the rouge gem itself outside Gitlab
Using the attached files, run rougify redos.factor, rougify redos.dump-cse, rougify redos.ceylon and see that processing takes a while. Doubling the length of the repeating section of the file will make the processing time take about 8 times as long.
Gitlab wiki (Stored ReDoS)
Edit the main wiki page by adding a fenced code block of factor, ceylon or ghc-core:
```ghc-core
Result size of
```
Attempt to view the wiki and get a 500 after ~60 seconds of 100% CPU.
Affect the Gitlab file viewer (Stored ReDoS)
Upload the redos.factor and redos.ceylon payloads to a repository and then view them. Increase the length of the repeating part to increase the time taken to process. If it takes >60 seconds, there will be a 502 error.
Upload README.md (containing a fenced code block) so that the ReDoS occurs every time the project page is visited.
Gitlab issues and comments
Create an issue containing a fenced code block of factor, ceylon or ghc-core. Rendering only occurs when creating or editing the issue or comment, not each time it's viewed.
Impact
Server DoS - The request causes Rouge to run at 100% CPU until killed e.g. by a 60 second timeout. Making multiple concurrent requests to the affected resources will overwhelm the server and make it unavailable.
Viewing a crafted .ceylon or .factor file will fail with 502 timeouts. As will viewing a Markdown page containing a fenced code block.
Visiting a wiki page with crafted factor, ceylon or ghc-core fenced code blocks will fail with a 500. If done on the home page, this blocks all access to the Wiki, unless you have a link to a particular wiki page or append /edit to the wiki page and fix it.
What is the current bug behavior?
Rouge is vulnerable to ReDoS, so some Gitlab requests result in high CPU usage until timeout.
What is the expected correct behavior?
Rouge regexes avoid overlapping / ambiguous regex patterns, avoiding cubic worst-case complexity.
Relevant logs and/or screenshots
Viewing wiki

Rack::Timeout::RequestTimeoutException (Request ran for longer than 60000ms):
lib/rouge/formatters/html_gitlab.rb:19:in `stream'
lib/gitlab/metrics/instrumentation.rb:160:in `block in stream'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `stream'
lib/banzai/filter/syntax_highlight_filter.rb:42:in `highlight_node'
lib/banzai/filter/syntax_highlight_filter.rb:22:in `block in call'
lib/banzai/filter/syntax_highlight_filter.rb:21:in `call'
lib/banzai/pipeline/base_pipeline.rb:23:in `block (2 levels) in singleton class'
lib/banzai/renderer.rb:130:in `render_result'
lib/gitlab/metrics/instrumentation.rb:160:in `block in render_result'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `render_result'
lib/banzai/renderer.rb:164:in `block in cacheless_render'
lib/gitlab/metrics.rb:79:in `measure'
lib/banzai/renderer.rb:163:in `cacheless_render'
lib/gitlab/metrics/instrumentation.rb:160:in `block in cacheless_render'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `cacheless_render'
lib/banzai/renderer.rb:30:in `render'
lib/gitlab/metrics/instrumentation.rb:160:in `block in render'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `render'
lib/banzai.rb:16:in `render'
app/helpers/markup_helper.rb:262:in `markdown_unsafe'
app/helpers/markup_helper.rb:140:in `markup_unsafe'
app/helpers/markup_helper.rb:131:in `render_wiki_content'
app/views/shared/wikis/show.html.haml:30
app/controllers/application_controller.rb:128:in `render'
app/controllers/concerns/wiki_actions.rb:81:in `show'
ee/lib/gitlab/ip_address_state.rb:10:in `with'
ee/app/controllers/ee/application_controller.rb:40:in `set_current_ip_address'
app/controllers/application_controller.rb:487:in `set_current_admin'
lib/gitlab/session.rb:11:in `with_session'
app/controllers/application_controller.rb:478:in `set_session_storage'
lib/gitlab/i18n.rb:99:in `with_locale'
lib/gitlab/i18n.rb:105:in `with_user_locale'
app/controllers/application_controller.rb:472:in `set_locale'
app/controllers/application_controller.rb:466:in `set_current_context'
lib/gitlab/metrics/elasticsearch_rack_middleware.rb:16:in `call'
lib/gitlab/middleware/rails_queue_duration.rb:33:in `call'
lib/gitlab/metrics/rack_middleware.rb:16:in `block in call'
lib/gitlab/metrics/web_transaction.rb:21:in `run'
lib/gitlab/metrics/rack_middleware.rb:16:in `call'
lib/gitlab/middleware/speedscope.rb:13:in `call'
lib/gitlab/request_profiler/middleware.rb:17:in `call'
lib/gitlab/jira/middleware.rb:19:in `call'
lib/gitlab/middleware/go.rb:20:in `call'
lib/gitlab/etag_caching/middleware.rb:21:in `call'
lib/gitlab/middleware/multipart.rb:172:in `call'
lib/gitlab/middleware/read_only/controller.rb:50:in `call'
lib/gitlab/middleware/read_only.rb:18:in `call'
lib/gitlab/middleware/same_site_cookies.rb:27:in `call'
lib/gitlab/middleware/handle_malformed_strings.rb:21:in `call'
lib/gitlab/middleware/basic_health_check.rb:25:in `call'
lib/gitlab/middleware/handle_ip_spoof_attack_error.rb:25:in `call'
lib/gitlab/middleware/request_context.rb:21:in `call'
config/initializers/fix_local_cache_middleware.rb:11:in `call'
lib/gitlab/middleware/rack_multipart_tempfile_factory.rb:19:in `call'
lib/gitlab/metrics/requests_rack_middleware.rb:74:in `call'
lib/gitlab/middleware/release_env.rb:12:in `call'
Viewing redos.ceylon


Editing an issue

Completed 500 Internal Server Error in 60084ms (ActiveRecord: 44.0ms | Elasticsearch: 0.0ms | Allocations: 32909)
Rack::Timeout::RequestTimeoutException (Request ran for longer than 60000ms):
lib/rouge/formatters/html_gitlab.rb:19:in `stream'
lib/gitlab/metrics/instrumentation.rb:160:in `block in stream'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `stream'
lib/banzai/filter/syntax_highlight_filter.rb:42:in `highlight_node'
lib/banzai/filter/syntax_highlight_filter.rb:22:in `block in call'
lib/banzai/filter/syntax_highlight_filter.rb:21:in `call'
lib/banzai/pipeline/base_pipeline.rb:23:in `block (2 levels) in singleton class'
lib/banzai/renderer.rb:130:in `render_result'
lib/gitlab/metrics/instrumentation.rb:160:in `block in render_result'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `render_result'
lib/banzai/renderer.rb:164:in `block in cacheless_render'
lib/gitlab/metrics.rb:79:in `measure'
lib/banzai/renderer.rb:163:in `cacheless_render'
lib/gitlab/metrics/instrumentation.rb:160:in `block in cacheless_render'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `cacheless_render'
lib/banzai/renderer.rb:52:in `cacheless_render_field'
lib/gitlab/metrics/instrumentation.rb:160:in `block in cacheless_render_field'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `cacheless_render_field'
app/models/concerns/cache_markdown_field.rb:53:in `rendered_field_content'
app/models/concerns/cache_markdown_field.rb:62:in `block in refresh_markdown_cache'
app/models/concerns/cache_markdown_field.rb:59:in `to_h'
app/models/concerns/cache_markdown_field.rb:59:in `refresh_markdown_cache'
app/services/issuable_base_service.rb:263:in `block in update'
app/services/issuable_base_service.rb:262:in `update'
app/services/issues/update_service.rb:20:in `update'
lib/gitlab/metrics/instrumentation.rb:160:in `block in update'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `update'
app/services/issues/update_service.rb:14:in `execute'
ee/app/services/ee/issues/update_service.rb:23:in `execute'
app/controllers/concerns/issuable_actions.rb:27:in `update'
ee/lib/gitlab/ip_address_state.rb:10:in `with'
ee/app/controllers/ee/application_controller.rb:40:in `set_current_ip_address'
app/controllers/application_controller.rb:487:in `set_current_admin'
lib/gitlab/session.rb:11:in `with_session'
app/controllers/application_controller.rb:478:in `set_session_storage'
lib/gitlab/i18n.rb:99:in `with_locale'
lib/gitlab/i18n.rb:105:in `with_user_locale'
app/controllers/application_controller.rb:472:in `set_locale'
app/controllers/application_controller.rb:466:in `set_current_context'
lib/gitlab/metrics/elasticsearch_rack_middleware.rb:16:in `call'
lib/gitlab/middleware/rails_queue_duration.rb:33:in `call'
lib/gitlab/metrics/rack_middleware.rb:16:in `block in call'
lib/gitlab/metrics/web_transaction.rb:21:in `run'
lib/gitlab/metrics/rack_middleware.rb:16:in `call'
lib/gitlab/middleware/speedscope.rb:13:in `call'
lib/gitlab/request_profiler/middleware.rb:17:in `call'
lib/gitlab/jira/middleware.rb:19:in `call'
lib/gitlab/middleware/go.rb:20:in `call'
lib/gitlab/etag_caching/middleware.rb:21:in `call'
lib/gitlab/middleware/multipart.rb:172:in `call'
lib/gitlab/middleware/read_only/controller.rb:50:in `call'
lib/gitlab/middleware/read_only.rb:18:in `call'
lib/gitlab/middleware/same_site_cookies.rb:27:in `call'
lib/gitlab/middleware/handle_malformed_strings.rb:21:in `call'
lib/gitlab/middleware/basic_health_check.rb:25:in `call'
lib/gitlab/middleware/handle_ip_spoof_attack_error.rb:25:in `call'
lib/gitlab/middleware/request_context.rb:21:in `call'
config/initializers/fix_local_cache_middleware.rb:11:in `call'
lib/gitlab/middleware/rack_multipart_tempfile_factory.rb:19:in `call'
lib/gitlab/metrics/requests_rack_middleware.rb:74:in `call'
lib/gitlab/middleware/release_env.rb:12:in `call'
Output of checks
Not tested on GitLab.com, but it is almost certainly vulnerable. Tested in docker.
Results of GitLab environment info
System information
System:
Proxy: no
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.14
Git Version: 2.32.0
Sidekiq Version:5.2.9
Go Version: unknown
GitLab information
Version: 14.0.2-ee
Revision: 2504e045362
Directory: /opt/gitlab/embedded/service/gitlab-rails
DB Adapter: PostgreSQL
DB Version: 12.6
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.19.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
CPU exhaustion DoS
Context
GitLab renders user-supplied Markdown and source code in several places: wiki pages, issues, comments and the repository file viewer. For syntax highlighting it uses the Ruby gem rouge, whose lexers are largely built from regular expressions. When GitLab renders a fenced code block or a source file, it hands the content to rouge, which runs the matching lexer over the text.
Some of those lexer regular expressions have cubic worst-case complexity. Because several of the alternatives inside them can match the same characters, a crafted input that never reaches the closing delimiter forces the regex engine to try an enormous number of ways to split the input. This is a classic Regular Expression Denial of Service (ReDoS). The researcher had already reported the underlying problem to rouge on 9 March 2021; at the time of this report it remained unfixed in a released version, so GitLab was still exposed.
Setup
The testing was done against a Docker installation of GitLab, not GitLab.com. The report notes that GitLab.com was not tested but is "almost certainly vulnerable". The environment information returned by the instance was:
System information
System:
Proxy: no
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.14
Git Version: 2.32.0
Sidekiq Version:5.2.9
Go Version: unknown
GitLab information
Version: 14.0.2-ee
Revision: 2504e045362
Directory: /opt/gitlab/embedded/service/gitlab-rails
DB Adapter: PostgreSQL
DB Version: 12.6
URL: https://gitlab.example.com
HTTP Clone URL: https://gitlab.example.com/some-group/some-project.git
SSH Clone URL: [redacted]:some-group/some-project.git
Elasticsearch: no
Geo: no
Using LDAP: no
Using Omniauth: yes
Omniauth Providers:
GitLab Shell
Version: 13.19.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
The vulnerable regexes are quoted against rouge version 3.26.0. To reproduce outside GitLab, the researcher used the rougify command-line tool that ships with rouge, together with three attached payload files.
Finding it
The starting point is the lexer source. Three lexers contain patterns whose repeated groups overlap:
| Lexer | File and line | Pattern |
|---|---|---|
| Factor | lib/rouge/lexers/factor.rb line 246 |
"""\s+.*?\s+""" |
| GHC Core | lib/rouge/lexers/ghc_core.rb line 20 |
^Result size of .+\s*.*} |
| Ceylon | lib/rouge/lexers/ceylon.rb line 54 |
.*``.*``.*" |
In each case the ambiguity is the same idea:
- Factor: both
\s+groups and the.*group can match spaces, so a long run of spaces with no closing"""causes catastrophic backtracking. - GHC Core:
.+,\s*and.*all match spaces, so an input that never ends in}makes the engine backtrack. - Ceylon: the three
.*groups also match backticks, so a long string of backticks that does not end in a"triggers backtracking. An initial double quote is required to reach the pattern.
Because the worst-case complexity is cubic, doubling the length of the repeating part (spaces or backticks) makes processing take roughly eight times as long. That gives a very cheap way to turn a short input into a long CPU burn.
The Factor and Ceylon regexes were fixed on rouge master in commit [redacted], but no new release had been cut. GHC Core had not been fixed at all.
Exploitation
Step 1: Confirm the lexer is slow outside GitLab
The researcher provides three recipes for source files that trigger the ReDoS. Each is a short prefix followed by 3456 repetitions of a single character:
- GHC Core (
.dump-cse):'Result size of ' + ' ' * 3456 - Factor (
.factor):'"""' + ' ' * 3456 - Ceylon (
.ceylon):'"' + '' * 3456`
With the attached files in place, run:
rougify redos.factor
rougify redos.dump-cse
rougify redos.ceylon
Each command takes a noticeable amount of time. Doubling the length of the repeating section makes processing take about eight times as long, which confirms the cubic behaviour.
Step 2: Stored ReDoS in a wiki page
Edit the main wiki page and add a fenced code block using one of the affected lexers:
```ghc-core
Result size of
```
The fence selects the ghc-core lexer, and the body is the Result size of prefix followed by a long run of spaces with no closing }. When the page is rendered, rouge runs the GHC Core regex over that body and backtracks catastrophically.
Attempting to view the wiki page returns a 500 after roughly 60 seconds of 100% CPU. The screenshot of the error page:

The corresponding server log shows the request being killed by the Rack timeout while inside rouge's HTML formatter, reached through the Markdown rendering pipeline:
Rack::Timeout::RequestTimeoutException (Request ran for longer than 60000ms):
lib/rouge/formatters/html_gitlab.rb:19:in `stream'
lib/gitlab/metrics/instrumentation.rb:160:in `block in stream'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `stream'
lib/banzai/filter/syntax_highlight_filter.rb:42:in `highlight_node'
lib/banzai/filter/syntax_highlight_filter.rb:22:in `block in call'
lib/banzai/filter/syntax_highlight_filter.rb:21:in `call'
lib/banzai/pipeline/base_pipeline.rb:23:in `block (2 levels) in singleton class'
lib/banzai/renderer.rb:130:in `render_result'
lib/gitlab/metrics/instrumentation.rb:160:in `block in render_result'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `render_result'
lib/banzai/renderer.rb:164:in `block in cacheless_render'
lib/gitlab/metrics.rb:79:in `measure'
lib/banzai/renderer.rb:163:in `cacheless_render'
lib/gitlab/metrics/instrumentation.rb:160:in `block in cacheless_render'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `cacheless_render'
lib/banzai/renderer.rb:30:in `render'
lib/gitlab/metrics/instrumentation.rb:160:in `block in render'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `render'
lib/banzai.rb:16:in `render'
app/helpers/markup_helper.rb:262:in `markdown_unsafe'
app/helpers/markup_helper.rb:140:in `markup_unsafe'
app/helpers/markup_helper.rb:131:in `render_wiki_content'
app/views/shared/wikis/show.html.haml:30
app/controllers/application_controller.rb:128:in `render'
app/controllers/concerns/wiki_actions.rb:81:in `show'
ee/lib/gitlab/ip_address_state.rb:10:in `with'
ee/app/controllers/ee/application_controller.rb:40:in `set_current_ip_address'
app/controllers/application_controller.rb:487:in `set_current_admin'
lib/gitlab/session.rb:11:in `with_session'
app/controllers/application_controller.rb:478:in `set_session_storage'
lib/gitlab/i18n.rb:99:in `with_locale'
lib/gitlab/i18n.rb:105:in `with_user_locale'
app/controllers/application_controller.rb:472:in `set_locale'
app/controllers/application_controller.rb:466:in `set_current_context'
lib/gitlab/metrics/elasticsearch_rack_middleware.rb:16:in `call'
lib/gitlab/middleware/rails_queue_duration.rb:33:in `call'
lib/gitlab/metrics/rack_middleware.rb:16:in `block in call'
lib/gitlab/metrics/web_transaction.rb:21:in `run'
lib/gitlab/metrics/rack_middleware.rb:16:in `call'
lib/gitlab/middleware/speedscope.rb:13:in `call'
lib/gitlab/request_profiler/middleware.rb:17:in `call'
lib/gitlab/jira/middleware.rb:19:in `call'
lib/gitlab/middleware/go.rb:20:in `call'
lib/gitlab/etag_caching/middleware.rb:21:in `call'
lib/gitlab/middleware/multipart.rb:172:in `call'
lib/gitlab/middleware/read_only/controller.rb:50:in `call'
lib/gitlab/middleware/read_only.rb:18:in `call'
lib/gitlab/middleware/same_site_cookies.rb:27:in `call'
lib/gitlab/middleware/handle_malformed_strings.rb:21:in `call'
lib/gitlab/middleware/basic_health_check.rb:25:in `call'
lib/gitlab/middleware/handle_ip_spoof_attack_error.rb:25:in `call'
lib/gitlab/middleware/request_context.rb:21:in `call'
config/initializers/fix_local_cache_middleware.rb:11:in `call'
lib/gitlab/middleware/rack_multipart_tempfile_factory.rb:19:in `call'
lib/gitlab/metrics/requests_rack_middleware.rb:74:in `call'
lib/gitlab/middleware/release_env.rb:12:in `call'
The chain is worth reading: render_wiki_content in markup_helper.rb calls BanZai, which runs the syntax_highlight_filter, which calls into lib/rouge/formatters/html_gitlab.rb. The timeout fires inside rouge's stream method in the HTML formatter.
Step 3: Stored ReDoS in the file viewer
Upload the redos.factor and redos.ceylon payloads to a repository and view them. Increasing the length of the repeating part increases the time taken to process; if it exceeds 60 seconds the request fails with a 502. Uploading a README.md containing a fenced code block makes the ReDoS occur every time the project page is visited.
Viewing redos.ceylon produces the error page:

and the highlighted file view that triggers it:

Step 4: ReDoS in issues and comments
Create an issue containing a fenced code block of factor, ceylon or ghc-core. Rendering only happens when the issue or comment is created or edited, not each time it is viewed, so the cost is paid on the write path rather than on every read.
Editing an issue with such a block produces the error page:

and the log shows the same timeout, this time reached through the issue update service and the Markdown cache refresh:
Completed 500 Internal Server Error in 60084ms (ActiveRecord: 44.0ms | Elasticsearch: 0.0ms | Allocations: 32909)
Rack::Timeout::RequestTimeoutException (Request ran for longer than 60000ms):
lib/rouge/formatters/html_gitlab.rb:19:in `stream'
lib/gitlab/metrics/instrumentation.rb:160:in `block in stream'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `stream'
lib/banzai/filter/syntax_highlight_filter.rb:42:in `highlight_node'
lib/banzai/filter/syntax_highlight_filter.rb:22:in `block in call'
lib/banzai/filter/syntax_highlight_filter.rb:21:in `call'
lib/banzai/pipeline/base_pipeline.rb:23:in `block (2 levels) in singleton class'
lib/banzai/renderer.rb:130:in `render_result'
lib/gitlab/metrics/instrumentation.rb:160:in `block in render_result'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `render_result'
lib/banzai/renderer.rb:164:in `block in cacheless_render'
lib/gitlab/metrics.rb:79:in `measure'
lib/banzai/renderer.rb:163:in `cacheless_render'
lib/gitlab/metrics/instrumentation.rb:160:in `block in cacheless_render'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `cacheless_render'
lib/banzai/renderer.rb:52:in `cacheless_render_field'
lib/gitlab/metrics/instrumentation.rb:160:in `block in cacheless_render_field'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `cacheless_render_field'
app/models/concerns/cache_markdown_field.rb:53:in `rendered_field_content'
app/models/concerns/cache_markdown_field.rb:62:in `block in refresh_markdown_cache'
app/models/concerns/cache_markdown_field.rb:59:in `to_h'
app/models/concerns/cache_markdown_field.rb:59:in `refresh_markdown_cache'
app/services/issuable_base_service.rb:263:in `block in update'
app/services/issuable_base_service.rb:262:in `update'
app/services/issues/update_service.rb:20:in `update'
lib/gitlab/metrics/instrumentation.rb:160:in `block in update'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `update'
app/services/issues/update_service.rb:14:in `execute'
ee/app/services/ee/issues/update_service.rb:23:in `execute'
app/controllers/concerns/issuable_actions.rb:27:in `update'
ee/lib/gitlab/ip_address_state.rb:10:in `with'
ee/app/controllers/ee/application_controller.rb:40:in `set_current_ip_address'
app/controllers/application_controller.rb:487:in `set_current_admin'
lib/gitlab/session.rb:11:in `with_session'
app/controllers/application_controller.rb:478:in `set_session_storage'
lib/gitlab/i18n.rb:99:in `with_locale'
lib/gitlab/i18n.rb:105:in `with_user_locale'
app/controllers/application_controller.rb:472:in `set_locale'
app/controllers/application_controller.rb:466:in `set_current_context'
lib/gitlab/metrics/elasticsearch_rack_middleware.rb:16:in `call'
lib/gitlab/middleware/rails_queue_duration.rb:33:in `call'
lib/gitlab/metrics/rack_middleware.rb:16:in `block in call'
lib/gitlab/metrics/web_transaction.rb:21:in `run'
lib/gitlab/metrics/rack_middleware.rb:16:in `call'
lib/gitlab/middleware/speedscope.rb:13:in `call'
lib/gitlab/request_profiler/middleware.rb:17:in `call'
lib/gitlab/jira/middleware.rb:19:in `call'
lib/gitlab/middleware/go.rb:20:in `call'
lib/gitlab/etag_caching/middleware.rb:21:in `call'
lib/gitlab/middleware/multipart.rb:172:in `call'
lib/gitlab/middleware/read_only/controller.rb:50:in `call'
lib/gitlab/middleware/read_only.rb:18:in `call'
lib/gitlab/middleware/same_site_cookies.rb:27:in `call'
lib/gitlab/middleware/handle_malformed_strings.rb:21:in `call'
lib/gitlab/middleware/basic_health_check.rb:25:in `call'
lib/gitlab/middleware/handle_ip_spoof_attack_error.rb:25:in `call'
lib/gitlab/middleware/request_context.rb:21:in `call'
config/initializers/fix_local_cache_middleware.rb:11:in `call'
lib/gitlab/middleware/rack_multipart_tempfile_factory.rb:19:in `call'
lib/gitlab/metrics/requests_rack_middleware.rb:74:in `call'
lib/gitlab/middleware/release_env.rb:12:in `call'
Root cause
The defect is in rouge, not in GitLab's own code. Each of the three lexer regexes contains several quantifiers that can match the same characters, so the engine has a combinatorial number of ways to partition the input. When the input never satisfies the trailing anchor (the closing """, the }, or the "), every one of those partitions is tried before the match fails. With three overlapping groups the number of partitions grows with the cube of the input length, hence cubic complexity.
GitLab's defences did not stop it because the work happens inside a library call during normal rendering. The Rack timeout (60 seconds) bounds each individual request, but it does not prevent the CPU from being consumed for that whole period. The Markdown cache means issues and comments only pay the cost on create or edit, but wiki pages and the file viewer render on read, so the cost recurs on every visit.
Impact
Server DoS. A single request drives rouge to 100% CPU until it is killed, for example by the 60 second timeout. Making multiple concurrent requests to the affected resources overwhelms the server and makes it unavailable.
Concretely, as described in the report:
- Viewing a crafted
.ceylonor.factorfile fails with a 502 timeout, as does viewing a Markdown page containing a matching fenced code block. - Visiting a wiki page with crafted factor, ceylon or ghc-core fenced code blocks fails with a 500. If the payload is placed on the wiki home page, it blocks all access to the Wiki, unless you have a direct link to a particular wiki page or append
/editto the wiki page and fix it. - Issues and comments only render on create or edit, so the cost there is paid on the write path.
The report hedges on scope: GitLab.com was not tested, though the researcher considered it almost certainly vulnerable.
Fix
Two of the three regexes were fixed upstream in rouge master (commit [redacted]), but no new release had been published at the time of writing, so GitLab installations pulling a released rouge version remained exposed. GHC Core had not been fixed at all. The correct behaviour, as stated in the report, is for the lexer regexes to avoid overlapping or ambiguous patterns so that they do not have cubic worst-case complexity.
Variants to test
- Other rouge lexers. The same ambiguity pattern (two or more quantifiers that can match the same character class, followed by a delimiter that the input omits) may exist in lexers beyond Factor, GHC Core and Ceylon. A positive result is a
rougifyrun whose time grows roughly eightfold when the repeating section doubles. - Other GitLab render paths. Anywhere GitLab renders user-controlled text through rouge is a candidate: wiki pages, issues, comments, merge request descriptions, repository file views, README rendering on project pages. A positive result is a 500 or 502 after roughly 60 seconds with the timeout stack trace pointing into
html_gitlab.rb. - Other applications embedding rouge. Any product that uses rouge for syntax highlighting inherits the same lexer regexes. A positive result is the same CPU spike and timeout when the crafted file or fenced block is rendered.
- Other regex-based highlighters. The general technique applies to any highlighter whose lexers use backtracking regex engines with overlapping quantifiers. A positive result is superlinear growth in processing time as the repeating section is lengthened.
Hunting notes
This class of bug hides in third-party libraries that applications call during normal rendering, especially syntax highlighters, Markdown renderers and template engines. During recon, look for the libraries an application uses for code rendering (rouge, Pygments, highlight.js and similar) and check their issue trackers and commit history for ReDoS fixes that have not yet reached a release. The signal is a lexer regex with several quantifiers over the same character class and a trailing delimiter.
The report shows three reachable surfaces (wiki, file viewer, issues), server-side stack traces with the timeout inside rouge, and the failure modes (500 on wiki, 502 on file view). The cubic scaling argument, doubling the input and observing roughly eightfold time, is what turns a theoretical regex concern into a practical DoS. The report also notes the upstream fix status, which helps the program understand what remains to be patched.
What happened, in plain words
GitLab is a website where teams keep their code and their notes in one place. It can show you a file of computer code with pretty colours, and it can show you written pages, called wiki pages, where people write notes using a simple formatting language called Markdown. To colour code and to turn Markdown into a web page, GitLab uses a helper program called rouge. Rouge is a library: a bundle of ready-made code that other programs can borrow instead of writing everything themselves.
Rouge decides what colour each piece of code should get by running patterns called regular expressions over the text. A regular expression, or regex, is a way of describing "what a piece of text should look like", a bit like a search rule. Some of rouge's rules are written in a way that makes the computer try an enormous number of possible ways to match when the text is deliberately malformed. An attacker can send text that never finishes matching, and the computer keeps trying, using all of its processing power, until something kills the request. This is called a Regular Expression Denial of Service, or ReDoS: denial of service means making a service unavailable to the people who need it.
The researcher had already told the rouge project about the problem on 9 March 2021. It was still not fixed in a released version, so GitLab was still affected, and that is why the report was filed with GitLab.
The setup
The testing was done on a GitLab installation running in Docker, which is a way of packaging a program so it runs the same way on any computer. The researcher did not test GitLab.com, the public hosted version, but wrote that it is almost certainly vulnerable. The instance reported its own environment details, which the researcher included:
System information
System:
Proxy: no
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.14
Git Version: 2.32.0
Sidekiq Version:5.2.9
Go Version: unknown
GitLab information
Version: 14.0.2-ee
Revision: 2504e045362
Directory: /opt/gitlab/embedded/service/gitlab-rails
DB Adapter: PostgreSQL
DB Version: 12.6
URL: https://gitlab.example.com
HTTP Clone URL: https://gitlab.example.com/some-group/some-project.git
SSH Clone URL: [redacted]:some-group/some-project.git
Elasticsearch: no
Geo: no
Using LDAP: no
Using Omniauth: yes
Omniauth Providers:
GitLab Shell
Version: 13.19.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
This block is a plain list of facts the server prints about itself. It says which version of GitLab is running (14.0.2-ee), which version of the Ruby programming language (2.7.2p137), which database (PostgreSQL 12.6) and so on. Knowing the exact version matters because a bug may exist in one version and be fixed in another.
The vulnerable regexes are quoted against rouge version 3.26.0. To test outside GitLab, the researcher used rougify, a small command-line tool that comes with rouge, together with three attached files containing the malicious text.
Finding it
The researcher started by reading the source code of rouge, the actual text of the program. Three of rouge's lexers contain patterns whose repeated parts overlap. A lexer is the part of a highlighter that knows the rules of one particular programming language.
| Lexer | File and line | Pattern |
|---|---|---|
| Factor | lib/rouge/lexers/factor.rb line 246 |
"""\s+.*?\s+""" |
| GHC Core | lib/rouge/lexers/ghc_core.rb line 20 |
^Result size of .+\s*.*} |
| Ceylon | lib/rouge/lexers/ceylon.rb line 54 |
.*``.*``.*" |
In each case the problem is the same idea:
- Factor: both
\s+groups and the.*group can match spaces, so a long run of spaces with no closing"""causes catastrophic backtracking. Backtracking is what a regex engine does when a match fails: it goes back and tries a different way of splitting the text, and with overlapping rules there are astronomically many ways. - GHC Core:
.+,\s*and.*all match spaces, so an input that never ends in}makes the engine backtrack. - Ceylon: the three
.*groups also match backticks, so a long string of backticks that does not end in a"triggers backtracking. An initial double quote is required to reach the pattern.
Because the worst-case complexity is cubic, doubling the length of the repeating part (spaces or backticks) makes processing take roughly eight times as long. Cubic means the time grows with the cube of the input length: twice the length, eight times the work. That gives a very cheap way to turn a short input into a long CPU burn.
The Factor and Ceylon regexes were fixed on rouge master, the main development copy of the project, in commit [redacted], but no new release had been cut. A release is the packaged version that other projects actually install. GHC Core had not been fixed at all.
Exploitation
Step 1: Confirm the lexer is slow outside GitLab
The researcher provides three recipes for source files that trigger the ReDoS. Each is a short prefix followed by 3456 repetitions of a single character:
- GHC Core (
.dump-cse):'Result size of ' + ' ' * 3456 - Factor (
.factor):'"""' + ' ' * 3456 - Ceylon (
.ceylon):'"' + '' * 3456`
These are written in a shorthand: a piece of text, plus a character repeated 3456 times. The file extension (.factor, .ceylon, .dump-cse) tells rouge which lexer to use, just as .jpg tells your computer an image is a photo.
With the attached files in place, run:
rougify redos.factor
rougify redos.dump-cse
rougify redos.ceylon
Each line runs the rougify tool on one file. rougify reads the file, picks the lexer from the extension, and prints the highlighted result. The point of running it outside GitLab first is to prove the slowness comes from rouge itself, not from GitLab's website code. Each command takes a noticeable amount of time. Doubling the length of the repeating section makes processing take about eight times as long, which confirms the cubic behaviour.
Step 2: Stored ReDoS in a wiki page
A wiki page is a page that anyone with access can edit, like a shared notebook. "Stored" means the malicious text is saved on the server and hurts whoever later opens the page, not just the person who typed it. Edit the main wiki page and add a fenced code block using one of the affected lexers:
```ghc-core
Result size of
```
A fenced code block is Markdown's way of saying "everything between these three backticks is code". The word after the opening fence, ghc-core, tells GitLab which language it is, so rouge picks the GHC Core lexer. The body is the Result size of prefix followed by a long run of spaces with no closing }. When the page is rendered, rouge runs the GHC Core regex over that body and backtracks catastrophically.
Attempting to view the wiki page returns a 500 after roughly 60 seconds of 100% CPU. A 500 is the standard web code for "the server hit an internal error". The screenshot of the error page:

The corresponding server log shows the request being killed by the Rack timeout while inside rouge's HTML formatter, reached through the Markdown rendering pipeline. Rack is the layer of the Ruby web server that hands requests to the application, and its timeout is a safety limit that stops a request after a set time:
Rack::Timeout::RequestTimeoutException (Request ran for longer than 60000ms):
lib/rouge/formatters/html_gitlab.rb:19:in `stream'
lib/gitlab/metrics/instrumentation.rb:160:in `block in stream'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `stream'
lib/banzai/filter/syntax_highlight_filter.rb:42:in `highlight_node'
lib/banzai/filter/syntax_highlight_filter.rb:22:in `block in call'
lib/banzai/filter/syntax_highlight_filter.rb:21:in `call'
lib/banzai/pipeline/base_pipeline.rb:23:in `block (2 levels) in singleton class'
lib/banzai/renderer.rb:130:in `render_result'
lib/gitlab/metrics/instrumentation.rb:160:in `block in render_result'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `render_result'
lib/banzai/renderer.rb:164:in `block in cacheless_render'
lib/gitlab/metrics.rb:79:in `measure'
lib/banzai/renderer.rb:163:in `cacheless_render'
lib/gitlab/metrics/instrumentation.rb:160:in `block in cacheless_render'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `cacheless_render'
lib/banzai/renderer.rb:30:in `render'
lib/gitlab/metrics/instrumentation.rb:160:in `block in render'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `render'
lib/banzai.rb:16:in `render'
app/helpers/markup_helper.rb:262:in `markdown_unsafe'
app/helpers/markup_helper.rb:140:in `markup_unsafe'
app/helpers/markup_helper.rb:131:in `render_wiki_content'
app/views/shared/wikis/show.html.haml:30
app/controllers/application_controller.rb:128:in `render'
app/controllers/concerns/wiki_actions.rb:81:in `show'
ee/lib/gitlab/ip_address_state.rb:10:in `with'
ee/app/controllers/ee/application_controller.rb:40:in `set_current_ip_address'
app/controllers/application_controller.rb:487:in `set_current_admin'
lib/gitlab/session.rb:11:in `with_session'
app/controllers/application_controller.rb:478:in `set_session_storage'
lib/gitlab/i18n.rb:99:in `with_locale'
lib/gitlab/i18n.rb:105:in `with_user_locale'
app/controllers/application_controller.rb:472:in `set_locale'
app/controllers/application_controller.rb:466:in `set_current_context'
lib/gitlab/metrics/elasticsearch_rack_middleware.rb:16:in `call'
lib/gitlab/middleware/rails_queue_duration.rb:33:in `call'
lib/gitlab/metrics/rack_middleware.rb:16:in `block in call'
lib/gitlab/metrics/web_transaction.rb:21:in `run'
lib/gitlab/metrics/rack_middleware.rb:16:in `call'
lib/gitlab/middleware/speedscope.rb:13:in `call'
lib/gitlab/request_profiler/middleware.rb:17:in `call'
lib/gitlab/jira/middleware.rb:19:in `call'
lib/gitlab/middleware/go.rb:20:in `call'
lib/gitlab/etag_caching/middleware.rb:21:in `call'
lib/gitlab/middleware/multipart.rb:172:in `call'
lib/gitlab/middleware/read_only/controller.rb:50:in `call'
lib/gitlab/middleware/read_only.rb:18:in `call'
lib/gitlab/middleware/same_site_cookies.rb:27:in `call'
lib/gitlab/middleware/handle_malformed_strings.rb:21:in `call'
lib/gitlab/middleware/basic_health_check.rb:25:in `call'
lib/gitlab/middleware/handle_ip_spoof_attack_error.rb:25:in `call'
lib/gitlab/middleware/request_context.rb:21:in `call'
config/initializers/fix_local_cache_middleware.rb:11:in `call'
lib/gitlab/middleware/rack_multipart_tempfile_factory.rb:19:in `call'
lib/gitlab/metrics/requests_rack_middleware.rb:74:in `call'
lib/gitlab/middleware/release_env.rb:12:in `call'
This is a stack trace: a list of the function calls that were running when the error happened, read from the innermost call at the top to the outermost at the bottom. The first line says the request ran for longer than 60000 milliseconds, which is 60 seconds. The top line, lib/rouge/formatters/html_gitlab.rb:19:in 'stream', is where the timeout fired: inside rouge, in the code that streams out the highlighted HTML. Further down you can see render_wiki_content in markup_helper.rb, which is GitLab's wiki rendering helper, and syntax_highlight_filter.rb, the part of the Markdown pipeline that adds colours. The chain is worth reading: render_wiki_content calls BanZai, GitLab's Markdown renderer, which runs the syntax_highlight_filter, which calls into lib/rouge/formatters/html_gitlab.rb. The timeout fires inside rouge's stream method in the HTML formatter.
Step 3: Stored ReDoS in the file viewer
The file viewer is the page that shows a file from a repository with syntax highlighting. Upload the redos.factor and redos.ceylon payloads to a repository and view them. Increasing the length of the repeating part increases the time taken to process; if it exceeds 60 seconds the request fails with a 502. A 502 means a gateway in front of the application gave up waiting for an answer. Uploading a README.md containing a fenced code block makes the ReDoS occur every time the project page is visited. A README is the front page of a project, shown automatically.
Viewing redos.ceylon produces the error page:

and the highlighted file view that triggers it:

Step 4: ReDoS in issues and comments
An issue is a task or bug report inside a project, and comments are replies on it. Create an issue containing a fenced code block of factor, ceylon or ghc-core. Rendering only happens when the issue or comment is created or edited, not each time it is viewed, so the cost is paid on the write path rather than on every read. In other words, the attacker burns CPU when saving the text, and later visitors see a normal page.
Editing an issue with such a block produces the error page:

and the log shows the same timeout, this time reached through the issue update service and the Markdown cache refresh. A cache is a stored copy of work already done, so the rendered Markdown is saved and reused instead of being recomputed on every view:
Completed 500 Internal Server Error in 60084ms (ActiveRecord: 44.0ms | Elasticsearch: 0.0ms | Allocations: 32909)
Rack::Timeout::RequestTimeoutException (Request ran for longer than 60000ms):
lib/rouge/formatters/html_gitlab.rb:19:in `stream'
lib/gitlab/metrics/instrumentation.rb:160:in `block in stream'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `stream'
lib/banzai/filter/syntax_highlight_filter.rb:42:in `highlight_node'
lib/banzai/filter/syntax_highlight_filter.rb:22:in `block in call'
lib/banzai/filter/syntax_highlight_filter.rb:21:in `call'
lib/banzai/pipeline/base_pipeline.rb:23:in `block (2 levels) in singleton class'
lib/banzai/renderer.rb:130:in `render_result'
lib/gitlab/metrics/instrumentation.rb:160:in `block in render_result'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `render_result'
lib/banzai/renderer.rb:164:in `block in cacheless_render'
lib/gitlab/metrics.rb:79:in `measure'
lib/banzai/renderer.rb:163:in `cacheless_render'
lib/gitlab/metrics/instrumentation.rb:160:in `block in cacheless_render'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `cacheless_render'
lib/banzai/renderer.rb:52:in `cacheless_render_field'
lib/gitlab/metrics/instrumentation.rb:160:in `block in cacheless_render_field'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `cacheless_render_field'
app/models/concerns/cache_markdown_field.rb:53:in `rendered_field_content'
app/models/concerns/cache_markdown_field.rb:62:in `block in refresh_markdown_cache'
app/models/concerns/cache_markdown_field.rb:59:in `to_h'
app/models/concerns/cache_markdown_field.rb:59:in `refresh_markdown_cache'
app/services/issuable_base_service.rb:263:in `block in update'
app/services/issuable_base_service.rb:262:in `update'
app/services/issues/update_service.rb:20:in `update'
lib/gitlab/metrics/instrumentation.rb:160:in `block in update'
lib/gitlab/metrics/method_call.rb:27:in `measure'
lib/gitlab/metrics/instrumentation.rb:160:in `update'
app/services/issues/update_service.rb:14:in `execute'
ee/app/services/ee/issues/update_service.rb:23:in `execute'
app/controllers/concerns/issuable_actions.rb:27:in `update'
ee/lib/gitlab/ip_address_state.rb:10:in `with'
ee/app/controllers/ee/application_controller.rb:40:in `set_current_ip_address'
app/controllers/application_controller.rb:487:in `set_current_admin'
lib/gitlab/session.rb:11:in `with_session'
app/controllers/application_controller.rb:478:in `set_session_storage'
lib/gitlab/i18n.rb:99:in `with_locale'
lib/gitlab/i18n.rb:105:in `with_user_locale'
app/controllers/application_controller.rb:472:in `set_locale'
app/controllers/application_controller.rb:466:in `set_current_context'
lib/gitlab/metrics/elasticsearch_rack_middleware.rb:16:in `call'
lib/gitlab/middleware/rails_queue_duration.rb:33:in `call'
lib/gitlab/metrics/rack_middleware.rb:16:in `block in call'
lib/gitlab/metrics/web_transaction.rb:21:in `run'
lib/gitlab/metrics/rack_middleware.rb:16:in `call'
lib/gitlab/middleware/speedscope.rb:13:in `call'
lib/gitlab/request_profiler/middleware.rb:17:in `call'
lib/gitlab/jira/middleware.rb:19:in `call'
lib/gitlab/middleware/go.rb:20:in `call'
lib/gitlab/etag_caching/middleware.rb:21:in `call'
lib/gitlab/middleware/multipart.rb:172:in `call'
lib/gitlab/middleware/read_only/controller.rb:50:in `call'
lib/gitlab/middleware/read_only.rb:18:in `call'
lib/gitlab/middleware/same_site_cookies.rb:27:in `call'
lib/gitlab/middleware/handle_malformed_strings.rb:21:in `call'
lib/gitlab/middleware/basic_health_check.rb:25:in `call'
lib/gitlab/middleware/handle_ip_spoof_attack_error.rb:25:in `call'
lib/gitlab/middleware/request_context.rb:21:in `call'
config/initializers/fix_local_cache_middleware.rb:11:in `call'
lib/gitlab/middleware/rack_multipart_tempfile_factory.rb:19:in `call'
lib/gitlab/metrics/requests_rack_middleware.rb:74:in `call'
lib/gitlab/middleware/release_env.rb:12:in `call'
The first line says the request completed with a 500 error after 60084 milliseconds, and breaks down where the time went: 44.0 milliseconds in the database layer (ActiveRecord), nothing in Elasticsearch, and 32909 memory allocations. The rest is the same stack trace shape as before, but the middle section is different: it goes through refresh_markdown_cache and issues/update_service.rb, which is the code that saves an edited issue and refreshes its cached rendered text. That confirms the CPU burn happens while saving, not while reading.
Root cause
The defect is in rouge, not in GitLab's own code. Each of the three lexer regexes contains several quantifiers that can match the same characters, so the engine has a combinatorial number of ways to partition the input. A quantifier is the part of a regex that says "one or more" or "any number of". When the input never satisfies the trailing anchor (the closing """, the }, or the "), every one of those partitions is tried before the match fails. An anchor is a piece of the pattern that must be found for the match to succeed. With three overlapping groups the number of partitions grows with the cube of the input length, hence cubic complexity.
GitLab's defences did not stop it because the work happens inside a library call during normal rendering. The Rack timeout (60 seconds) bounds each individual request, but it does not prevent the CPU from being consumed for that whole period. The Markdown cache means issues and comments only pay the cost on create or edit, but wiki pages and the file viewer render on read, so the cost recurs on every visit.
Impact
Server DoS. A single request drives rouge to 100% CPU until it is killed, for example by the 60 second timeout. Making multiple concurrent requests to the affected resources overwhelms the server and makes it unavailable. Concurrent means several at the same time.
Concretely, as described in the report:
- Viewing a crafted
.ceylonor.factorfile fails with a 502 timeout, as does viewing a Markdown page containing a matching fenced code block. - Visiting a wiki page with crafted factor, ceylon or ghc-core fenced code blocks fails with a 500. If the payload is placed on the wiki home page, it blocks all access to the Wiki, unless you have a direct link to a particular wiki page or append
/editto the wiki page and fix it. - Issues and comments only render on create or edit, so the cost there is paid on the write path.
The report hedges on scope: GitLab.com was not tested, though the researcher considered it almost certainly vulnerable.
Fix
Two of the three regexes were fixed upstream in rouge master (commit [redacted]), but no new release had been published at the time of writing, so GitLab installations pulling a released rouge version remained exposed. GHC Core had not been fixed at all. The correct behaviour, as stated in the report, is for the lexer regexes to avoid overlapping or ambiguous patterns so that they do not have cubic worst-case complexity.
Variants to test
- Other rouge lexers. The same ambiguity pattern (two or more quantifiers that can match the same character class, followed by a delimiter that the input omits) may exist in lexers beyond Factor, GHC Core and Ceylon. A positive result is a
rougifyrun whose time grows roughly eightfold when the repeating section doubles. - Other GitLab render paths. Anywhere GitLab renders user-controlled text through rouge is a candidate: wiki pages, issues, comments, merge request descriptions, repository file views, README rendering on project pages. A positive result is a 500 or 502 after roughly 60 seconds with the timeout stack trace pointing into
html_gitlab.rb. - Other applications embedding rouge. Any product that uses rouge for syntax highlighting inherits the same lexer regexes. A positive result is the same CPU spike and timeout when the crafted file or fenced block is rendered.
- Other regex-based highlighters. The general technique applies to any highlighter whose lexers use backtracking regex engines with overlapping quantifiers. A positive result is superlinear growth in processing time as the repeating section is lengthened.
Hunting notes
This class of bug hides in third-party libraries that applications call during normal rendering, especially syntax highlighters, Markdown renderers and template engines. During recon, look for the libraries an application uses for code rendering (rouge, Pygments, highlight.js and similar) and check their issue trackers and commit history for ReDoS fixes that have not yet reached a release. The signal is a lexer regex with several quantifiers over the same character class and a trailing delimiter.
The report shows three reachable surfaces (wiki, file viewer, issues), server-side stack traces with the timeout inside rouge, and the failure modes (500 on wiki, 502 on file view). The cubic scaling argument, doubling the input and observing roughly eightfold time, is what turns a theoretical regex concern into a practical DoS. The report also notes the upstream fix status, which helps the program understand what remains to be patched.
Glossary
- Regular expression (regex): a pattern that describes what a piece of text should look like, used to search or match text.
- ReDoS (Regular Expression Denial of Service): an attack where a crafted input makes a regex engine do an enormous amount of work, using up the server's processing power.
- Denial of Service (DoS): making a service unavailable to the people who need it, usually by exhausting its resources.
- Backtracking: when a regex engine fails to match and goes back to try a different way of splitting the text.
- Catastrophic backtracking: backtracking that explodes into an enormous number of attempts because the pattern has overlapping parts.
- Cubic complexity: work that grows with the cube of the input size, so doubling the input makes it about eight times slower.
- Lexer: the part of a syntax highlighter that knows the rules of one programming language.
- Syntax highlighting: colouring code so it is easier to read.
- Library / gem: a bundle of ready-made code that other programs borrow; in Ruby, libraries are called gems.
- rouge: the Ruby library GitLab uses for syntax highlighting.
- Markdown: a simple way of writing formatted text, for example with backticks for code.
- Fenced code block: a Markdown block of code between three backticks, with an optional language name after the opening fence.
- Wiki: a set of pages that people with access can edit together.
- Issue: a task or bug report inside a project.
- Comment: a reply on an issue or other item.
- README: the front page of a project, shown automatically.
- File viewer: the page that shows a file from a repository with highlighting.
- Docker: a way of packaging a program so it runs the same way on any computer.
- Rack: the layer of a Ruby web server that hands incoming requests to the application.
- Rack timeout: a safety limit that stops a request after a set time.
- Stack trace: a list of the function calls that were running when an error happened.
- 500: the web code for an internal server error.
- 502: the web code for a gateway that gave up waiting for the application.
- Cache: a stored copy of work already done, reused instead of recomputed.
- Quantifier: the part of a regex that says "one or more" or "any number of".
- Anchor: a piece of a regex pattern that must be found for the match to succeed.
- Commit: a saved change in a code project's history.
- Release: a packaged version of a library that other projects install.
- Upstream: the original project that publishes a library, as opposed to the application using it.
- Concurrent: happening at the same time.
- Recon: the early stage of security testing where you learn what an application is built from.