Skip to content
Access control Write-up Intermediate

Gain write permission of repositories with a bug in GitHub Actions

A bug in GitHub Actions allowed an attacker to gain write permission to a repository by forking it, modifying the workflow file, and opening a pull request. The workflow then ran with a token that had write access, enabling the attacker to push changes to the main branch.

tyage Follow

Published on blog.tyage.net

Bounty$25,000 View original

GitHub Actions is a useful feature for the jobs, for example, building, publishing.

tyage (@tyage), on blog.tyage.net

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 6 min read.

Context

GitHub Actions is a feature of GitHub used to run jobs such as building and publishing. Workflows are defined in files under .github/workflows in a repository, and a workflow can be triggered by events including pull_request.

During a workflow run, GitHub makes a GITHUB_TOKEN available to the job. According to the report, this token allows the workflow to commit code, create packages and so on. The researcher's question was whether the GITHUB_TOKEN belonging to the target (parent) repository is shared with jobs triggered by a pull request coming from a fork. If it were, an unauthenticated or unauthorised user could obtain write permission on a repository they cannot commit to.

The report states that repositories with GitHub Actions enabled were affected, but only during a brief window: from 2021/02/04 18:42 UTC to 2021/02/05 13:35 UTC. It also states that protected branches were safe.

Setup

The test required the following preconditions, as described in the report:

  1. A victim repository containing a random workflow file in .github/workflows.
  2. The attacker must have no permission to commit to that repository.
  3. The attacker forks the repository and replaces the workflow file with their own.
  4. The attacker opens a pull request from the fork to the parent repository.

The report's captions show a pull request sent to tyage-test/repo-with-workflow and that repository being polluted. No specific tooling, versions or accounts beyond a GitHub account able to fork and open pull requests are described in the report.

Finding it

The researcher had seen a case where users abused GitHub Actions for DDoS: they forked many repositories, added a workflow file that runs a malicious program on the pull request event, and created pull requests so that their programs ran in the target repositories. The researcher was not sure that this explanation was correct, but guessed that those pull requests were sent to bypass the resource limits on GitHub Actions.

That raised a follow-on question: if resources are shared for pull request jobs, what about the GITHUB_TOKEN? Since the token permits committing code and creating packages, sharing the target repository's token with jobs triggered by a pull request would give write permission on the repository without authorisation. The researcher decided to test this.

Exploitation

The test was described as simple: send a pull request and wait.

Step 1: Prepare the victim repository

The victim prepares a repository that has a random workflow file in .github/workflows. The attacker must have no permission to commit to this repository.

Step 2: Fork and replace the workflow

The attacker forks the repository and replaces the workflow file with the following one. This workflow edits the main branch after the pull request event.

YAML
on: [pull_request]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Create local changes and commit
        run: |
          echo test > newfile
          git add newfile
          git config --local user.email "[redacted]"
          git config --local user.name "tyage"
          git commit -m "Add changes" -a
          git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}
          git push --force origin HEAD:refs/heads/main

What the important parts do:

  • on: [pull_request] makes the workflow run on the pull request event.
  • actions/checkout@v2 checks out the repository in the job.
  • The run block creates a file named newfile containing test, stages it, sets a local git identity, and commits with git commit -m "Add changes" -a.
  • git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY} rewrites the remote URL so that the push authenticates with the workflow's GITHUB_TOKEN against the repository named by GITHUB_REPOSITORY.
  • git push --force origin HEAD:refs/heads/main force-pushes the commit to the main branch of that repository.

The report does not state what the original random workflow file contained, nor any other details of the victim repository.

Step 3: Send the pull request and wait

The attacker sends a pull request to the parent repository and waits. The researcher reports that, surprisingly, the test succeeded: after the workflow finished, the victim repository had been modified.

(Send pull request to tyage-test/repo-with-workflow)

From the original on blog.tyage.net

(tyage-test/repo-with-workflow was polluted)

From the original on blog.tyage.net

The report does not include the pull request itself, the workflow run logs, or the resulting commit. It only states that the repository was modified after the workflow finished.

Root cause

The report does not describe the internal cause. The researcher's hypothesis was that the target repository's GITHUB_TOKEN might be shared with jobs triggered by a pull request from a fork. If that sharing occurred, the workflow supplied by the fork would run with a token that could write to the parent repository, which is what the test demonstrated. The exact mechanism inside GitHub Actions is not stated in the original, so any deeper explanation would be speculation.

On defences: the report notes that protected branches were safe.

Impact

Under the conditions described, an attacker with no commit permission on a repository could open a pull request from a fork and, through a workflow in that fork, modify the parent repository's main branch. The report states that repositories with GitHub Actions enabled were affected, but only within the window from 2021/02/04 18:42 UTC to 2021/02/05 13:35 UTC, and that protected branches were safe.

