Skip to content
RCE Disclosed report Advanced

developers can inject codes into proteced branch pipelines by using git replace command

GitLab does not filter pushes of git replace refs, allowing a developer to replace file content across branches. By obtaining a maintainer's job token, the developer can trigger a pipeline that fetches the replace refs and injects code into a protected branch.

GitLab

Originally disclosed on GitLab

  • Fixed in 19 days
  • Disclosed after 19 days

⚠️ Please read the process on how to fix security issues before starting to work on the issue. Vulnerabilities must be fixed in a security mirror.

HackerOne report #2793305 by xorz on 2024-10-20, assigned to GitLab Team:

Report | Attachments | How To Reproduce

Report

Summary

git replace is a git command to mapping an object to another, it also can be used across different branches.

A developer can use this cmd to replace a file content with another file and then this developer can push this replace ref to remote, the Gitlab server do NOT check the push action of replace refs.

The developer can get the job token of a maintainer user by using the steps in #2792703, the developer can get the job token when a maintainer creates a branch with a commit and no more actions is required, or just a merge click in #2786548.

The developer can use the job token to trigger a new pipeline with the maintainer token with parameter variables[GIT_FETCH_EXTRA_FLAGS]="-u refs/*:refs/*", then the code in main will be injected.

This report is focusing on showing the weakness of git refs push rules, please mark as requiring more info if you have concerns about the how to get job token.

Steps to reproduce
  1. create a project with ultimate plan
  2. invite victim as maintainer
  3. invite attacker as developer
  4. goto http://gitlab.test/test-git/project-a/-/settings/ci_cd
  5. click on General pipelines and set the Git strategy to git clone
  6. goto http://gitlab.test/victim/project-a/-/settings/merge_requests
  7. enable Enable merged results pipelines and save the config

Login as Victim

  1. unzip the project-a.zip
  2. push the main to remote as victim

Login as Attacker

  1. push the attacker-branch to remote as attacker
  2. push the replace ref to remote as attacker using git push origin refs/replace/bfb0b4cd8258d7aa5d9b85083edc1b91eb7ddce5:refs/replace/bfb0b4cd8258d7aa5d9b85083edc1b91eb7ddce5. You can use git replace -l to get this hash.
  3. get the maintainer job token following the steps in the #2792703 or check the video to see the details
  4. run trigger-pipeline.sh to trigger a pipeline on main using the job token
  5. then you should see the keywords injected in main branch pipeline
Impact
  1. Codes can be injected into protected branches
  2. Protected tokens like project access token can be leaked
  3. The attacker can remove users from current project if there are project/group access tokens in the environment variables for protected branches
  4. The attacker can also push codes to other private projects if there are personal/group access tokens in the environment variables for protected branches
Examples
What is the current bug behavior?
  1. git replace refs not filtered
What is the expected correct behavior?
  1. git replace refs can be filtered
Relevant logs and/or screenshots
job-detail.png
job-detail.png From the original on GitLab
pipeline-code-injection-git-replace.mp4
Output of checks
Results of GitLab environment info
System information  
System:  
Proxy:          no  
Current User:   git  
Using RVM:      no  
Ruby Version:   3.1.5p253  
Gem Version:    3.5.17  
Bundler Version:2.5.11  
Rake Version:   13.0.6  
Redis Version:  7.0.15  
Sidekiq Version:7.2.4  
Go Version:     unknown

GitLab information  
Version:        17.4.2-ee  
Revision:       e85e7bae1a9  
Directory:      /opt/gitlab/embedded/service/gitlab-rails  
DB Adapter:     PostgreSQL  
DB Version:     14.11  
URL:            http://gitlab.test  
HTTP Clone URL: http://gitlab.test/some-group/some-project.git  
SSH Clone URL:  [email protected]:some-group/some-project.git  
Elasticsearch:  no  
Geo:            no  
Using LDAP:     no  
Using Omniauth: yes  
Omniauth Providers:

GitLab Shell  
Version:        14.39.0  
Repository storages:  
- default:      unix:/var/opt/gitlab/gitaly/gitaly.socket  
GitLab Shell path:              /opt/gitlab/embedded/service/gitlab-shell

Gitaly  
- default Address:      unix:/var/opt/gitlab/gitaly/gitaly.socket  
- default Version:      17.4.2  
- default Git Version:  2.46.0

Impact

  1. Codes can be injected into main branch
  2. Protected tokens like project access token can be leaked
    1. The attacker can also access other private projects or remove users from current project if there are project/group access tokens in the environment variables for protected branches

Attachments

Warning: Attachments received through HackerOne, please exercise caution!

How To Reproduce

Please add reproducibility information to this section:

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

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely GitLab does not validate or filter pushes of git replace refs, allowing them to be used in pipelines.

Impact

An attacker could inject code into protected branches and leak protected tokens, potentially removing users or pushing code to other private projects if access tokens are present in environment variables.

What the attacker needed

Ultimate plan project; Attacker has developer role; Victim has maintainer role; Attacker obtains maintainer job token

The fix

GitLab should filter git replace refs to prevent their use in pipelines.

How it was found

  1. Create a project with ultimate plan and invite a victim as maintainer and an attacker as developer.
  2. Set the Git strategy to git clone and enable merged results pipelines.
  3. As victim, push the main branch to remote.
  4. As attacker, push the attacker-branch and the replace ref to remote.
  5. Obtain the maintainer job token following steps in another report.
  6. Run trigger-pipeline.sh to trigger a pipeline on main using the job token, then see 'injected' in the pipeline.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

Git replace refs let you swap one Git object for another, even across branches. GitLab didn't block developers from pushing these refs, so a developer could make a protected branch's pipeline use different code. With a maintainer's job token, they could trigger a pipeline that fetches the malicious refs and runs injected code.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

More from GitLab

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.