CI/CD variables, masking and hidden values

Variable types, what masking hides, and what it misses.

Advanced13 min · lesson 9 of 17
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (16 files, 6 KB): secrets.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/secrets.tar.gz && tar -xzf secrets.tar.gz, which creates ~/lab/secrets/. SHA-256: 5b2329b6645cee58e0e23ea6a18103ade1c832a3d09b8a80ca97bad45127a2b1

A team marks its deploy token "masked" and considers the job done: the log shows [MASKED], so the secret is safe. Then a contractor with Maintainer access opens the CI/CD settings and copies the value straight out of the variable list. Masking was never meant to stop that, and it doesn't stop a job from leaking the value either. This lesson shows what each variable setting does, and the one GitLab added to close the settings-page gap.

Four kinds of variable

ubuntu@secopslog-cicd:~/lab/secrets · 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/ >/dev/null && QUIET=1 ./pipeline.sh app/ >/dev/null; ./gl.sh /projects/root%2Fapp/variables -X POST -d key=API_TOKEN -d value=lab-token-0123456789 -d masked=true -o /dev/null && ./gl.sh /projects/root%2Fapp/variables -X POST -d key=HIDDEN_TOKEN -d value=hidden-token-abcdefgh -d masked_and_hidden=true -o /dev/null && ./gl.sh /projects/root%2Fapp/variables -X POST -d key=SSH_KEY -d variable_type=file --data-urlencode $'value=-----BEGIN LAB KEY-----\nnot-a-real-key\n-----END LAB KEY-----\n' -o /dev/null && ./gl.sh /projects/root%2Fapp/variables -X POST -d key=GREETING -d 'value=hello from the project settings' -o /dev/null && echo 4 variables created
4 variables created
$ ./gl.sh /projects/root%2Fapp/variables | jq -r '.[] | "\(.key): type=\(.variable_type) masked=\(.masked) hidden=\(.hidden) protected=\(.protected) value=\(.value // "(not returned)" | .[0:24])"'
API_TOKEN: type=env_var masked=true hidden=false protected=false value=lab-token-0123456789 HIDDEN_TOKEN: type=env_var masked=true hidden=true protected=false value=(not returned) SSH_KEY: type=file masked=false hidden=false protected=false value=-----BEGIN LAB KEY----- GREETING: type=env_var masked=false hidden=false protected=false value=hello from the project s
SettingWhat it doesWhat it doesn't do
MaskedReplaces the exact value with [MASKED] in job logsHide it from Maintainers, or from the job itself
Masked and hiddenMasked, and the value can no longer be read in the UI or API after savingHide it from the job, which still receives it
ProtectedOnly pipelines on protected refs receive itMask it (combine both)
File typeThe job gets a path to a temporary file holding the valueMask it: multi-line values can't be masked

The API call above was made as root, who owns the project, and it returned API_TOKEN's full value. Any Maintainer gets the same answer. HIDDEN_TOKEN came back without one. Use "masked and hidden" for anything nobody should read back. The catch is that you can't look at it either, so keep the source of truth in a secret manager. Hidden closes the settings page and the API, not the pipeline: a Maintainer can still push a job that prints the value in another form, as the next section shows. Against people with that much access, keep the secret out of GitLab, which the ID token lesson does. A value can only be masked if it is a single line of at least 8 characters with no spaces, which is one more reason to generate tokens rather than type them.

What a job sees

app/.gitlab-ci.yml
# What a job sees of each kind of variable, and what the log shows of it.
variables:
GREETING: "hello from .gitlab-ci.yml" # not a secret: anyone who can read the repo sees it
show:
image: secopslog-py:3.15
script:
- echo "GREETING=$GREETING"
- echo "API_TOKEN=$API_TOKEN (length ${#API_TOKEN})"
- echo "HIDDEN_TOKEN=$HIDDEN_TOKEN (length ${#HIDDEN_TOKEN})"
- echo "SSH_KEY is a file at $SSH_KEY, first line $(head -1 "$SSH_KEY")"
- echo "base64=$(printf %s "$API_TOKEN" | base64)"
- echo "reversed=$(printf %s "$API_TOKEN" | rev)"
ubuntu@secopslog-cicd:~/lab/secrets · GitLab 19.5.3
$ ./run-pipeline.sh app && sleep 3 && ./pipeline.sh app/
pipeline #2 created source=api --- job show (test): success $ echo "GREETING=$GREETING" GREETING=hello from the project settings $ echo "API_TOKEN=$API_TOKEN (length ${#API_TOKEN})" API_TOKEN=[MASKED] (length 20) $ echo "HIDDEN_TOKEN=$HIDDEN_TOKEN (length ${#HIDDEN_TOKEN})" HIDDEN_TOKEN=[MASKED] (length 21) $ echo "SSH_KEY is a file at $SSH_KEY, first line $(head -1 "$SSH_KEY")" SSH_KEY is a file at /builds/root/app.tmp/SSH_KEY, first line -----BEGIN LAB KEY----- $ echo "base64=$(printf %s "$API_TOKEN" | base64)" base64=bGFiLXRva2VuLTAxMjM0NTY3ODk= $ echo "reversed=$(printf %s "$API_TOKEN" | rev)" reversed=9876543210-nekot-bal pipeline #2: success

