Automation
GitHub Actions: contain pull_request_target before restoring production writes
A production runbook for containing a privileged pull_request_target workflow, tracing untrusted code execution, rotating exposed credentials and proving negative canaries before restoring repository or Azure writes.
A repository uses pull_request_target to label pull requests from forks and publish an authenticated preview status. A workflow change then checks out the pull request head and runs a package command. The job still comes from the trusted default branch, but the process now executes contributor-controlled code while holding a privileged GITHUB_TOKEN, repository secrets or an Azure OIDC path.
Disabling the workflow stops new runs; it does not prove that earlier runs failed to copy a token, modify a release, poison an external system or leave a durable credential behind. The incident must separate the event that granted trust, the exact commit that ran, the code and artifacts consumed by the runner, every credential available to the job, and every write the job could reach.
The running case is a public application repository whose workflow comments on pull requests and can update an Azure test environment. The objective is one decision: keep the privileged automation disabled, replace it with an unprivileged pull_request workflow plus a narrow trusted follow-up, or restore pull_request_target only after negative canaries prove that fork code cannot execute with repository or cloud write access.
Stop new privileged executions and preserve the evidence
Disable the affected workflow or block the event at repository or organization policy level. Cancel queued runs, but do not delete run logs, artifacts or the pull request. Record the workflow path, run IDs, triggering actors, fork repositories, head SHAs, runner type, environment, permissions and UTC window.
incident: INC-GHA-318
repository: platform/orders-api
workflow: .github/workflows/pr-automation.yml
event: pull_request_target
window_utc:
start: 2026-11-07T08:00:00Z
end: 2026-11-07T11:30:00Z
containment:
workflow_disabled: true
queued_runs_cancelled: true
production_environment_paused: true
azure_federation_disabled: true
evidence:
- workflow file at each run SHA
- event payload and pull request head SHA
- job permissions and referenced secrets
- runner labels and persistence model
- logs, artifacts, caches and deployments
stop_conditions:
- an untrusted checkout was executed
- a self-hosted runner was reused after the run
- token or secret exposure cannot be bounded
- a production write cannot be attributed Preserve both the workflow revision from the base branch and the untrusted pull request revision. A later workflow fix does not describe what an earlier run executed. Treat log redaction as presentation hygiene, not as proof that a malicious process could not read or exfiltrate a secret.
Reconstruct the trust boundary for each job
pull_request_target runs the workflow definition in the context of the base repository. That is useful for labeling or commenting on fork pull requests, but dangerous when a later step fetches the fork head and executes anything from it.
Draw the path for every job:
- Trigger: which event and actor created the run?
- Workflow source: which base commit supplied the YAML and referenced local actions?
- Untrusted inputs: pull request title, body, labels, branch name, repository name, head SHA, artifacts or API responses.
- Execution: checkout, dependency installation, build scripts, tests, local actions, generated shell, configuration loaders and post-job hooks.
- Authority:
GITHUB_TOKENpermissions, secrets, environment secrets, OIDCid-token, cloud roles, package registry credentials and runner network access. - Effects: comments, tags, releases, packages, workflow changes, deployments, Key Vault reads and Azure resource writes.
Checking out a fork is not the only execution path. npm install can run lifecycle scripts; a test runner loads contributor code; a Makefile is executable logic; a local action under .github/actions can be replaced by the pull request; and a downloaded artifact can carry scripts or configuration interpreted by a privileged job.
Identify the vulnerable shape in the deployed workflow
Read the workflow at the base SHA used by each run. Search for explicit pull request refs, fork repositories, git fetch, gh pr checkout, downloaded artifacts and expressions that place untrusted text directly into a shell script.
# Vulnerable pattern: do not deploy.
on:
pull_request_target:
permissions:
contents: write
pull-requests: write
id-token: write
jobs:
preview:
runs-on: self-hosted
steps:
- uses: actions/checkout@v6
with:
ref: ${{ github.event.pull_request.head.sha }}
- run: npm ci
- run: npm run preview
- run: az deployment group create --resource-group rg-preview --template-file infra/main.bicep The security failure is the combination, not the checkout line alone: untrusted material is executed in a job that holds authority. Also inspect reusable workflows and composite actions. A top-level workflow with contents: read may call a reusable workflow that requests id-token: write, accesses an environment, or runs on a persistent self-hosted runner.
Create a per-job authority table rather than assigning one risk level to the whole file.
triage
untrusted code executed: no
github token: pull-requests write
secrets: none
runner: GitHub-hosted ephemeral
allowed effect: add one label and one comment
preview-build
untrusted code executed: yes
github token: contents write
oidc: write
secrets: registry token
runner: self-hosted reused
reachable effects: repository, packages, Azure preview subscription
decision
contain preview-build as credential exposure
keep triage disabled until its inputs and permissions are independently proven Bound credential and runner exposure
For each affected run, inventory credentials that were available, not only credentials visibly used in the YAML. Include organization, repository and environment secrets; inherited secrets in reusable workflows; GITHUB_TOKEN; OIDC eligibility; package credentials; cloud sessions; and tokens left on a self-hosted runner.
If untrusted code ran, assume it could read the job environment, workspace, process arguments, generated scripts and reachable credential files. Rotate or revoke according to capability and lifetime:
- expire or invalidate cloud sessions and disable the federated credential until the workflow contract is repaired;
- rotate long-lived repository, registry and application secrets exposed to the job;
- review tags, releases, packages, branch changes, workflow edits and deployments made during the window;
- quarantine persistent runners, preserve their disks when required, then rebuild them from a trusted image;
- invalidate or isolate caches and artifacts that crossed from the untrusted job into trusted jobs.
OIDC removes a long-lived cloud secret, but it does not remove the incident. A compromised job with id-token: write can request a short-lived token if the cloud trust conditions accept its claims. Review the federated credential subject, audience, repository, environment and workflow restrictions, then correlate Azure sign-ins and activity logs with the run window.
Split untrusted CI from privileged automation
The preferred repair is two explicit trust domains. Build and test fork code under pull_request with a read-only token and no secrets. Perform labeling or status updates in a separate trusted workflow that never executes fork code. If a privileged follow-up consumes an artifact or metadata from the untrusted run, treat it as data: validate its schema, size and digest, and never execute its contents.
name: untrusted-pr-ci
on:
pull_request:
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- run: npm ci --ignore-scripts
- run: npm test
# A separate base-branch workflow may label the PR.
# It must not checkout, build, source or execute the fork head.
# Give that job only pull-requests: write, with contents: read and id-token: none. --ignore-scripts reduces one execution surface but does not make arbitrary tests safe. The entire untrusted CI job remains unprivileged. Conversely, moving the same checkout into a workflow_run job does not make it trusted: artifacts and metadata produced by the first workflow remain attacker-controlled.
For Azure deployment, require a dedicated trusted trigger, a protected environment and a federated credential constrained to the intended repository and deployment context. The build job must not be able to request the production identity, and the deployment job must consume an immutable, verified artifact rather than rebuild the pull request.
Harden the narrow privileged workflow
If pull_request_target is genuinely required for triage, keep it data-only. Set workflow permissions to {} or contents: read by default, then grant a single job only the permission it needs. Do not grant id-token: write, expose environment secrets, use a production runner group or enable write-capable caches in that workflow.
Treat pull request fields as untrusted strings. Pass them through environment variables to a program that treats them as data, validate expected formats, and avoid interpolating titles, branch names or bodies directly into run commands. Pin third-party actions to reviewed immutable commits where the repository policy requires it, and audit local and reusable actions as part of the same authority chain.
The privileged job should be easy to describe in one sentence: “read pull request metadata and add the needs-review label.” If it also builds, signs, publishes, deploys or accesses an internal network, split those responsibilities.
Prove the repair with negative canaries
A green workflow is insufficient. Create a fork pull request that contains harmless canaries in every likely execution surface: package lifecycle scripts, test command, Makefile, local action and configuration file. Each canary should write a unique marker only if executed. The privileged workflow must complete its narrow metadata action without producing any marker.
canary_pull_request:
source: controlled-fork
markers:
package_preinstall: canary-preinstall
test_command: canary-test
makefile: canary-make
local_action: canary-local-action
config_loader: canary-config
expected_privileged_run:
label_added: needs-review
fork_checkout: false
fork_artifact_executed: false
marker_observed: false
github_token_write_scope: pull-requests-only
oidc_request_possible: false
repository_write_denied: true
azure_write_denied: true
promotion_gate:
- workflow SHA reviewed
- reusable workflows included in review
- ephemeral runner used
- audit evidence retained
- rollback disables the privileged workflow Add a positive control to the untrusted CI workflow so the canary test command does execute there, without secrets or write permissions. This proves that the test is meaningful and that privilege separation, rather than a broken canary, explains the denial.
Decide, restore or roll back
Keep the privileged workflow disabled when any affected run executed untrusted material, credential exposure remains unbounded, a persistent runner has not been rebuilt, or repository and Azure effects cannot be reconciled.
Promote the split design when fork code runs only in the unprivileged workflow, the privileged workflow handles metadata as data, permissions are minimal, OIDC is absent from the pull request path, and positive and negative canaries pass.
Restore a narrow pull_request_target workflow only when the use case cannot be served by pull_request, it never executes or sources fork content, and its only write permission matches the documented effect. Roll back by disabling that workflow or enforcing an event policy; do not fall back to a broad token or static Azure secret.
After restoration, monitor workflow changes, new uses of pull_request_target, permissions expansion, self-hosted runner selection and OIDC additions. A later refactor can reconnect the two trust domains even if today’s YAML is safe.
Conclusion
The dangerous unit is not the event name alone. It is a privileged execution path that lets attacker-controlled code, configuration or artifacts reach a token, secret, runner or cloud identity.
Contain new runs, reconstruct each job’s trust boundary, rotate credentials according to actual capability and split fork CI from privileged automation. Restore writes only after a controlled fork proves two things at once: untrusted code still runs where CI needs it, and it cannot run where repository or Azure authority exists.