Designing Pipelines to Prevent Leakage
This lesson explains each route by which secrets leak, and the control that closes it.
Learning outcomes
After completing this lesson, you will be able to:
- Explain the limits of log masking.
- Prevent leaks through artifacts and images.
- Identify unsafe uses of pull_request_target.
- Prevent script injection.
Leaks through logs
Both platforms mask registered secret values, replacing them with asterisks in logs. Masking is a useful safety net and not a guarantee, as Microsoft and GitHub both state, because it works by matching known text. The GitHub Actions runner also masks some common encodings of a registered value, such as Base64, but GitHub still advises registering any transformed value as a secret in its own right, since automatic redaction cannot cover every transformation.
- A transformation that the platform does not recognize, such as hexadecimal encoding or encryption, changes the text, so the result is not masked. Do not rely on an encoded form being caught on either platform.
- Printing part of a secret, or splitting it across lines, avoids the match.
- Structured values, such as a JSON document with a secret inside, are not masked unless each sensitive value is registered separately.
- Commands that echo their arguments may print values that a pipeline did not register.
Avoid passing secrets as command-line arguments, as they may appear in process lists or in echoed output. Prefer environment variables or files. Avoid running with debug logging, such as system.debug: true or ACTIONS_STEP_DEBUG, since verbose output can reveal more. A derived value that is secret should be registered, using ::add-mask:: in GitHub Actions, or ##vso[task.setvariable variable=x;issecret=true] in Azure Pipelines.
The following diagram shows how an encoded form of a registered secret escaped log masking, and the fixes.

Will masking catch it?
Leaks through artifacts and repositories
An artifact may contain a configuration file with a live connection string, or a container image may keep a secret in one of its layers, even if a later layer deletes it. Remedies are to build artifacts without secrets, and to inject configuration at deployment. For containers, use multi-stage builds, and BuildKit secrets, which provide a value during a build, without saving it in a layer.
Secret scanning and push protection guard the repository itself, as the next module explains. Test data should be synthetic, and .gitignore should exclude local configuration files.
Secrets in artifacts and images
Leaks through untrusted code
The most serious risk is untrusted code running in a job that holds secrets. If code from a pull request runs with access to secrets, its author can steal them.
Pull requests from forks
Azure Pipelines does not give secrets to fork builds by default, and it has an option requiring a comment from a team member before building. In GitHub Actions, the pull_request event gives a fork's workflow no secrets and a read-only token.
pull_request_target
The pull_request_target event runs in the context of the base repository, with secrets and a write-capable token, so that a workflow can label a pull request from a fork. It uses the workflow file from the base branch, which is safe, unless the workflow then checks out and runs code from the pull request. That combination is a well-known attack, sometimes called a pwn request.
# Unsafe: runs untrusted code with secrets available
on: pull_request_target
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- run: npm install && npm test
env:
TOKEN: ${{ secrets.DEPLOY_TOKEN }}Script injection
A workflow expression is expanded into a script before it runs. An attacker can set a pull request title to a shell command, and if a step places ${{ github.event.pull_request.title }} directly in a run block, the shell executes it. The safe form passes the value through an environment variable.
# Unsafe
- run: echo "Title is ${{ github.event.pull_request.title }}"
# Safe
- run: echo "Title is $TITLE"
env:
TITLE: ${{ github.event.pull_request.title }}Third-party actions and tasks
An action runs with access to the job's context. A tag such as @v3 can be moved by its owner, or by an attacker who takes over the repository. Pinning to a full commit SHA ensures that the code cannot change without you. The risk is real: in March 2026, attackers force-pushed most version tags of the widely used aquasecurity/trivy-action, so workflows that referenced those tags ran credential-stealing code (CVE-2026-33634). GitHub organizations can restrict which actions may run, and Dependabot can keep pinned SHAs up to date.
# Pin third-party actions to a full commit SHA
- uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0A pwn request
Design controls
The following table summarizes the controls for each route.
| Route | Control |
|---|---|
| Logs | Environment variables, masking, and no debug logging in production |
| Artifacts and images | Inject configuration at deployment; use BuildKit secrets |
| Pull requests from forks | No secrets; review before running |
| pull_request_target | Never check out and run the pull request's code |
| Script injection | Pass untrusted values through env |
| Third-party code | Pin to a SHA, and allow only approved actions |
| Over-privileged jobs | Minimal permissions, and separate jobs for secrets |
| Shared self-hosted runners | Ephemeral runners and separate groups |
Finally, separating duties inside a pipeline reduces exposure: a build job that runs tests needs no deployment secrets, so those secrets should appear only in a deployment job, behind an environment.
Leak routes and their controls
A masked value that leaked
A script converts an API key to hexadecimal for an HTTP header and prints the header while debugging. The full hexadecimal value appears in the logs, although the key is a registered secret. What explains this, and what is the fix?
Masking looks for the registered text and a limited set of common encodings, so the hexadecimal form was not recognized. The fix is to stop printing the header, and to register the encoded value as a secret, with ::add-mask:: or a logging command, before it is used. Any exposure means that the key must also be rotated. Changing the log retention period only shortens how long the exposure lasts.
Why is a tag such as @v3 weaker than a full SHA for a third-party action?
Show answer
A tag can be moved to different code, whereas a full commit SHA identifies one exact version.
Exam Tips
Remember for the exam
- Masking is not guaranteed, so register transformed values and never print secrets.
- Prefer environment variables to command-line arguments.
- Never check out pull request code in a pull_request_target workflow.
- Pass untrusted expressions through env to prevent script injection.
- Pin third-party actions to a full commit SHA.