A secure CI/CD pipeline is not a pipeline that runs scanners: it is a
pipeline that stops when a scanner finds what the team has decided to
block. This lesson assembles the four pillars of this defence, security
gates, secrets, isolation and signing, for a DevOps engineer who
already has a working pipeline. It rests on measurements: the exit codes of
the scanners seen in the previous module, which show that half of them block
nothing by default, a gates workflow run through three workflow scanners,
and an image replaced under the same tag that cosign verify refuses.
What you will learn
- Explain why the pipeline concentrates what an attacker is looking for
- Place the security gates at the transitions of the pipeline, and decide their thresholds
- Make each scanner block, based on its measured exit code
- Write a gates workflow validated by actionlint, zizmor and poutine
- Limit what a compromised job can reach: secrets, permissions, runners
- Refuse at deployment an image that nobody signed
Why is the pipeline a target?
A pipeline turns source code into a deployed artifact, and to do so it brings together three things that an attacker otherwise looks for separately: secrets (deployment tokens, cloud credentials), privileges (publishing a package, pushing an image, deploying) and automatic execution on every commit. Compromising it means getting your own code signed and shipped by the victim's chain.
The tj-actions/changed-files attack, in March 2025, showed it bluntly: by moving the version tags of an action, the attacker had the secrets of more than 23,000 repositories printed in their logs. No test failed, no application scanner went off: the threat did not come from the code, but from what the pipeline runs and what it exposes. The previous lesson demonstrated it on a moved tag; this one builds the defence.
| Pillar | What it prevents | Dedicated lesson |
|---|---|---|
| Security gates | Rejecting a release under explicit and verified conditions | this lesson |
| Secrets | Limiting which jobs can access credentials | Secrets in pipelines |
| Isolation | Reducing what a compromised job can reach | Minimal permissions |
| Signing | Detecting substituted artifacts when verification is enforced | Signing and verifying with Cosign |
These controls address different risks. Vulnerability scanning does not protect credentials available to a job. A signed image may still contain vulnerabilities: signing can help verify origin under an enforced policy, not guarantee safety.
Where do you place the security gates?
A security gate evaluates a condition and can stop a release. To be effective, its result must be required and interpreted correctly, and no alternative publication path may bypass it. A later control does not automatically make up for a misconfigured earlier one.
| Gate | Transition | What it checks | What it blocks |
|---|---|---|---|
| Pre-commit | Before the commit | Secrets | The commit, on the workstation |
| Pull request | Before the merge | Secrets, SAST, SCA | Merging, if the check is required by branch protection |
| Build | Before publication | Image scan, signing | Publication, if it depends on the check result |
| Deployment | Before production | Provenance verification, staging tests | Deployment, if admission or promotion enforces the rule |
Each gate has its reason to exist. Pre-commit gives fast feedback but can be bypassed with --no-verify. A pull request provides a shared control only when branch protection requires it. Builds let you inspect the resulting artifact. At deployment, check its digest and provenance to detect substitutions.
Deciding the thresholds
Blocking on every alert, false positives and minor severities included, makes the teams lose trust, and they end up bypassing or disabling the gate. Blocking nothing makes the gate decorative. The threshold is therefore defined for each finding type: a detected secret needs prompt verification and response according to its type and exposure; CVE prioritization also depends on exploitability and component exposure.
| Finding | Pull request | Deployment |
|---|---|---|
| Secret detected | Blocks | Blocks |
| SAST with ERROR severity | Blocks | Blocks |
| CVE listed in the KEV or CRITICAL | Blocks | Blocks |
| HIGH CVE | Warns in the PR | Blocks, unless a dated exception |
| Injection confirmed by DAST | not applicable | Blocks |
| Unsigned image | not applicable | Blocks |
Start strict on this reduced table, and widen it as the team matures. An exception is documented with an expiry date, otherwise it becomes invisible debt.
A gate is an exit code
The runner does not read a scanner's report: it reads its exit code. A
scanner that finds flaws and exits with 0 lets the job pass, whatever
its report contains. Yet, measured on the tools of the previous module, half
of them exit with 0 by default even when they find something.
| Tool (measured version) | By default, with findings | To block |
|---|---|---|
| Gitleaks 8.30.1 | 1 | nothing to add |
| Semgrep 1.178.0 | 0 | --error, with --severity ERROR for the threshold |
| Trivy 0.74.0 | 0 | --exit-code 1 and --severity |
| Grype 0.119.0 | 0 | --fail-on <severity>, which exits with 2 |
| ZAP 2.17.0 | 2 on a mere WARN | rules file and -I |
| Nuclei 3.11.1 | 0, always | test the file written by -o |
| cosign verify 3.1.1 | 10 without a valid signature | nothing to add |
The threshold is part of the gate. Measured with Trivy on a
requirements.txt that only contains requests==2.25.1: 4 MEDIUM
vulnerabilities. Without options, the code is 0. With
--severity HIGH,CRITICAL --exit-code 1, it is still 0, which is intended:
nothing exceeds the threshold. With --severity MEDIUM --exit-code 1, it
moves to 1 and the job fails. Always test a gate both ways: on a
repository that must pass, and on a repository that must be refused.
A complete gates workflow
The workflow below assembles the pull request gates, then signs the image
on a push to main. Each scanner runs in its official image pinned by
digest, with the option that makes it block, and the actions are pinned
by commit SHA. The build job only starts if the three gates have
succeeded. The step names keep the French of the measured file.
name: Security gates
on: pull_request: branches: [main] push: branches: [main]
permissions: {}
jobs: secrets: runs-on: ubuntu-24.04 permissions: contents: read container: image: ghcr.io/gitleaks/gitleaks:v8.30.1@sha256:c00b6bd0aeb3071cbcb79009cb16a60dd9e0a7c60e2be9ab65d25e6bc8abbb7f steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 persist-credentials: false - name: Secrets dans l'historique run: gitleaks git --redact --exit-code 1 .
sast: runs-on: ubuntu-24.04 permissions: contents: read container: image: semgrep/semgrep:1.178.0@sha256:32e459968daabe7ab86968184a29109b9564aa00392401156f9788452b42786b steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - name: Analyse statique, bloquante sur ERROR run: semgrep scan --config p/security-audit --severity ERROR --error --metrics=off .
sca: runs-on: ubuntu-24.04 permissions: contents: read container: image: aquasec/trivy:0.74.0@sha256:62b1e65e8869bc4b4c6aa4fa2b21595256c7c2f6018a9d9ad61caf87187c1969 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - name: Dépendances, bloquant sur CRITICAL run: trivy fs --scanners vuln --severity CRITICAL --exit-code 1 .
build-sign: if: github.event_name == 'push' needs: [secrets, sast, sca] runs-on: ubuntu-24.04 permissions: contents: read packages: write id-token: write outputs: digest: ${{ steps.push.outputs.digest }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - uses: sigstore/cosign-installer@6f9f17788090df1f26f669e9d70d6ae9567deba6 # v4.1.2 - name: Build et push id: push env: IMAGE: ghcr.io/${{ github.repository }} GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} ACTOR: ${{ github.actor }} run: | echo "$GH_TOKEN" | docker login ghcr.io -u "$ACTOR" --password-stdin docker build -t "$IMAGE:$GITHUB_SHA" . docker push "$IMAGE:$GITHUB_SHA" digest=$(docker inspect --format '{{index .RepoDigests 0}}' "$IMAGE:$GITHUB_SHA" | cut -d@ -f2) echo "digest=$digest" >> "$GITHUB_OUTPUT" - name: Signature keyless, par digest env: IMAGE: ghcr.io/${{ github.repository }} DIGEST: ${{ steps.push.outputs.digest }} run: cosign sign --yes "$IMAGE@$DIGEST"Three choices deserve a close reading. permissions: {} at workflow level
removes all the default rights, and each job only gets back its own: only
build-sign can write a package and obtain an OIDC token
(id-token: write), which cosign needs for keyless signing. Values coming
from the GitHub context go through env:, never directly into run:, which
shuts the door on template injection. Finally, the signature targets the
image's digest, not its tag, for the reason measured further down.
Measured on this file: actionlint 1.7.12 exits with 0, zizmor 1.30.0
answers No findings to report (its 6 pedantic remarks, missing names and
missing comments on permissions, are hidden by default), and poutine 1.1.6
passes all its rules. This is not a security guarantee, it is the assurance
that the workflow reproduces none of the known defects of these tools. Run
them on your own workflows, starting with
zizmor.
Protecting the pipeline's secrets
A delivery check does not protect credentials available to a job. Three precautions help:
- Inject credentials only into steps that need them rather than the entire workflow. Scope depends on where
env:is configured. Log redaction does not guarantee there was no leak. - Separate identities across environments and configure required approvals where appropriate. Naming an environment
productiondoes not activate protection automatically. - Prefer OIDC and temporary cloud credentials to long-lived keys. A compromised job can still abuse them while they remain valid.
The Shai-Hulud worm, in 2025, illustrated what is at stake: its install script looked for GitHub, npm and cloud tokens in the environment of the machines where it ran, then republished itself with the stolen accounts. A job that holds no unnecessary secret offered it nothing.
Limiting what a compromised job can reach
You start from the assumption that a job will be compromised one day, through an action, a dependency or a trapped pull request. Isolation then decides the extent of the damage, the "blast radius". It plays on four axes, which do not replace one another.
| Axis | Measure | Effect |
|---|---|---|
| Permissions | permissions: {} then rights per job | A stolen token only allows what the job did |
| Runner | Fresh environment destroyed after every job | Reduces persistence between jobs, not malicious activity while a job runs |
| Network | Runner with no route to the internal network | The job does not reach production |
| Environment | Secrets and accounts separated per environment | Reduces the risk of test credentials accessing production |
Standard GitHub-hosted runners provide a disposable environment for each job. A persistent self-hosted runner can retain files or processes across jobs. Keep untrusted contributions separate from sensitive operations, and provision genuinely fresh environments between jobs. The risk also applies to private repositories when contributors can run code.
Refusing a substituted image at deployment
Signing answers a question that gates do not ask: is the image going to production the one the pipeline built? An attacker who obtains write access to the registry can push another image under the same tag. The measurement can be replayed with a local registry and a cosign key pair; in CI, keyless signing replaces the key pair with the workflow's OIDC identity, detailed in the Cosign lesson.
IMG=localhost:5055/api-facturation:1.0.0cosign verify --key cosign.pub --insecure-ignore-tlog "$IMG"Measured with cosign 3.1.1, in three steps:
| Situation | cosign verify code | Message |
|---|---|---|
| Image signed by the pipeline | 0 | The signatures were verified against the specified public key |
| Another image pushed under the same tag | 10 | Error: no signatures found |
| Verification by the original digest, after the substitution | 0 | the original image remains verifiable |
The deployment gate therefore fits in a single command, whose code 10
stops the pipeline. The third row says how to deploy: by digest, not by
tag. The tag changed content before your eyes; the digest designates one
image and one only. The --insecure-ignore-tlog option is only there
because the bench signs without a public transparency log; in CI, keyless
signing is recorded in Rekor and verification consults it.
The diagram sums up the substitution: the tag 1.0.0 used to
designate the signed image, it now designates the trapped image, and only
verification by digest finds the original.
Signature and provenance attestation
A signature proves that the image comes from the pipeline and has not been modified. It says nothing about how it was built: a Dockerfile trapped before the build produces a trapped image, correctly signed. The provenance attestation adds this information, repository, commit, workflow and runner, signed along with the artifact. The SLSA framework defines its levels, from L0 to L3 on the Build track; aiming for L2 is achievable with the native tools of GitHub Actions. The module on the supply chain devotes the lesson from SLSA provenance to the decision to it.
Secure pipeline checklist
The checklist reads in tiers: the first can be set up in a few days on an existing pipeline, the last requires heavier tooling and organisation. Each box matches a control seen in this lesson or in the lessons of the module.
Foundation, right away:
- Each scanner blocks on a written threshold, checked through its exit code
- The gates are declared required in the protection of
main -
permissions: {}at workflow level, minimal rights per job - Actions and images pinned by SHA or digest
Next:
- Secrets exposed only to the step that uses them, OIDC instead of cloud keys
- Workflows run through zizmor, actionlint and poutine in CI
- Signed images, and signature verified at deployment, by digest
Advanced maturity:
- SLSA L2 or higher provenance attestations
- SBOM published with each artifact
- Ephemeral self-hosted runners isolated from the internal network
A green pipeline that still allows unsafe merges
Atlas runs the security job on every pull request. The job succeeds and scanner reports are available. But the branch rule for main requires only unit-tests. A failing security job might therefore leave merging possible.
Inspect the repository rules. A status check must be required by an applicable rule if its failure is to block merging. GitHub also allows an expected application to be specified as the check source, as documented in its ruleset guidance.
The team finds another issue. The job that runs code submitted in a pull request can obtain an identity used to publish images. Pinning third-party actions to commit SHAs does not make contributed code trustworthy. Verification and publishing need separate permissions and execution environments.
Trace a contribution from the external author to the registry. Where does the code run? Which identity can the job request? At which point is a merge or publication actually refused?
Design two safe checks. A healthy pull request should be allowed. A deliberately failing security check should block merging. Also consider a scanner that fails to start: a broken tool and a detected vulnerability may both stop delivery, but they call for different responses.
Key points
- The pipeline concentrates secrets, privileges and automatic execution: that is what makes it a target.
- A gate blocks only when it is actually enforced at the relevant transition.
- A scanner finding does not automatically block delivery: inspect the exit code and configured behavior of the tool version actually used.
- A gate is tested both ways: a repository that must pass, a repository that must be refused.
- The measured gates workflow passes actionlint, zizmor and poutine without findings, thanks to
permissions: {}, the SHA andenv:. cosign verifyexits with 10 on an image substituted under the same tag: deploy by digest.- A gate that is not required in branch protection prevents no merge.
Knowledge check
Check that you have the essentials of this lesson. The questions only cover what has just been explained here.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Result saved on this device. It will be sent to your account at the next sync. Sync now
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Hardening the build environment: securing runners and pipelines
A new full quiz with random questions
Work again only on the questions you missed
FAQ: frequently asked questions
The questions below cover the most common doubts about building a secure pipeline: where to start, what to block, and what sets a signature apart from an attestation.