CI/CD variables, masking and hidden values
Variable types, what masking hides, and what it misses.
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: 5b2329b6645cee58e0e23ea6a18103ade1c832a3d09b8a80ca97bad45127a2b1A 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
| Setting | What it does | What it doesn't do |
|---|---|---|
| Masked | Replaces the exact value with [MASKED] in job logs | Hide it from Maintainers, or from the job itself |
| Masked and hidden | Masked, and the value can no longer be read in the UI or API after saving | Hide it from the job, which still receives it |
| Protected | Only pipelines on protected refs receive it | Mask it (combine both) |
| File type | The job gets a path to a temporary file holding the value | Mask 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
# 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 itshow:image: secopslog-py:3.15script:- 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)"
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.
.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.
echo "$TOKEN" | rev, where TOKEN is masked. What does the log show?.gitlab-ci.yml sets LOG_LEVEL: info, and the project settings define LOG_LEVEL=debug. What does a job see?GREETING.