The report does not enumerate further consequences beyond editing the main branch, although it notes that the GITHUB_TOKEN allows committing code, creating packages and so on.

Fix

The researcher reported the bug to GitHub, and the report states that GitHub fixed it quickly. The timeline records the bug as dead at 2021/02/05 13:35 UTC and a message that the issue was fixed at 2021/02/05 17:58 UTC.

Variants to test

These are suggestions for testing similar behaviour on other targets; the original report does not list them.

  1. Fork a repository and change the workflow trigger to other events that a fork can cause, such as pull_request_target or issue events. A positive result is that a job triggered from the fork can write to the parent repository using the workflow token.
  2. In the forked workflow, print or use the token's permissions against the parent repository, for example by pushing a branch or creating a package. A positive result is a successful write that the attacker's own account could not perform.
  3. Repeat the test against a repository with branch protection on the default branch. A positive result would be a push that branch protection should have blocked; the original report states protected branches were safe, so this checks whether that holds.
  4. Test whether the token is scoped to the fork or the parent by pushing to both. A positive result is a push to the parent succeeding while the attacker has no commit permission there.
  5. Check whether the same behaviour appears with pull requests from branches within the same repository rather than from forks. A positive result is a write performed by a user who lacks direct commit permission.

Each of these should be run only against repositories you own or are authorised to test.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 13 min read.

What happened, in plain words

This is a story about a website called GitHub. GitHub is a place where people store computer code online, a bit like a shared online folder that keeps a history of every change. Many people can work on the same project, and GitHub keeps track of who changed what and when.

GitHub has a feature called GitHub Actions. Think of it as a robot helper that runs chores for you automatically. You write a list of instructions, and whenever something happens (for example, someone sends in a suggested change), the robot wakes up and follows those instructions. Those instructions are called a workflow.

The researcher in this report found a bug: a way for someone with no permission on a project to change that project's main code, just by sending in a suggested change. The bug only existed for a short time, from 2021/02/04 18:42 UTC to 2021/02/05 13:35 UTC. Projects that had turned on GitHub Actions were affected. Projects that had turned on a protection setting for their main branch were safe.

The researcher reported it to GitHub, GitHub fixed it quickly, and the researcher was paid a $25,000 bounty. A bounty is a reward a company pays to someone who finds and reports a security problem responsibly.

This beginner version walks through the same story as the technical write-up, in the same order, explaining every idea from scratch.

Some background ideas first

Before the story, here are a few everyday ideas you will need.

  • Repository (repo): a project folder stored on GitHub. It holds files and the full history of changes.
  • Fork: your own personal copy of someone else's repository, kept on GitHub under your account. You can change your fork freely, but you do not automatically get to change the original.
  • Pull request (PR): a polite request that says "please take the changes I made in my copy and add them to your original project". The owner of the original project can review it and accept or reject it.
  • Branch: a separate line of work inside a repository. The main line is often called main. Changing main means changing the project's real code.
  • Commit: a saved change, like pressing save with a short note about what you changed.
  • Push: sending your saved commits up to GitHub so others can see them.
  • Token: a long secret string that works like a temporary password. If a program has the right token, the website treats it as allowed to do certain things. It is similar to a wristband at an event: show the wristband and you get into the areas it covers.
  • API: a way for one computer program to talk to another over the internet, like a waiter taking your order to the kitchen. You do not go into the kitchen yourself; you ask through the waiter.
  • Workflow: the list of instructions the GitHub Actions robot follows.
  • Event: the trigger that wakes the robot up, for example "a pull request was opened".
  • DDoS: flooding a website with so much traffic that it struggles to serve real users. It is like a crowd all pushing through one door at once.
  • Broken access control: a class of bug where the website lets someone do something they should not be allowed to do, because it checks permission in the wrong place or not at all. It is like a hotel cleaner's key that accidentally opens every guest room.
  • Protected branch: a setting on a branch that blocks risky actions, such as force pushing over the existing history.

The feature being tested

GitHub Actions is a feature of GitHub used to run jobs such as building and publishing. A workflow is defined in a file placed under .github/workflows inside a repository. A workflow can be triggered by events, and one of those events is pull_request, which fires when someone opens or updates a pull request.

During a workflow run, GitHub makes a GITHUB_TOKEN available to the job. According to the report, this token allows the workflow to commit code, create packages and so on. The researcher's question was simple: when a pull request comes from a fork (someone else's copy), does the job run with the token of the original (parent) repository? If it did, then someone with no permission on the parent repository could use that token to write to it.

