Sub topic 5
Log Masking and Why It Is Not a Security Boundary
This chapter assumes you can read a GitHub Actions workflow, know what an environment variable is, and have stored a repository secret. You will learn what log masking does, where it fails, and what to do about those failures.
What masking does
Most CI platforms keep a list of secret values for each job. Before a log line is shown, the runner searches it for those values and replaces each match with ***. GitHub Actions does this for every value stored in the secrets context.
Masking is a string match on the output, applied at display time. It does not stop a process from reading the secret, copying it, or sending it elsewhere.
How masking fails
The runner only knows the exact strings it registered. Any change to the value makes it invisible to the masker.
| Transformation | Why it escapes masking |
|---|---|
| Base64 or hex encoding | The output no longer contains the original string. |
| Splitting or reversing | A match needs the whole value in one piece. |
| Adding characters between letters | The exact sequence is broken. |
| Multi-line secrets | Masking works per line, so a key spread across lines can show unmasked parts. |
| Short or common values | Values such as `true` or `1234` are poor mask targets and may be skipped or may cause false matches. |
| Structured secrets | A JSON blob stored as one secret may have its inner fields printed on their own. |
A further problem is that output can leave the job without passing through the log. A step can write the secret to an artifact, a cache, a file, or a network request. None of those paths are masked.
Registering derived values
If a step computes a new value from a secret, the platform does not know it is sensitive. You must register it with the add-mask workflow command before it can be printed.
steps:
- name: Mask a derived value
env:
RAW_TOKEN: ${{ secrets.RAW_TOKEN }}
run: |
DERIVED=$(printf '%s' "$RAW_TOKEN" | base64)
echo "::add-mask::$DERIVED"
echo "DERIVED=$DERIVED" >> "$GITHUB_ENV"The order matters. The ::add-mask:: line must run before any line that prints DERIVED. The secret reaches the step through env, scoped to this one step, rather than being written into the script text.
Note that add-mask protects only the log. The value still sits in $GITHUB_ENV, so every later step in the job can read it.
Common errors
- Treating masked logs as proof of safety. Seeing
***tells you that one exact string matched. It says nothing about artifacts or network calls. - Debug logging. Settings such as
set -xprint expanded commands. Turning on runner debug logging can also expose more detail. Check both for any job that handles secrets. - Trusting untrusted code. A pull request that changes a script can encode the secret and print it. Masking does not stop that. Keep secrets away from such code, as covered in the chapter on fork pull requests.
- Masking instead of limiting. A masked token with broad permissions is still a broad token.
What to rely on instead
Use masking as a safety net, and put your real controls elsewhere:
- Scope each secret to the job and environment that needs it.
- Prefer short-lived credentials from OIDC so a leaked value expires quickly.
- Review code that runs in jobs with secrets.
- Scan logs and artifacts for leaks, and rotate any value you suspect was exposed.
Exercise
A step runs echo "$TOKEN" | rev and the log shows the reversed token in plain text. Why was it not masked, and what is the real lesson?
Show answer
The runner masks only the exact registered string. Reversing the token produces a different string that does not match, so it is printed unmasked. The lesson is that masking is a display filter on known strings, not a security boundary. Any code that can read the secret can expose it, so limit which code and jobs can access it.
Exercise
You derive a value from a secret with base64 and want to use it in later steps without it appearing in logs. Which command do you run, and when?
Show answer
Run echo "::add-mask::$DERIVED" right after computing the value and before anything prints it. Then, if later steps need it, write it to $GITHUB_ENV. Later steps can still read the value, so limit what runs in the job.