Keeping your GitHub Actions and workflows secure Part 1: Preventing pwn requests | GitHub Security Lab
skip to content Resources Open Source Community Enterprise / Security Lab Research Advisories CodeQL Wall of Fame Resources Open Source Community Enterprise Events Get Involved This post is the first in a series of posts about GitHub Actions security. Part 2 , Part 3 , Part 4 Secure your workflows with CodeQL: You can enable CodeQL for GitHub Actions to identify and fix the patterns described in this post. In this article, we’ll discuss some common security malpractices for GitHub Actions and workflows, and how to best avoid them.
Our examples are based on real-world GitHub workflow implementation vulnerabilities the GitHub Security Lab has reported to maintainers. Preventing pwn requests GitHub workflows can be triggered through a wide variety of repository events . This includes events related to incoming pull requests (PR). There exists a potentially dangerous misuse of the pull_request_target workflow trigger that may lead to malicious PR authors (i.e. attackers) being able to obtain repository write permissions or stealing repository secrets.
TL;DR : Combining pull_request_target workflow trigger with an explicit checkout of an untrusted PR is a dangerous practice that may lead to repository compromise. Any automated processing of PRs from an external fork is potentially dangerous and such PRs should be treated like untrusted input. It is common CI/CD practice to ensure that when a new PR is submitted that it does not break the build for your project, that no functionality regressions are introduced, and that tests are passing.
But when operating on untrusted PRs, such automated behavior can leave your repository exposed to abuse if you’re not careful. Since, by definition, a PR supplies code to any build or test logic in place for your project, attackers can achieve arbitrary code execution in a workflow runner operating on a malicious PR in a variety of ways. They may submit malicious changes to the existing build scripts like make or powershell files or redefine the build script in the package.json file.
They can simply write their payload as a new test that will be run with others. They can achieve code execution even before the actual build happens. Npm packages for example may have custom preinstall and postinstall scripts, so running npm install would already trigger any malicious code if the attackers added a new package reference. Any modern build orchestration is complex enough to have multiple code injection points.
This is why you should never checkout and build PRs from untrusted sources on a local machine without carefully examining the code for the PR. Due to the dangers inherent to automatic processing of PRs from forks, GitHub’s standard pull_request workflow trigger by default prevents write permissions and secrets access to the target repository. However, in some scenarios such access is needed to properly process the PR.
To this end the pull_request_target workflow trigger was introduced. Like how the introduction of Cross-Origin Resource Sharing (CORS) in the browser security model allowed a web site developer to relax the default Same Origin Policy (SOP), the introduction of pull_request_target trigger allowed a workflow writer to relax some restrictions to a target repository and must be used carefully. The main differences between the two triggers are: Workflows triggered via pull_request_target have write permission to the target repository.
They also have access to target repository secrets. The same is true for workflows triggered on pull_request from a branch in the same repository, but not from external forks. The reasoning behind the latter is that it is safe to share the repository secrets if the user creating the PR has write permission to the target repository already. pull_request_target runs in the context of the target repository of the PR, rather than in the merge commit.
This means the standard checkout action uses the target repository to prevent accidental usage of the user supplied code. These safeguards enable granting the pull_request_target additional permissions. The reason to introduce the pull_request_target trigger was to enable workflows to label PRs (e.g. needs review ) or to comment on the PR. The intent is to use the trigger for PRs that do not require dangerous processing, say building or running the content of the PR.
Together with the pull_request_target , a new trigger workflow_run was introduced to enable scenarios that require building the untrusted code and also need write permissions to update the PR with e.g. code coverage results or other test results. To do this in a secure manner, the untrusted code must be handled via the pull_request trigger so that it is isolated in an unprivileged environment. The workflow processing the PR should then store any results like code coverage or failed/passed tests in artifacts and exit.
The following workflow then starts on workflow_run where it is granted write permission to the target repository and access to repository secrets, so that it can download the artifacts and make any necessary modifications to the repository or interact with third party services that require repository secrets (e.g. API tokens). Below is an example of the intended usage in which the results of an unprivileged pull_request workflow are combined with a privileged workflow to leave a comment in response to a received PR: NOTE: The example was updated on June 17, 2026 ReceivePR.yml name : Receive PR # read-only repo token # no access to secrets on : pull_request : permissions : contents : read jobs : build : runs-on : ubuntu-latest steps : - uses : actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 # imitation of a build process - name : Build run : /bin/bash ./build.sh - name : Save PR number run : | mkdir -p ./pr echo ${{ github.event.number }} > ./pr/NR - uses : actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 with : name : pr path : pr/ CommentPR.yml name : Comment on the pull request # read-write repo token # access to secrets on : workflow_run : workflows : [ " Receive PR" ] types : - completed permissions : actions : read pull-requests : write jobs : elevated : runs-on : ubuntu-latest if : > github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' steps : - name : ' Download artifact' uses : actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 with : name : pr path : ${{ runner.temp }}/pr # do not extract in the workspace dir that may contain executable scripts run-id : ${{ github.event.workflow_run.id }} github-token : ${{ secrets.GITHUB_TOKEN }} - name : ' Comment on PR' uses : actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 with : github-token : ${{ secrets.GITHUB_TOKEN }} script : | var fs = require('fs'); const issue_number_raw = fs.readFileSync('${{ runner.temp }}/pr/NR', 'utf8').trim(); const issue_number = Number(issue_number_raw); if (!Number.isInteger(issue_number)) { throw new Error(`Invalid PR number in PR artifact: "${issue_number_raw}"`); } await github.rest.issues.createComment({ owner: context.repo.owner, repo: context.repo.repo, issue_number: issue_number, body: 'Everything is OK.
Thank you for the PR!' }); This example saves the PR number as a workflow artifact, but can be easily extended to pass code coverage messages or similar PR artifacts in the same way. Note that: The workflow_run context is different from the pull_request context and it doesn’t contain, for example, the PR number. The example above retrieves the number from the artifact. While the number itself is attacker controlled, it is an acceptable risk if the elevated workflow just comments on a PR.
Alternative option is to use github.rest.pulls.list API call filtering by github.event.workflow_run.head_sha and the base branch name in order to find the PR. Incoming data from artifacts is potentially untrusted. When used in a safe manner, like
The rest of this page is on the live site.