The report states that repositories with GitHub Actions enabled were affected, but only during a brief window: from 2021/02/04 18:42 UTC to 2021/02/05 13:35 UTC. It also states that protected branches were safe.

Setting up the test

The test needed a few preconditions, as described in the report:

  1. A victim repository containing a random workflow file in .github/workflows.
  2. The attacker must have no permission to commit to that repository.
  3. The attacker forks the repository and replaces the workflow file with their own.
  4. The attacker opens a pull request from the fork to the parent repository.

The report's captions show a pull request sent to tyage-test/repo-with-workflow and that repository being polluted. The report does not describe any specific tools, versions or accounts beyond a GitHub account able to fork and open pull requests.

Why these steps? The point is to prove that a person who is not allowed to change the project can still change it. If the attacker already had commit permission, there would be nothing to prove. So the attacker must be an outsider, and the only action they take is sending a pull request, which is normally a harmless, reviewable request.

How the researcher found the idea

The researcher had seen a case where users abused GitHub Actions for DDoS. In that case, people forked many repositories, added a workflow file that runs a malicious program on the pull request event, and created pull requests so that their programs ran inside the target repositories. The researcher was not sure that this explanation was correct, but guessed that those pull requests were sent to bypass the resource limits on GitHub Actions.

That raised a follow-on question: if resources are shared for pull request jobs, what about the GITHUB_TOKEN? Since the token permits committing code and creating packages, sharing the target repository's token with jobs triggered by a pull request would give write permission on the repository without authorisation. The researcher decided to test this.

In plain words: if the robot that runs your pull request job is somehow borrowing the original project's wristband, then the robot can do things the outsider could not do themselves. The researcher wanted to check whether that borrowing actually happens.

Carrying out the attack

The test was described as simple: send a pull request and wait.

Step 1: Prepare the victim repository

The victim prepares a repository that has a random workflow file in .github/workflows. The attacker must have no permission to commit to this repository.

Step 2: Fork and replace the workflow

The attacker forks the repository and replaces the workflow file with the following one. This workflow edits the main branch after the pull request event.

YAML
on: [pull_request]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Create local changes and commit
        run: |
          echo test > newfile
          git add newfile
          git config --local user.email "[redacted]"
          git config --local user.name "tyage"
          git commit -m "Add changes" -a
          git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}
          git push --force origin HEAD:refs/heads/main

Line by line, in plain words:

  • on: [pull_request] tells GitHub Actions to run this workflow whenever a pull request event happens. This is the trigger, the alarm clock that wakes the robot.
  • jobs: starts the list of jobs. A job is one unit of work.
  • build: is the name of this job. The name is arbitrary.
  • runs-on: ubuntu-latest says the job runs on a fresh Ubuntu Linux machine provided by GitHub. Think of it as a clean rented computer that is thrown away afterwards.
  • steps: starts the list of steps the robot performs in order.
  • - uses: actions/checkout@v2 downloads the repository's files onto that rented computer, so the robot has something to work with.
  • - name: Create local changes and commit is a human friendly label for the next step.
  • run: | means the following lines are shell commands to run on the rented computer.
  • echo test > newfile creates a new file called newfile containing the word test. This is the change the attacker wants to make.
  • git add newfile stages the new file, meaning it marks it to be included in the next commit.
  • git config --local user.email "[redacted]" and git config --local user.name "tyage" set the name and email recorded on the commit. Git refuses to commit without an identity, so this is just bookkeeping.
  • git commit -m "Add changes" -a saves the change as a commit with the message Add changes. The -a flag includes all tracked changes.
  • git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY} rewrites the address the robot will push to. The important part is ${{ secrets.GITHUB_TOKEN }}: this is where the workflow's token is inserted. The address also uses ${GITHUB_REPOSITORY}, which is the repository name the job is running for. In effect, the robot will authenticate with the token against that repository.
  • git push --force origin HEAD:refs/heads/main sends the commit to the main branch of that repository. The --force flag means "overwrite whatever is there", which is why branch protection matters.

The report does not state what the original random workflow file contained, nor any other details of the victim repository.

Step 3: Send the pull request and wait

The attacker sends a pull request to the parent repository and waits. The researcher reports that, surprisingly, the test succeeded: after the workflow finished, the victim repository had been modified.

(Send pull request to tyage-test/repo-with-workflow)

From the original on blog.tyage.net

(tyage-test/repo-with-workflow was polluted)

From the original on blog.tyage.net

The report does not include the pull request itself, the workflow run logs, or the resulting commit. It only states that the repository was modified after the workflow finished. The two images show the pull request being sent to tyage-test/repo-with-workflow and the repository afterwards being polluted, according to the captions in the original.

Why it worked

