Lack of filtering in GIT_FETCH_EXTRA_FLAGS CI option allows Developer to run a protected pipeline from that can disclose protected CI/CD variable
A user with developer role could run a pipeline on a protected branch using arbitrary code from another branch by setting the GIT_FETCH_EXTRA_FLAGS CI variable. This allowed execution of untrusted code in the context of a protected branch, potentially exposing protected CI/CD variables.
GitLab
Originally disclosed on GitLab
- Fixed in 12 months
- Disclosed after 12 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 #2033561 by samuellg on 2023-06-21, assigned to @ottilia_westerlund:
Report | Attachments | How To Reproduce
Report
Summary
Any user allowed to run pipelines on a branch (even a protected one) can run it with arbitrary code from another branch/commit.
Steps to reproduce
- The attacker must have at least a
developerrole on the project and he must be allowed to run pipelines on protected branches (i.e. he must be able to accept Merge Requests). - The attacker creates a branch on the project. Let's call this branch
somebranch. He commits arbitrary code, that will later be executed within the context of a protected branch pipeline. - If the target branch is
main, the attackers note down the commit ID of themainbranch HEAD reference. For example:c709254daa03cda29815acf7117ed8842aecda70. - The attacker runs a pipeline on the
mainbranch, and add the following CI variables from the "Run pipeline" form:
GIT_FETCH_EXTRA_FLAGS : c709254daa03cda29815acf7117ed8842aecda70 --force --refmap='' somebranch:refs/heads/c709254daa03cda29815acf7117ed8842aecda70
4. All the jobs executed in the pipelines will be executed with the code from somebranchinstead of the code from main. In the example provided in the attached project, the file somefile is read from somebranch instead of from main.
Impact
Arbitrary code can be pushed to production environment. This is not visible on the attacked branch. This can also allow an attacker to steal production credentials stored within the protected CI variables.
Examples
Example here (this project has also been exported and attached to this report):
https://gitlab.com/slegoff/refmap
The branch and commit IDs are the one used in the "Steps to reproduce section".
What is the current bug behavior?
Anyone that is able to run a pipeline can choose the commit that will be checked out when running the job. The pipeline will still run within the context of the original branch (e.g. if the branch is protected, the protected variables will be available).
What is the expected correct behavior?
https://drive.google.com/file/d/10gpKnRDItwWi9Ajn2NTQq-Mq7mSfQ_ZF/view?usp=drive_link
When running a pipeline on a branch, we expect that jobs run with the code from that same branch, no matter which options are provided by the user.
Relevant logs and/or screenshots
See the attached files.
Output of checks
This bug happens on GitLab.com
Impact
The attacker can have all the pipelines job run on arbitrary code. The jobs cannot be edited through the editing of the .gitlab-ci.yml file though, because as far as I know this file is not parsed by the runner.
Attachments
Warning: Attachments received through HackerOne, please exercise caution!
- 2023-06-21_15-30-729_slegoff_refmap_export.tar.gz
- Capture_d_e_cran_2023-06-21_a__17.39.56.png
- Capture_d_e_cran_2023-06-21_a__17.41.27.png
- Capture_d_e_cran_2023-06-21_a__17.43.34.png
How To Reproduce
Please add reproducibility information to this section:
Proposal(s)
Option 1
- Ensure that the runner git checkout command always checks out by SHA, instead of inferring the branch name
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 runner's git checkout command used the branch name from the pipeline context without validating that the commit matched the protected branch, allowing the GIT_FETCH_EXTRA_FLAGS variable to override the checkout reference.
Impact
Arbitrary code could be executed in the context of a protected branch pipeline, potentially allowing theft of protected CI/CD variables and pushing code to production.
What the attacker needed
Developer role on the project; ability to run pipelines on protected branches
How it was found
- The attacker has at least developer role and can run pipelines on protected branches.
- The attacker creates a branch with arbitrary code and notes the commit ID of the target protected branch's HEAD.
- The attacker runs a pipeline on the protected branch and sets the GIT_FETCH_EXTRA_FLAGS variable to point to the attacker's branch and commit.
- All jobs in the pipeline execute code from the attacker's branch instead of the protected branch.
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, pipelines run jobs that check out code from a branch. A user with developer access can start a pipeline on a protected branch, which normally runs trusted code. By setting a special variable, the attacker made the pipeline run code from a different branch, so their own code executed with the protected branch's permissions and secrets.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.