Threat-model your pipeline
What a branch push, a fork and one button can reach.
cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-gitlab/threat.tar.gz && tar -xzf threat.tar.gz, which creates ~/lab/threat/. SHA-256: 6d83e8f97631e039019e04badab6bd52e8f8e7bff4c84eb4f501992d11fd327aMost attacks on CI/CD don't break into GitLab. They use it as designed: someone who may push a branch changes the pipeline file, and the pipeline runs their code with whatever it can reach. The industry name for this is poisoned pipeline execution, and the first step in defending against it is knowing exactly who can run code in your pipelines, and with which secrets.
Who can run code in a pipeline?
| Who | How their code runs | Default reach |
|---|---|---|
| Maintainer | Pushes to protected branches and tags | Everything, including protected variables |
| Developer | Pushes a branch with a changed .gitlab-ci.yml | Unprotected variables, runners that accept unprotected refs |
| Outside contributor | Opens a merge request from a fork | Their fork's own variables and runners |
| Parent-project member (Developer or above) reviewing a fork | Clicks "Run pipeline" for a fork merge request | The parent project's pipeline, running the fork's code, with a CI_JOB_TOKEN that acts with the reviewer's permissions |
| Owner of an included template | Pushes to the template project | Every pipeline that includes it from a moving ref |
This lab tries the Developer, fork and Maintainer-review paths on purpose. Three terms come first, each with its own lesson later. A *masked* variable is replaced by [MASKED] in job logs. A *protected* variable is given only to pipelines on protected branches and tags, which by default only Maintainers can push. And roles are numbered: 30 is Developer, who can push branches, and 40 is Maintainer, who can change settings and push to protected branches.
The project is public, has one masked variable (DEPLOY_TOKEN) and one masked and protected variable (PROD_TOKEN). Three helper scripts handle users: ./user.sh NAME creates a user with an API token, ./member.sh app NAME 30 adds them to the project as a Developer and waits until GitLab applies it, and ./as.sh NAME COMMAND runs a command with that user's token. dev is a Developer, and outsider has no role in the project at all.
A developer edits the pipeline on a branch
dev replaces the pipeline on a feature branch with one job that only reports what it can see:
# What someone who can change this file can make a job do. The variables are masked in the log,# but a job holds the real values and can transform them.workflow:rules:- if: $CI_PIPELINE_SOURCE == "merge_request_event"- if: $CI_COMMIT_BRANCHleak:image: secopslog-py:3.15script:- echo "DEPLOY_TOKEN=$DEPLOY_TOKEN"- echo "reversed=$(echo "$DEPLOY_TOKEN" | rev)"- echo "PROD_TOKEN is ${#PROD_TOKEN} characters"- echo "running in $CI_PROJECT_PATH for $CI_PIPELINE_SOURCE on ref $CI_COMMIT_REF_NAME (protected=$CI_COMMIT_REF_PROTECTED)"
DEPLOY_TOKEN was masked in the plain line, and the reversed line printed fedcba9876543210-yolped, the token backwards. dev now has a token they were never given. PROD_TOKEN was empty, because it is protected and feature/tweak is not. The last line confirms the setting: project root/app, a push pipeline on a ref with protected=false.
A merge request from a fork
outsider forks the project, pushes the same file to a branch of the fork, and opens a merge request into root/app:
The fork also ran a branch pipeline for the push, which is why the merge request pipeline is #4. It ran in outsider/app, the fork, as a merge_request_event. Both tokens were empty. The fork is a separate project with its own variables, and root/app's settings don't travel with the code. The job did run on the lab's instance runner, which takes jobs from every project, so the fork owner still ran code on your infrastructure.
One button that changes everything
A reviewer in the parent project wants its pipeline to run on the merge request, perhaps because the parent has the test database. Any member who may run pipelines, Developer or above, can do that with the "Run pipeline" button in the merge request, which shows a warning first, or through the API, which doesn't. The lab uses the API as root:
Same commit, same job, different project: the pipeline ran in project 1, root/app, and the reversed DEPLOY_TOKEN came out in full. outsider, who has no role in the project, wrote the code that printed it. PROD_TOKEN stayed empty, because GitLab never gives protected variables to a pipeline for a merge request from a fork. The unprotected DEPLOY_TOKEN is the one that leaked.
.gitlab-ci.yml, any file it includes, and any script a job calls. The pipeline will run that code with the parent's unprotected variables and on the parent's runners. If you aren't sure, don't press the button, and keep long-lived secrets in protected variables or, better, out of GitLab altogether. Two instance settings matter on self-managed GitLab: open sign-up lets any stranger create an account and fork public projects onto your instance runners, and public projects show job logs to everyone by default, so dev's reversed token was readable by anyone who could open the project.What to do about it
There's no single fix, because the problem is who may change code that runs. The next lessons each close one path. Protected branches and tags decide who may change the refs that get secrets. Protected and hidden variables keep secrets away from unprotected refs and from the settings page. Separate runners keep untrusted jobs away from trusted machines. And ID tokens replace stored secrets with credentials that expire.
Hands-on
Make DEPLOY_TOKEN protected with ./gl.sh /projects/root%2Fapp/variables/DEPLOY_TOKEN -X PUT -d protected=true, then push the leak job again as dev on a new branch: git -C app checkout feature/tweak, change any file (for example echo '# again' >> app/calc.py, since commit.sh needs something to commit), and run ./as.sh dev ./commit.sh app/ "try again" feature/again and ./pipeline.sh app/. What does it print now? Finish with ./down.sh.
What you should see: the API confirms "masked":true,"protected":true, and the job on feature/again prints DEPLOY_TOKEN= and reversed= with nothing after them. No masking was needed, because the value was never sent to the job. Protection works by not handing the secret over at all, which no change to .gitlab-ci.yml on a branch can undo.
Remember
Anyone who can push a branch can run code in your pipeline. Assume every unprotected variable is readable by every Developer, and treat running a fork's pipeline in the parent project as running a stranger's code with your secrets.
main, but can push branches. A masked, unprotected variable holds a cloud key. Can the Developer get the key?outsider/app and saw none of the parent's variables.