Your forked merge requests are running with your secrets
One pipeline decision controls whether a stranger's merge request can read your deploy keys. Here's how to check it in five minutes.
Open source projects take merge requests from strangers. That’s the point. The problem starts when the pipeline that tests those merge requests runs on the same runners, with the same variables, as the pipeline that deploys to production.
On GitLab, a merge request from a fork runs its pipeline in the fork by default, with the fork’s runners and the fork’s variables. That default is safe. It stops being safe the moment a maintainer clicks Run pipeline on the merge request in the parent project to get a green check, because that pipeline runs the contributor’s code with the parent project’s variables and runners. GitHub has the same trap under a different name: workflows triggered by pull_request_target run with the base repository’s secrets, and the damage starts when one of them checks out the pull request’s head commit and runs it.
The same problem exists inside a company without any public forks. Any developer who can push a branch can change .gitlab-ci.yml on that branch. If the branch pipeline can reach a deploy credential, so can every developer, and so can anyone who steals one of their sessions.
The five minute check
- List every CI/CD variable at the project, group and instance level.
- For each one, check whether it is protected. Protected variables only reach pipelines on protected branches and tags.
- Check who can run parent-project pipelines for fork merge requests, and whether anyone does it routinely.
- Check which runners pick up those jobs, and what else those runners can reach.
The API makes step one and two quick. With a token that has read access:
GL=https://gitlab.com/api/v4
curl -sS --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "$GL/projects/$PROJECT_ID/variables?per_page=100" \
| jq -r '.[] | [.key, "protected=\(.protected)", "masked=\(.masked)", "env=\(.environment_scope)"] | @tsv'
curl -sS --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "$GL/groups/$GROUP_ID/variables?per_page=100" \
| jq -r '.[] | [.key, "protected=\(.protected)", "masked=\(.masked)", "env=\(.environment_scope)"] | @tsv'
Run the group query for every parent group, including the top-level one. Instance-level variables need an administrator token. Anything with protected=false and a name like AWS_SECRET_ACCESS_KEY, DEPLOY_TOKEN or KUBECONFIG is the first fix.
If any deploy credential is unprotected and fork code ever runs in your project, a merge request can print it, encode it, or send it somewhere. Masking doesn’t help much. It hides the value in the job log, but it does nothing to stop a script from base64-encoding the value or posting it to a server.
Masking is a log filter. It is not access control.
Read the job before you run it
When a maintainer runs a fork pipeline in the parent project, they’re approving code execution with project credentials. Treat it that way. Read the diff to .gitlab-ci.yml, to any file it includes, to any script the pipeline calls, and to build files that execute code, such as a Makefile, package.json scripts or setup.py. A one-line change to a test helper is enough to read the environment.
Watch for changes that look harmless but move trust. A new include: that pulls a template from another project or a URL, a changed image: pointing at a registry you don’t control, or a before_script added to the default section all change what runs before your own jobs do.
Fence secret jobs off from fork code
Jobs that need credentials should refuse to run for merge requests that come from another project. The predefined variables make that a two-line rule:
deploy-staging:
stage: deploy
rules:
- if: $CI_MERGE_REQUEST_SOURCE_PROJECT_ID && $CI_MERGE_REQUEST_SOURCE_PROJECT_ID != $CI_PROJECT_ID
when: never
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
environment: staging
script:
- ./deploy.sh
This doesn’t replace protected variables. It’s a second wall in case someone unprotects a variable to debug something and forgets to change it back.
Separate the runners
Variables are half the problem. The runner is the other half. A shell executor on a long-lived host, or a Docker executor with privileged = true, lets one job leave files, processes or credentials behind for the next. If fork code and deploy jobs share that host, the fork job can wait for the deploy job.
I split runners by trust. Deploy runners are marked protected in GitLab, so they only take jobs from protected branches and tags, and they carry a tag that untrusted jobs never request. Runners for merge request code get nothing worth stealing:
[[runners]]
name = "untrusted-mr"
executor = "docker"
[runners.docker]
image = "alpine:3.20"
privileged = false
disable_cache = true
network_mode = "mr-isolated"
allowed_pull_policies = ["always"]
mr-isolated is a Docker network whose only route out goes to the package mirror. No cloud metadata endpoint, no internal services, no registry push. Give these runners their own cloud account or no cloud identity at all.
Caches deserve the same thinking. GitLab keeps separate caches for protected branches by default, which stops an unprotected branch from planting a poisoned dependency that a release job later restores. Check that nobody switched that setting off to save build time.
What I do instead
- Every deploy credential is protected and scoped to one environment.
- Where the platform supports it, pipelines get short-lived tokens through OIDC federation instead of stored keys, so there’s nothing long-lived to steal.
- The cloud side of that federation checks the branch. An AWS trust policy can require a
subclaim likeproject_path:my-group/my-project:ref_type:branch:ref:main, so a token from any other branch is refused. - Fork pipelines run on a dedicated runner pool with no secrets and no egress beyond the package mirror.
- Deploy jobs require an approval on a protected environment.
- A canary token sits in an unprotected variable. If it ever gets used, I know someone tried.
That last one costs nothing. Generate a decoy cloud key from a canary service, or build your own with a key that has no permissions and an alert on any API call that uses it. The Tripwire CI tool on the products page, which is still in development, packages that idea for GitLab and GitHub.
How teams get this wrong
- Unprotecting to debug. A variable gets unprotected so a feature branch can test a deploy, and it stays that way for a year.
- Protecting every branch. If developers can push to a wildcard protected branch pattern such as
release/*, protected variables reach anyone who can create a branch with that name. - Trusting the job token too far.
CI_JOB_TOKENcarries the permissions of the user who started the pipeline, limited by the project’s job token settings. Restrict which projects it can reach under the job token allowlist. - Forgetting old pipelines. Retried jobs run with the configuration of the original commit. A fix to
.gitlab-ci.ymldoesn’t change what a retry of last month’s pipeline does.
Where this lands in the standards
NIST SP 800-218 asks you to separate and protect each environment involved in software development (PO.5.1) and to store all forms of code with least-privilege access (PS.1.1). NIST SP 800-204D covers CI/CD pipeline security directly, including keeping secrets out of reach of untrusted builds. The DoD Enterprise DevSecOps Fundamentals treat the pipeline itself as part of the attack surface. NIST SP 800-53 AC-6 and NIST SP 800-171 r2 requirement 3.1.5 ask for least privilege, which a deploy key readable by every branch fails on its face. The OWASP Top 10 CI/CD Security Risks names this pattern Poisoned Pipeline Execution (CICD-SEC-4) and covers the credential side under CICD-SEC-6.
A pipeline that hands production credentials to unreviewed code is the shortest path through all of them.
Want someone to try it on your pipeline?
Runner isolation, variable scoping and fork handling are the first things I check in a pipeline security review. It’s fixed scope, one pipeline from first commit to deploy, with every finding mapped to NIST SP 800-218 and retested after you fix it. If you aren’t sure that’s the right first step, book a free 30-minute fit call.