# Git flag injection leading to file overwrite and potential remote code execution

Unsantized ref_name in GitLab Commits API passed to git as a flag, letting an attacker overwrite/truncate arbitrary files and leak the gitlab_shell_secret for internal API access.

- Payout: $3,500
- Severity: critical
- Program: GitLab
- Found by: vakzz
- Disclosed: 2019-12-19
- Write-up quality: 8/10
- Original: https://hackerone.com/reports/653125
- Library page: https://bugbountylibrary.com/r/h1%3A653125
- Tags: area:web domain:api domain:web impact:data-leak impact:internal-access impact:privesc kind:bounty platform:hackerone sev:critical tech:rest technique:race-window vuln:command-injection vuln:dos

**Target:** GitLab Commits API (ref_name) via Gitaly find_commits.go
**Root cause:** The ref_name parameter is passed to git log/rev-list without a `--` separator or validation, so a value starting with `--` is interpreted as a git flag (e.g. --output=) rather than a ref.
**Steps:**
1. Create a repo with a file
2. Send `GET /api/v4/projects/5/repository/commits?path=.&ref_name=--output=/tmp/written`
3. Observe git log writes commits to /tmp/written, then rev-list truncates it to 0 bytes
4. To leak gitlab_shell_secret: spam ~32 requests targeting `/var/opt/gitlab/gitlab-rails/etc/gitlab_shell_secret` then `gitlab-ctl restart unicorn` so a git log lands last, leaving commit content in the file
5. Read secret, then call `GET /api/v4/internal/check?secret_token=<secret>` and `/api/v4/internal/discover?secret_token=<secret>&user_id=1`
**How found:** Noticed ref_name flows into git commands in find_commits.go; hypothesized git would parse a leading `--` value as a flag, tested --output= to confirm file write/truncate, then reasoned about which sensitive file could be captured before truncation.
**Exploitation:** Attacker with API access supplies ref_name=--output=<path>; git log writes commit list to the target file and rev-list truncates it. By racing a unicorn restart, the file retains commit content instead of being truncated, leaking gitlab_shell_secret, which authenticates requests to the internal API (discover user_id=1 -> root).
**Escalation:** File truncation DoS chained with race against unicorn restart to preserve secret content, then use leaked gitlab_shell_secret to access internal API and enumerate users including root.
**Insight:** When a user-controlled string is passed as a positional arg to a CLI tool, test leading `--` values to turn it into a flag; git's --output= gives arbitrary file write/truncate, and racing a service restart can preserve written content before truncation.

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

**Curl request with flag-injected ref_name**

```
curl 'http://4290d4225642/api/v4/projects/5/repository/commits?path=.&ref_name=--output=/tmp/written'
```

**Internal API check with leaked secret token**

```
curl -s 'http://4290d4225642/api/v4/internal/check?secret_token=██████████'
{"api_version":"v4","gitlab_version":"12.0.3","gitlab_rev":"08a51a9db93","redis":true}
```

**Internal discover call using leaked token**

```
curl -s 'http://4290d4225642/api/v4/internal/discover?secret_token=███&user_id=1'
{"id":1,"name":"Administrator","username":"root"}
```

