Bug with CODEOWNERS approval removal allows to merge arbitrary code into protected branches
When CODEOWNERS approval removal is enabled, pushing new commits to a merge request after approval does not remove the existing approval, even if the new code touches files owned by the CODEOWNER. An attacker with developer access could push malicious code after receiving approval and merge it into a protected branch without re-approval.
GitLab
Originally disclosed on GitLab
- Fixed in 3 months
- Disclosed after 3 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 #1928709 by glan1k on 2023-03-31, assigned to @ottilia_westerlund:
Report | How To Reproduce
Report
Summary
In case of enabled CODEOWNERS, and "Remove approvals by Code Owners if their files changed" is checked (1), it's possible to push new code which is related to one or more CODEOWNERS, but approve won't be removed
Such vulnerability allows attacker to push arbitrary, unapproved code after the initial approval, and compromise protected branches code/variables, and ultimately - production environments
Steps to reproduce
(Step-by-step guide to reproduce the issue, including:)
- Create GItLab Repository
- Create user "repo_approver_user" as Developer in repository
- Create user "repo_attacker_user" as Developer in repository
- Enable CODEOWNERS (1) in repository settings
- Enable "Remove approvals by Code Owners if their files changed" in repository settings (2)
- Push CODEOWNERS file to main branch with content like "* repo_approver_user" so everything will require approve by "repo_approver_user"
- Make CODEOWNERS approval mandatory for protected branch main (3)
- Create new branch "feature_branch" from "repo_attacker_user"
- In local copy, make safe change, push it to remote, and create MR from "repo_attacker_user"
- Receive approve from "repo_approver_user"
- In locally copy, change files to arbitrary malicious code to compromise production, or .gitlab-ci.yml change to steal protected variables
- Do "git amend" and "git push -f" to remote
- Malicious code in remote branch, approve still in place
- Merge code into protected main branch as "repo_attacker_user"
(1) https://docs.gitlab.com/ee/user/project/code_owners.html
(2) https://docs.gitlab.com/ee/user/project/merge_requests/approvals/settings.html#remove-approvals-by-code-owners-if-their-files-changed
(3) https://docs.gitlab.com/ee/user/project/protected_branches.html#require-code-owner-approval-on-a-protected-branch
Impact
Varies from low to high, depending on:
- Protected GitLab CI/CD variables content
- Automated CI/CD to production
- Many other factors
In worst case:
Production environment to which protected branch is eventually deployed, could be compromised including confidential data leak, and integrity compromise
Or
Credentials are stollen from protected GitLab CI/CD variables, and used to compromise production environment, including confidential data leak, and integrity compromise
Examples
GitLab instance is self-hosed and not available from the internet, bug is not tested in cloud GitLab, but probably the same
What is the current bug behavior?
New code related to CODEOWNERS is pushed, approvals from relevant CODEOWNERS remain in place
What is the expected correct behavior?
New code related to CODEOWNERS is pushed, approvals from relevant CODEOWNERS removed
Relevant logs and/or screenshots
No relevant logs, or screenshots
Should be easy to reproduce using "Steps to reproduce"
Output of checks
GitLab.com was not tested
Results of GitLab environment info
System information
System:
Proxy: no
Current User: git
Using RVM: no
Ruby Version: 2.7.7p221
Gem Version: 3.1.6
Bundler Version:2.3.15
Rake Version: 13.0.6
Redis Version: 6.2.8
Sidekiq Version:6.5.7
Go Version: unknown
GitLab information
Version: 15.9.3-ee
Revision: d3356768b48
Directory: /opt/gitlab/embedded/service/gitlab-rails
DB Adapter: PostgreSQL
DB Version: 12.12
URL: https://gitlab.*REDACTED*.com
HTTP Clone URL: https://gitlab.*REDACTED*.com/some-group/some-project.git
SSH Clone URL: ssh://git@gitlab.*REDACTED*.com:2222/some-group/some-project.git
Elasticsearch: no
Geo: no
Using LDAP: no
Using Omniauth: yes
Omniauth Providers: saml
GitLab Shell
Version: 14.17.0
Repository storages:
- default: unix:/var/opt/gitlab/gitaly/gitaly.socket
GitLab Shell path: /opt/gitlab/embedded/service/gitlab-shell
Impact
Varies from low to high, depending on:
- Protected GitLab CI/CD variables content
- Automated CI/CD to production
- Many other factors
In worst case:
Production environment to which protected branch is eventually deployed, could be compromised including confidential data leak, and integrity compromise
Or
Credentials are stollen from protected GitLab CI/CD variables, and used to attack compromise production environment, including confidential data leak, and integrity compromise
How To Reproduce
Please add reproducibility information to this section:
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 the approval removal logic does not trigger when new commits are force-pushed to an existing merge request, even if those commits modify files owned by CODEOWNERS.
Impact
An attacker could merge unapproved code into a protected branch, potentially compromising production environments or stealing protected CI/CD variables, though the severity depends on the environment.
What the attacker needed
CODEOWNERS enabled; Approval removal setting enabled; Attacker has developer access; Approver approves initial MR
How it was found
- Create a GitLab repository and two developer users: one approver and one attacker.
- Enable CODEOWNERS and the setting to remove approvals when CODEOWNERS files change.
- Push a CODEOWNERS file requiring approval from the approver for all files.
- Make CODEOWNERS approval mandatory for the protected main branch.
- The attacker creates a branch, makes a safe change, pushes it, and opens a merge request.
- The approver approves the merge request.
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 GitLab, protected branches can require approval from specific people (CODEOWNERS) before code is merged. Normally, if you change the code after getting approval, that approval should be removed so the approver can review the new changes. Here, the approval stayed even after new code was pushed, letting an attacker sneak in malicious changes.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.