The researcher's hypothesis was that the target repository's GITHUB_TOKEN might be shared with jobs triggered by a pull request from a fork. If that sharing occurred, the workflow supplied by the fork would run with a token that could write to the parent repository, which is what the test demonstrated. The exact mechanism inside GitHub Actions is not stated in the original, so any deeper explanation would be speculation.

On defences: the report notes that protected branches were safe.

In everyday terms: the researcher suspected the robot was handed the original project's wristband instead of a visitor's wristband. The report notes protected branches were safe but does not explain why.

What the impact was

Under the conditions described, an attacker with no commit permission on a repository could open a pull request from a fork and, through a workflow in that fork, modify the parent repository's main branch. The report states that repositories with GitHub Actions enabled were affected, but only within the window from 2021/02/04 18:42 UTC to 2021/02/05 13:35 UTC, and that protected branches were safe.

The report does not enumerate further consequences beyond editing the main branch, although it notes that the GITHUB_TOKEN allows committing code, creating packages and so on.

How it was fixed

The researcher reported the bug to GitHub, and the report states that GitHub fixed it quickly. The timeline records the bug as dead at 2021/02/05 13:35 UTC and a message that the issue was fixed at 2021/02/05 17:58 UTC.

A related story

Interestingly, one day before the researcher found this bug, another people had found a similar bug. The report links to a blog post titled "Stealing arbitrary GitHub Actions secrets". Though this is an independent bug, the bug also exists in GitHub Actions and is triggered by a crafted pull request.

Timeline

  • 2021/02/04 18:42 UTC The bug was born
  • 2021/02/05 12:09 UTC The researcher reported this issue to the GitHub Bug Bounty Program
  • 2021/02/05 13:35 UTC The bug was dead
  • 2021/02/05 17:58 UTC The researcher received a message that the issue was fixed
  • 2021/03/31 13:14 UTC GitHub awarded a $25,000 bounty

Things you could test yourself (only on your own projects)

These are suggestions for testing similar behaviour on other targets; the original report does not list them. Only run them against repositories you own or are authorised to test.

  1. Fork a repository and change the workflow trigger to other events that a fork can cause, such as pull_request_target or issue events. A positive result is that a job triggered from the fork can write to the parent repository using the workflow token.
  2. In the forked workflow, print or use the token's permissions against the parent repository, for example by pushing a branch or creating a package. A positive result is a successful write that the attacker's own account could not perform.
  3. Repeat the test against a repository with branch protection on the default branch. A positive result would be a push that branch protection should have blocked; the original report states protected branches were safe, so this checks whether that holds.
  4. Test whether the token is scoped to the fork or the parent by pushing to both. A positive result is a push to the parent succeeding while the attacker has no commit permission there.
  5. Check whether the same behaviour appears with pull requests from branches within the same repository rather than from forks. A positive result is a write performed by a user who lacks direct commit permission.

Glossary

  • API: a way for one computer program to talk to another over the internet, like a waiter carrying orders to a kitchen.
  • Attacker: the person trying to do something they are not supposed to be allowed to do.
  • Bounty: a reward a company pays to someone who finds and reports a security problem responsibly.
  • Branch: a separate line of work inside a repository; main is the usual main line.
  • Broken access control: a bug where the website lets someone do something they should not be allowed to do, because permission is checked in the wrong place or not at all.
  • Commit: a saved change in a repository, with a short note describing it.
  • Event: the trigger that starts a workflow, for example a pull request being opened.
  • Fork: your own copy of someone else's repository, kept on GitHub under your account.
  • GitHub: a website for storing and sharing code, with a history of every change.
  • GitHub Actions: a feature of GitHub that runs automated jobs, called workflows, in response to events.
  • GITHUB_TOKEN: a secret token that GitHub makes available to a workflow run, which the report says allows committing code, creating packages and so on.
  • Job: one unit of work inside a workflow, run on a machine provided by GitHub.
  • Main branch: the main line of a repository, often called main; changing it changes the project's real code.
  • Parent repository: the original repository that a fork was copied from.
  • Protected branch: a branch setting that blocks risky actions such as force pushing over existing history.
  • Pull request (PR): a request to take changes from your copy and add them to the original project.
  • Push: sending saved commits up to GitHub so others can see them.
  • Repository (repo): a project folder stored on GitHub, holding files and the full history of changes.
  • Token: a long secret string that works like a temporary password, granting whatever permissions it covers.
  • Workflow: the list of instructions that GitHub Actions follows when an event happens.
  • Workflow file: the file under .github/workflows that defines a workflow.
  • Write permission: the ability to change a repository, for example by committing or pushing.

More by tyage

View all

1 more disclosed report, best first.

More from GitHub

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.