Skip to content
Access control Disclosed report Intermediate

Smuggling content in MR with refs/replace in Gitlab

A user with Developer role could push a refs/replace reference to substitute a merge request's head commit with a different commit, so the merge request displayed one change but merged another. This allowed bypassing branch protection and injecting malicious code into a protected branch.

GitLab

Originally disclosed on GitLab

  • Fixed in 2 months
  • Disclosed after 2 months

⚠️ 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 #1938185 by inspector-ambitious on 2023-04-07, assigned to @ottilia_westerlund:

Report | Attachments | How To Reproduce

Report

Summary

In Git, the refs/replace namespace is used to create a temporary reference that points to a commit object, allowing for the replacement or overwriting of an existing commit with a new one.
When a new commit object is created in Git, it is assigned a unique SHA-1 hash that identifies it in the repository's history.

However, an attacker with the Developer role in a repository can push a replacement reference in the refs/replace namespace, which would replace an existing commit with a malicious one. And if the head commit of a MR is replaced with a malicious one, we will get a situation where the MR will show the original commit but what would be merged will be the malicious one instead. Therefore this would allow an attacker to inject malicious code in the protected branch.
You can find a documentation on git replace here.

Steps to reproduce

To reproduce this vulnerability we will need 2 users:

  • victim01 the victim and repository owner.
  • inspector-ambitious the attacker with Developer access.

Step 1: Setup repository (in victim context victim01)

  1. Create an empty (no README) private project called replace

  2. Add one empty file called a. (we need at least one commit)

image.png
image.png From the original on GitLab
  1. Go to Settings > Repository > Protected Branches and check that main branch is protected
image.png
image.png From the original on GitLab

It should be the default setup. Only maintainers can merge or push to this branch.

  1. Got to Project information > Members and invite inspector-ambitious with the role Developer
image.png
image.png From the original on GitLab

Step 2: Attack preparation (in attacker context inspector-ambitious)

** It is extremely important to respect the order of operations here **

  1. Create a new branch from main called b for example
  2. Add a new empty file called bin the branch b
image.png
image.png From the original on GitLab
  1. Create a branch called c from main with an empty file called c
image.png
image.png From the original on GitLab
  1. Clone the repository and run git ls-remote in the terminal
image.png
image.png From the original on GitLab
  1. Now push a refs/replace reference to replace commit from b with c

in our case based on previous output of ls-remote

image.png
image.png From the original on GitLab

running git ls-remote again we get

image.png
image.png From the original on GitLab
  1. Create a MR from b to main.
image.png
image.png From the original on GitLab
image.png
image.png
image.png
image.png

As you can see only the file b should be added when this PR is merged.

Step 3: main branch is compromised (in victim context victim01)

  1. Go to the pull request and merge it.
image.png
image.png From the original on GitLab
  1. Go to the main branch
image.png
image.png From the original on GitLab

You can see the file c from c got merged !

The commit history looks correct.

image.png
image.png From the original on GitLab

However if we look at the merge commit we see that c was added instead of b

image.png
image.png From the original on GitLab

Output of checks

This bug happens on GitLab.com, I tried to reproduce it on a local instance but somehow it didn't work out...

Impact

A collaborator can bypass branch protection and smuggle malicious content in a merge request, which could potentially introduce security vulnerabilities or other types of defects into the codebase, compromising the integrity and security of the repository. This could result in sensitive data being leaked, unauthorized access to resources, or even complete system compromise.

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's merge logic did not resolve or reject refs/replace references, so the commit used for the merge differed from the one shown in the merge request.

Impact

A collaborator could bypass branch protection and merge malicious content into a protected branch, potentially compromising the repository's integrity and security.

What the attacker needed

Attacker has Developer role in the project; victim merges the merge request; protected branch exists

How it was found

  1. Create a private project with a protected main branch and invite the attacker as Developer.
  2. As the attacker, create branches b and c from main, each adding a different empty file.
  3. Clone the repository and list remote refs to get commit hashes.
  4. Push a refs/replace reference to replace the commit from branch b with the commit from branch c.
  5. Create a merge request from branch b to main.
  6. As the victim, merge the merge request and observe that the file from branch c was merged instead of the file from branch b.

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.

In Git, a commit is identified by a long code (hash). Git has a feature called refs/replace that lets you tell Git 'when you see commit X, use commit Y instead'. The report shows that GitLab did not account for this when merging a merge request, so the merge request looked like it would add one file but actually added a different one. This let a user with only Developer access (who normally cannot push to a protected branch) sneak changes into that branch.

What is Access control?

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.

More from GitLab

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Broken access control

The lesson behind this report.

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.