GREETING printed in full, since it isn't secret. API_TOKEN and HIDDEN_TOKEN appear as [MASKED], and the job still knows their lengths. SSH_KEY is a file variable, so the job got a path, /builds/root/app.tmp/SSH_KEY, with the key inside. Then the last two lines: the same API_TOKEN in base64 and reversed, both printed in full. bGFiLXRva2VuLTAxMjM0NTY3ODk= decodes to the token, and 9876543210-nekot-bal is the token read backwards.

Watch out
Masking is a search-and-replace on the log, done by the runner on the exact value. Every transformation a job applies, such as reversing, encoding, splitting or spacing out the value, produces a different string that the filter doesn't know. Anyone who can change .gitlab-ci.yml on a branch that receives the variable can make a job print it in a form that isn't masked, or send it somewhere. Protected variables limit which branches receive it. Masking only prevents accidents.

Precedence

GREETING was set twice: in .gitlab-ci.yml and in the project's settings. The job printed the project setting. Variables from settings (instance, group, project) take precedence over variables in the file, and variables passed to a pipeline when it is started manually or through the API take precedence over both. That's convenient for overriding a default, and it also means a pipeline started with a variable can change how a job behaves. Who may do that is the project setting "Minimum role to use pipeline variables" under Settings > CI/CD > Variables. It is Maintainer by default on self-managed GitLab, and it can be lowered to Developer or set so that nobody may.

Hands-on

Start a fresh project with ./seed.sh app app/, recreate API_TOKEN as a masked variable, and copy solutions/.gitlab-ci.yml, which adds two more ways of printing the token: with a space after each character, and in two halves. Push and read the log. Which lines are masked? Finish with ./down.sh.

What you should see: the plain line is masked again, API_TOKEN=[MASKED] (length 20), and neither new line is. spaced=l a b - t o k e n - 0 1 2 3 4 5 6 7 8 9 and cut in two=lab-token- 0123456789 both give away the whole token. The masker looks for the exact string, so any change to it, however small, gets through.

Remember

Masking covers the job log only, not artifacts, caches or anything a job sends elsewhere, and never turn on CI_DEBUG_TRACE in a job that holds secrets, since it prints every variable. Masked means "not in the log by accident", hidden means "not readable after saving", and protected means "only protected refs receive it". A secret worth protecting needs protected and masked-and-hidden, and better still a short-lived credential from a secret manager, which the next lesson sets up.

Quick check
01A variable is masked but not hidden. Who can read its value?
Incorrect — Masking only changes what appears in job logs.
Incorrect — Project Maintainers can read it too.
Correct — Maintainers can read it in the settings and the API. The lab's API call returned the full value of the masked variable.
Incorrect — That would be the protected setting, which is separate.
02A job runs echo "$TOKEN" | rev, where TOKEN is masked. What does the log show?
Correct — Masking matches the exact value, and a reversed string is a different string.
Incorrect — The runner only replaces the exact value and does not detect transformations.
Incorrect — Jobs receive the real value and can do anything with it.
Incorrect — Masking does not restrict how a job uses a value.
03.gitlab-ci.yml sets LOG_LEVEL: info, and the project settings define LOG_LEVEL=debug. What does a job see?
Incorrect — Settings variables take precedence over file variables.
Incorrect — A variable has one value.
Incorrect — Duplicates are allowed and resolved by precedence.
Correct — The lab printed the project setting for GREETING.

Related