Skip to content
Info disclosure Disclosed report Intermediate

[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:

YAML
...  
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.

Please note that workflows triggered by pull_request_target events are run in the context of the base branch. Since the base branch is considered trusted, workflows triggered by these events will always run, regardless of approval settings.

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
  1. Login to GitHub and create an empty public GitHub repository.
  2. Follow steps described in Mirroring a repository to create a mirror of

gitlabhq/terraform-provider-gitlab.

  1. Ensure that the default branch is main for the mirror.
  2. Ensure that Actions are enabled for the mirror.
Proof of Concept
  1. Login to GitHub as a malicious user.
  2. Create a fork of the mirror.
  3. Modify the GNUmakefile file: change the testacc-up script to echo "untrusted code execution!".
  4. Create a pull request from the fork to the mirror.
  5. 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.

Bash
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:

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_target if the workflow doesn’t need write repository permissions and doesn’t use any repository secrets. They can simply use the pull_request trigger instead.
  • Assign repository privileges only where needed explicitly through pull_request and workflow_run.
  • Add a condition to the pull_request_target to 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:

Supporting Material/References

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:

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

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

  1. Create a mirror of the target repository with Actions enabled and main as the default branch.
  2. As a malicious user, fork the mirror.
  3. Change the testacc-up script in the GNUmakefile to print a marker string.
  4. Open a pull request from the fork to the mirror.
  5. 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.

More from GitLab

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Information disclosure

The lesson behind this report.

Data the application should keep private (personal data, internal details, other users' records) reaches someone it should not.