[gitlabhq/terraform-provider-gitlab] Unauthorized repository modification or secrets exfiltration via the `pr-acceptance.yml` workflow
A GitHub Actions workflow used the pull_request_target trigger but then checked out and ran code from the pull request's fork, so untrusted code executed with the base repository's privileges. An attacker could use the resulting token to push to the base repository, bypass review rules, or exfiltrate secrets.
GitLab
Originally disclosed on GitLab
- Fixed in 15 days
- Disclosed after 15 days
HackerOne report #1479435 by nikitastupin on 2022-02-12, assigned to GitLab Team:
Report | How To Reproduce
Report
Hi team!
The pr-acceptance.yml GitHub workflow is vulnerable to unauthorized modification of the base repository or secrets exfiltration.
Here is the vulnerable code fragment:
...
on:
# The pull_request_target event type fires for pull requests, but in the context of the target
# project.
pull_request_target:
# Acceptance tests are unnecessary to run on some types of PRs.
paths-ignore:
- 'docs/**'
- 'examples/**'
- 'README.md'
- 'CHANGELOG.md'
- 'CONTRIBUTING.md'
...
# Check out the pull request code (as opposed to the target project).
- uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}
...
# CAUTION: EXECUTING UNTRUSTED CODE.
# This is made safe because we have not referenced any secrets or GitHub tokens.
- run: make testacc-up
...
By explicitly checking out and running code from a fork the untrusted code is running in an environment that is able to push to the base repository and to access secrets.
Steps To Reproduce
I haven't tested this vulnerability against the original gitlabhq/terraform-provider-gitlab repository because this would disclose the vulnerability to the public. Instead I've created a mirror of the gitlabhq/terraform-provider-gitlab repository to verify the proof of concept. I suggest you follow the similar way when reproducing the vulnerability.
Environment setup
- Login to GitHub and create an empty public GitHub repository.
- Follow steps described in Mirroring a repository to create a mirror of
gitlabhq/terraform-provider-gitlab.
- Ensure that the default branch is
mainfor the mirror. - Ensure that Actions are enabled for the mirror.
Proof of Concept
- Login to GitHub as a malicious user.
- Create a fork of the mirror.
- Modify the
GNUmakefilefile: change thetestacc-upscript toecho "untrusted code execution!". - Create a pull request from the fork to the mirror.
- Navigate to the workflow run logs to verify that the "untrusted code execution!" string is indeed printed in the logs.
If you need a payload that pushes to the base repo or exfiltrates secrets, please let me know in the comments!
Description
Exfiltration of the GITHUB_TOKEN
In most cases it's possible to exfiltrate the GITHUB_TOKEN with the following technique.
find /home/runner/work/. -type f -name config | xargs cat | base64; while true; do echo 5; sleep 5; done
This works because the actions/checkout action persists credentials with a local git config by default.
There are rare cases when the GITHUB_TOKEN is not referenced in a workflow and is not persisted with a local git config so the above technique doesn't work. However, since GITHUB_TOKEN secret is always passed to a runner even if not directly referenced in a workflow. So after having an arbitrary code execution one could dump memory of the Runner.Worker process and grep the dump for the GITHUB_TOKEN secret.
Using the GITHUB_TOKEN a malicious user can push to the base repository or manipulate it in some other way.
Exploitation with protected branches
You may have protected branches enabled in the repository. Particularly, you may have the require pull request reviews before merging rule enabled.
However, the rule can be bypassed using the GITHUB_TOKEN because the token has write access to the repository thus can approve a malicious pull request. You may find more details in the Bypassing required reviews using GitHub Actions blog post.
The bypass won't work if code owners approval required on all files. In this case a malicious user still should be able:
- Push to a non-protected branch (or create a new one) to create a release out of it.
- Push to an already approved pull request and merge it to a protected branch unless
Dismiss stale pull request approvals when new commits are pushedenabled. - Exfiltrate secrets.
Remediation
The common fix bypass is to make a pull request to another branch with the vulnerable code. Because of this consider remediating the issue in every branch of the repo.
To remediate the issue, you could:
- Avoid using
pull_request_targetif the workflow doesn’t need write repository permissions and doesn’t use any repository secrets. They can simply use thepull_requesttrigger instead. - Assign repository privileges only where needed explicitly through
pull_requestandworkflow_run. - Add a condition to the
pull_request_targetto run only if a certain label is assigned the PR, like safe to test that indicates the PR has been vetted by someone with write privileges to the target repository.
Please refer to the Keeping your GitHub Actions and workflows secure: Preventing pwn requests for more details.
To harden a repository against workflow vulnerabilities you may:
- Use permissions.
- Use protected branches possibly in combination with a CODEOWNERS file.
- Disable the "Allow GitHub Actions to approve pull requests" setting under the Actions tab in organization settings.
Supporting Material/References
- Keeping your GitHub Actions and workflows secure: Preventing pwn requests.
- Following the principle of least privilege section of the Keeping your GitHub Actions and workflows secure Part 3: How to trust your building blocks blog post.
Impact
The vulnerability allows for unauthorized modification of the base repository and secrets exfiltration.
The list of secrets I have found in the repository workflows:
${{ secrets.GITHUB_TOKEN }}${{ secrets.GPG_PRIVATE_KEY }}${{ secrets.LICENSE_ENCRYPTION_PASSWORD }}${{ secrets.PASSPHRASE }}
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
The workflow combined the privileged pull_request_target trigger with an explicit checkout of the pull request head commit and then executed that untrusted code.
Impact
An attacker could modify the base repository without authorisation and exfiltrate secrets, and could potentially bypass required reviews unless code owner approval is required on all files.
What the attacker needed
A GitHub account; a fork of the repository; Actions enabled on the repository
The fix
The workflow should avoid pull_request_target when it needs no write permissions or secrets, or restrict it to vetted pull requests via a label and least-privilege permissions.
How it was found
- Create a mirror of the target repository with Actions enabled and main as the default branch.
- As a malicious user, fork the mirror.
- Change the testacc-up script in the GNUmakefile to print a marker string.
- Open a pull request from the fork to the mirror.
- Check the workflow run logs to confirm the marker string was printed.
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.
GitHub Actions are automated jobs that run when something happens in a repository, such as a pull request. The pull_request_target trigger runs the job with the trusted base repository's permissions, but this workflow then downloaded and ran the contributor's own code, so a stranger's code got the trusted permissions. That let the stranger act as the repository owner, for example pushing changes or reading stored passwords.
What is Info disclosure?
Data the application should keep private (personal data, internal details, other users' records) reaches someone it should not.