TExcelTAD
Home › CI/CD Pipelines & Pipeline Security › Shift Left: Security Stages in a Pipeline

Shift Left: Security Stages in a Pipeline

"Shift left" means moving security checks earlier, towards the developer: a hard-coded password found by a pre-commit hook costs seconds; the same password found in a leaked backup costs an incident. A pipeline is the natural place for those checks, because it already runs on every change and already has a pass/fail verdict.

What to check, and where

Check Finds Tools in this course Runs on
Secret detection API keys, tokens, passwords in code and history gitleaks, TruffleHog; GitGuardian (SaaS) pre-commit, every push, scheduled full-history scans
SAST (static application security testing) dangerous code patterns: injection, unsafe deserialisation, weak crypto Semgrep; SonarQube every PR (diff), main (full)
SCA (software composition analysis) known-vulnerable dependencies in lockfiles Trivy fs, Grype, osv-scanner; Dependabot/Renovate for fixes every PR, plus daily (new CVEs appear without code changes)
IaC and pipeline config scanning open security groups, privileged pods, unsafe workflows Trivy config, Checkov, Semgrep rules, actionlint/zizmor PRs touching those files
Container image scanning vulnerable OS packages and libraries in the image Trivy, Grype after build, before push; again on a schedule
SBOM the inventory of what shipped Syft, Trivy every release build
DAST (dynamic testing) vulnerabilities in the running app OWASP ZAP and similar against staging, after deploy

None of these replace threat modelling, code review or the secure-coding habits taught in Cybersecurity; they catch the known, mechanical mistakes automatically so humans can focus on the rest.

Gates vs reports

Every check has to be configured as one of two things:

  • a gate: the job fails and blocks the merge or the deployment;
  • a report: findings are published (merge request widget, SARIF in GitHub code scanning, a dashboard) but don't block.

A useful default:

Finding Policy
a new secret gate, always; plus rotation
new SAST findings with high confidence rules gate on the diff; report the rest
fixable critical/high vulnerabilities in the image gate (with an expiry-dated ignore list for accepted risks)
unfixed or low/medium vulnerabilities report, track in a backlog
old findings in code nobody touched report; use baselines so they don't block unrelated work

The last row matters most when you introduce scanning to an existing codebase: if day one turns every pipeline red, the gate will be disabled by day three. Gate new findings, and burn down the old ones separately.

A model: what a finding costs

The economics behind shift-left can be modelled with rough numbers. Each stage catches some share of the issues that reach it; an issue that slips past every stage reaches production. The model compares a pipeline with only late checks against one with early checks too (all numbers illustrative):

python · try it yourself
STAGES = [  # name, minutes of work to fix an issue found here
    ("pre-commit",     5),
    ("pull request",   30),
    ("main pipeline",  120),
    ("staging/DAST",   480),
    ("production",     4800),   # incident, rotation, disclosure
]

def expected_cost(catch_rates, issues=100):
    remaining, cost, found = issues, 0.0, []
    for (name, minutes), rate in zip(STAGES, catch_rates + [1.0]):
        caught = remaining * rate
        cost += caught * minutes
        found.append(f"{name}={caught:.0f}")
        remaining -= caught
    return cost / 60, found

late = [0.0, 0.0, 0.3, 0.5]          # scanning only on main and in staging
early = [0.4, 0.5, 0.3, 0.5]          # hooks and PR scans added in front
for label, rates in [("late checks only", late), ("shift left", early)]:
    hours, found = expected_cost(rates)
    print(f"{label:<17} {hours:6.0f} engineer-hours per 100 issues   caught at: {', '.join(found)}")
late checks only    3140 engineer-hours per 100 issues   caught at: pre-commit=0, pull request=0, main pipeline=30, staging/DAST=35, production=35
shift left           960 engineer-hours per 100 issues   caught at: pre-commit=40, pull request=30, main pipeline=9, staging/DAST=10, production=10

The early stages don't catch everything; they don't have to. Removing most issues before they reach the expensive stages is what cuts the total. The numbers are made up, but the shape (each stage later costs several times more) is the robust finding behind the idea.

The pipeline this section builds

pre-commit:   gitleaks protect, formatters
PR pipeline:  lint → unit tests → Semgrep (diff) → gitleaks (commits in the PR) → build → Trivy fs (lockfiles)
main:         … → build image → Trivy/Grype image gate → Syft SBOM → push by digest → deploy
nightly:      TruffleHog full history, Trivy against the deployed images (new CVEs), Semgrep full scan

The next lessons build each box: Semgrep, writing rules, SonarQube, gitleaks, TruffleHog & GitGuardian, image scanning and SBOMs.

Test yourself

Why scan images again on a schedule, even if nothing was rebuilt?

  1. Images change in the registry
  2. New vulnerabilities are published every day for packages that were clean at build time
  3. Scanners are random
  4. To refresh the cache

You add a SAST scanner to a 10-year-old codebase and it reports 900 findings. What is a sustainable gate?

  1. Fail on all 900
  2. Fail only on new findings (baseline or diff scanning) and track the old ones separately
  3. Disable the scanner
  4. Only scan on Fridays

What does DAST test that SAST can't?

  1. Source code style
  2. The running application's behaviour, from outside
  3. Dependencies
  4. Commit messages