# Remote Command Execution via Github import

Attacker-controlled GitHub API response overrides Sawyer::Resource to_s/bytesize, injecting arbitrary Redis commands during import, chaining to Marshal.load deserialization for RCE.

- Payout: $33,510
- Severity: critical
- Program: GitLab
- Found by (vendor credit): yvvdwf
- Found by: vakzz
- Disclosed: 2022-10-06
- CVE: CVE-2022-2884
- Write-up quality: 9/10
- Original: https://hackerone.com/reports/1679624
- Library page: https://bugbountylibrary.com/r/h1%3A1679624
- Tags: area:web domain:web impact:rce kind:bounty platform:hackerone sev:critical tech:redis tech:ruby technique:chained technique:incomplete-fix technique:source-review vuln:command-injection vuln:deserialization vuln:rce

**Target:** GitLab GitHub importer (RepositoryImporter / redis-rb command serialization)
**Root cause:** Sawyer::Resource dynamically defines methods from JSON keys, allowing attacker to override built-in to_s/bytesize; redis-rb uses these to compute RESP bulk-string length, so a shorter bytesize than actual string injects extra Redis commands.
**Steps:**
1. Host fake GitHub API server returning repo JSON with `default_branch` = `{"to_s":{"to_s":"ggg\r\nINJECT_RESP_HERE","bytesize":3}}`
2. POST `/api/v4/import/github` with `github_hostname` pointing to attacker server and valid PRIVATE-TOKEN
3. Importer calls `change_head(default_branch)` -> `branch_exists?` -> redis `sismember` with the Sawyer::Resource
4. redis-rb writes `$3\r\nggg\r\n` then attacker's injected RESP commands (e.g. `set session:gitlab:jjjj`) (e.g. `set session:gitlab:jjjj <marshal gadget>`)
5. Send request with `Cookie: _gitlab_session=jjjj` to trigger `Marshal.load` of the injected session -> RCE
**How found:** Noticed prior CVE-2022-2884 patch only covered Gitlab::Cache::Import::Caching; searched for other paths where Sawyer::Resource reaches redis. Traced default_branch from GitHub API response through change_head -> branch_exists? -> redis_set_cache.try_include?, and recognized redis-rb's use of to_s/bytesize on the object enables RESP length mismatch.
**Exploitation:** Attacker needs ability to import a GitHub repo (any user). Fake GitHub API returns malicious default_branch; Redis command injection writes a Marshal-serialized gadget (Gem::Requirement/Net::WriteAdapter chain) into a session key; then a request with that session cookie deserializes it, executing `system` command (e.g. `echo id > /tmp/...`).
**Escalation:** Redis injection chained with Ruby Marshal.load deserialization gadget to escalate from command injection into full RCE.
**Insight:** When a library dynamically defines methods from untrusted JSON keys (Sawyer), any downstream code calling to_s/bytesize/length on that object is attacker-controllable; grep for protocol serializers that trust object methods for length prefixes. Also: incomplete fixes for prior CVEs are prime hunting ground—enumerate sibling call sites of the patched sink.

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

**Malicious default_branch JSON overriding to_s/bytesize**

```
{
            "default_branch": {
                "to_s": {
                    "to_s": 'ggg\r\nINJECT_RESP_HERE',
                    "bytesize": 3,
                }
            }
        }
```

**curl with crafted session cookie to trigger gadget**

```
curl -v 'http://gitlab.wbowling.info/root' -H 'Cookie: _gitlab_session=gggg'` replacing `gitlab.wbowling.info` with your gitlab url and `gggg` with the string you used in `gen_payload3.rb`
```

