GitHub Actions makes it easier to automate builds, tests, code analysis, and deployment activities whenever developers create or update pull requests. However, workflows become more complicated when pull requests come from forked repositories.
A fork allows someone to create an independent copy of a repository, make changes, and submit those changes through a pull request. This is an important part of open-source collaboration, but it also introduces a security challenge.
The code submitted through a pull request may come from someone who does not have permission to access the original repository. That code could contain modified build scripts, malicious dependencies, or commands designed to expose credentials available to the workflow.
A workflow that executes untrusted code while holding access to repository secrets or write permissions can create a serious security risk.
This article explains how GitHub Actions handles pull requests from forks, how to configure workflow permissions, why pull_request_target requires special attention, and how to design safer automation for external contributions.
Understanding the Security Difference Between Pull Requests and Forks
When a developer creates a pull request from a branch within the same repository, the contributor usually has some level of repository access.
A fork is different. The contributor works in a separate repository and submits changes to the original repository through a pull request.
For example, consider a public repository:
organization/customer-portal
An external developer creates a fork:
external-user/customer-portal
The developer modifies the application and opens a pull request against the original repository.
GitHub Actions may execute workflows in response to this pull request. However, the workflow must not automatically trust the submitted code.
The contributor could modify files such as:
- GitHub Actions workflow definitions.
- Build scripts.
- Package installation scripts.
- Test execution commands.
- Dependency configuration files.
- Application startup scripts.
If a workflow executes these files while privileged credentials are available, an attacker may be able to access those credentials.
GitHub applies additional restrictions to workflows triggered by pull requests from forks to reduce this risk.
Understanding How GitHub Protects Fork Pull Requests
For the standard pull_request event, GitHub applies security restrictions when the pull request originates from a fork.
The important restrictions include:
- Repository and organization secrets are not passed to the workflow.
- The GITHUB_TOKEN receives read-only permissions by default.
- Fork workflow approval policies can prevent certain workflows from running automatically.
- The workflow runs against the pull request's merge commit.
These protections allow teams to run many validation workflows without granting external contributors access to privileged resources.
For example, a workflow that compiles an application and executes unit tests generally does not need permission to modify repository contents.
A basic workflow can use explicitly restricted permissions:
name: Pull Request Validation
on:
pull_request:
types:
- opened
- synchronize
- reopened
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '10.0.x'
- name: Restore dependencies
run: dotnet restore
- name: Build application
run: dotnet build --no-restore
- name: Run tests
run: dotnet test --no-build
This example uses read-only repository permissions and does not require application secrets.
The workflow can validate changes from external contributors while limiting the authority available to the job.
The important principle is that build and test workflows should receive only the permissions they actually require.
Why pull_request_target Requires Special Attention
One of the most important security considerations in GitHub Actions is the difference between pull_request and pull_request_target.
The pull_request_target event is designed for workflows that need to respond to pull request activity with access to the base repository's trusted context.
For example, a repository may want to automatically apply labels, assign reviewers, or post a comment when someone opens a pull request from a fork.
Unlike the standard pull_request event, pull_request_target runs using the workflow definition from the base repository's default branch.
It also receives elevated permissions. Its GITHUB_TOKEN can have write permissions, and the workflow can access repository and organization secrets according to their availability and configuration.
This makes it useful for trusted repository automation, but dangerous when combined with untrusted pull request code.
Consider this workflow:
name: Pull Request Build
on:
pull_request_target:
types:
- opened
- synchronize
permissions:
contents: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout pull request
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Build application
run: dotnet build
The problem is not simply that the workflow uses pull_request_target.
The problem is that it checks out and builds code submitted by an external contributor while running in a privileged workflow context.
The pull request can modify build scripts or other files that execute during the build. Those commands may attempt to access available credentials or modify repository resources.
This is commonly described as a pull request compromise or pwn request vulnerability.
For ordinary builds and tests, use pull_request instead.
Separating Trusted Automation From Untrusted Code Execution
A safer architecture separates operations that require privileged access from operations that execute contributor code.
For example, an application may need two different workflows:
- A validation workflow that builds and tests pull request code with restricted permissions.
- A repository automation workflow that applies labels or manages pull request metadata without executing contributor code.
The first workflow can use pull_request:
name: Pull Request Checks
on:
pull_request:
types:
- opened
- synchronize
- reopened
permissions:
contents: read
jobs:
validate:
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v4
- name: Build application
run: dotnet build
- name: Execute tests
run: dotnet test
The second workflow can use pull_request_target when elevated access is genuinely required.
For example, consider a workflow that applies a label to every newly opened pull request:
name: Pull Request Labeling
on:
pull_request_target:
types:
- opened
permissions:
pull-requests: write
issues: write
jobs:
label:
runs-on: ubuntu-latest
steps:
- name: Add review label
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
gh pr edit "$PR_NUMBER" \
--add-label "needs-review"
This workflow uses the pull request number as data and does not check out or execute the contributor's code.
It also uses narrowly scoped permissions instead of granting access to the entire repository.
When using pull_request_target, keep the workflow independent of untrusted source code. Avoid downloading artifacts produced by untrusted workflows, fetching fork branches for execution, or running scripts supplied by pull request contributors.
Restricting GITHUB_TOKEN Permissions
The GITHUB_TOKEN is automatically created for each GitHub Actions workflow run. It allows workflows to interact with GitHub APIs and repository resources.
However, granting broad permissions creates unnecessary risk.
GitHub Actions supports permission settings at the workflow and job levels.
For example:
permissions:
contents: read
pull-requests: read
issues: none
This configuration allows read access to repository contents and pull requests while disabling issue permissions.
A job that needs to publish a release may require additional permissions, but those permissions should not be granted to unrelated jobs.
For example:
jobs:
validate:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- run: echo "Running validation"
publish:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- run: echo "Publishing release"
The validation job does not need write access simply because another job publishes a release.
Using job-level permissions makes it easier to understand and control which operations can modify repository resources.
Avoiding Command Injection Through Pull Request Data
Another common security issue occurs when workflow expressions are inserted directly into shell commands.
Pull request titles, branch names, commit messages, and other contributor-controlled values should be treated as untrusted input.
Consider the following example:
- name: Print pull request title
run: echo "${{ github.event.pull_request.title }}"
The pull request title is inserted directly into the generated shell script.
An attacker could create a title containing shell syntax that changes how the command executes.
A safer approach is to pass the value through an environment variable:
- name: Print pull request title
env:
PR_TITLE: ${{ github.event.pull_request.title }}
run: |
printf '%s\n' "$PR_TITLE"
The value is now passed as environment data instead of being inserted directly into the shell command.
This does not make every use of external input automatically safe. Developers must still validate values and avoid passing them into commands in ways that allow unintended execution.
Handling Secrets and Self-Hosted Runners
Self-hosted runners introduce another important consideration.
A self-hosted runner may have access to internal networks, local configuration files, cached credentials, or other resources that are not available on a standard hosted runner.
If an untrusted pull request executes code on a self-hosted machine, the impact may extend beyond the current workflow.
For example, a malicious build script could attempt to access files left behind by previous jobs or communicate with internal services.
For workflows that execute untrusted code:
- Prefer isolated and ephemeral runners.
- Avoid storing long-lived credentials on runner machines.
- Do not share workspaces between trusted and untrusted jobs.
- Restrict network access to internal systems.
- Clean up temporary files after every job.
- Avoid using privileged runner groups for public pull request workflows.
Also review how artifacts and caches move between workflows. An artifact produced by an untrusted pull request should not automatically be treated as trusted input by a privileged workflow.
Common Security Mistakes
| Mistake |
Recommended approach |
| Using pull_request_target for ordinary builds |
Use pull_request |
| Checking out fork code in a privileged workflow |
Keep privileged workflows independent of contributor code |
| Granting write permissions to every job |
Apply minimum required permissions |
| Passing pull request values directly into shell commands |
Use environment variables and validate input |
| Storing long-lived credentials on shared runners |
Use isolated runners and short-lived credentials |
| Trusting artifacts from fork workflows |
Treat external artifacts as untrusted data |
| Enabling secrets for all fork workflows |
Grant secrets only to trusted workflows that require them |
Testing and Reviewing Workflow Security
Before enabling a workflow across an organization, test it using a forked repository.
Create a test pull request and verify:
- The expected workflow is triggered.
- The GITHUB_TOKEN has only the intended permissions.
- Repository secrets are not exposed to fork-triggered pull_request workflows.
- Build and test operations work without privileged credentials.
- Privileged automation does not execute contributor-controlled code.
- Shell commands handle pull request metadata safely.
- Self-hosted runners do not expose internal resources.
It is also useful to review workflow changes during pull request reviews. A seemingly small change to an event trigger, checkout reference, or permission setting can significantly alter the security boundary.
Organizations should establish workflow templates and reusable security checks so individual repositories do not need to solve these problems independently.
Summary
GitHub Actions makes it possible to automate development workflows for contributions from external developers, but forked pull requests must be treated as untrusted input.
The standard pull_request event provides important protections by restricting secrets and token permissions for fork-based workflows. The pull_request_target event serves a different purpose and should be reserved for trusted automation that needs access to the base repository context.
The most effective security approach is to separate code execution from privileged repository operations, apply minimum permissions, protect secrets, and use isolated runners.
A workflow should never receive more authority than it needs. By establishing this principle across repositories, development teams can support external contributions without unnecessarily exposing their GitHub organizations and automation infrastructure to security risks.
GitHub Actions securityfork pull requestspull_request_targetworkflow permissionsGitHub ActionsCI/CD security
Join the conversation! Your thoughts help the community grow.