Sub topic 13
Permissions, fork pull requests, and cache poisoning
This chapter assumes you can read a GitHub Actions workflow and have followed the earlier chapters on cache-from, cache-to, and the gha, registry, and S3 backends. A remote cache is shared state that later builds trust, so who can write to it matters.
Why a cache is a trust boundary
BuildKit reuses a cached layer when its cache key matches. It does not check who produced the layer. If an untrusted build writes a layer into the shared cache, a later trusted build can import it, mark the step CACHED, and ship its contents. This is cache poisoning.
Fork pull requests
For a pull request from a fork, GitHub gives the workflow a read-only GITHUB_TOKEN and no repository secrets. The OIDC token (id-token: write) is also unavailable. The effects on each backend:
| Backend | Fork PR behavior |
|---|---|
| gha | Can restore caches, cannot save to the base repository's scope |
| registry | Push fails, because there are no registry credentials |
| s3 | Role assumption fails, because there is no OIDC token |
Do not work around this by switching to pull_request_target and checking out the fork's code. That event runs with secrets and write access, so the fork's Dockerfile would run with credentials that can write to your cache.
Write only from trusted events
Make cache-to conditional. Pull requests then import cache but never export it.
name: build
on:
push:
branches: [main]
pull_request:
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v6
with:
context: .
push: false
tags: myapp:ci
cache-from: type=gha,scope=myapp
cache-to: ${{ github.event_name == 'push' && 'type=gha,scope=myapp,mode=max' || '' }}An empty cache-to value disables export. The same condition works for the registry and S3 backends.
Least privilege for S3 and registries
Use the policy from the S3 chapter, scoped to one bucket. Then restrict who can assume the role. Without a condition on the OIDC sub claim, any workflow in the repository, including one on a feature branch, can write to the cache. This trust policy limits the role to pushes to main:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::123456789012:oidc-provider/token.actions.githubusercontent.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:my-org/myapp:ref:refs/heads/main"
}
}
}
]
}Apply the same idea elsewhere:
- Give pull request jobs a separate role with only
s3:GetObjectands3:ListBucket. - For a registry cache, give only the main-branch job push rights to the
buildcachetag. - Keep the build job's
permissionsminimal. Addid-token: writeonly to jobs that assume a role.
Limiting the damage
- Use separate cache scopes or names for untrusted and release builds. Do not let a feature branch write to the scope that release builds read.
- Rebuild release images without cache, or from a cache written only by
main, when you need strong provenance. - Never put secrets in build arguments or layers. Cache layers can be pulled by anyone who can read the cache.
- Treat the cache bucket and tag as sensitive as the image registry.
Exercise
A workflow runs on pull_request, assumes an AWS role with OIDC, and writes the S3 cache. A contributor opens a PR from a fork. What happens, and what two changes make the setup safe for both fork and same-repository PRs?
Show answer
The fork PR has no OIDC token, so role assumption fails and the job fails or builds without cache. Change 1: make cache-to conditional on a push to main, so PRs only import cache. Change 2: restrict the role's trust policy to the main branch with a sub condition, and give PR jobs a separate read-only role if they need to read the S3 cache.