Before the last release, you ran npm audit, read the Semgrep results, and checked the headers one by one. This week you're busy, and a pull request that adds a payment key to the code passes with nobody checking. A check that depends on someone remembering will be skipped at some point.
The solution is security automation: machines run the security checks on every code change, instead of waiting for someone to remember. Picture baggage screening at an airport. Every bag goes through the X-ray machine, not just the ones that look suspicious, and the belt stops when something forbidden is found. In CI, every pull request goes through a security stage (a step of security checks). If a check finds something serious, the merge is blocked until that finding is fixed. The bag is the pull request, the X-ray machine is the security stage, and the belt that stops is the blocked merge.
DevSecOps and shift left
DevOps is a way of working that joins development (writing code) and operations (running the app on servers). DevSecOps is building security into every DevOps stage, instead of adding it at the end before release. Its principle is shift left: move the checks to the left on the timeline, as early as possible. The timeline runs from the developer's laptop on the left to production on the right, in the order of the table below. The reason: a bug found in a pull request is far cheaper to fix than a bug found in production.
| Stage | Automatic check | Example tools |
|---|---|---|
| Laptop, before a commit | secret scanning in a pre-commit hook | gitleaks |
| Pull request (CI) | dependency audit (SCA), secret scanning, SAST | npm audit, gitleaks, Semgrep, CodeQL |
| Staging | DAST, a security header check | OWASP ZAP, curl -sI |
| Production | logging, monitoring, alerts | the Security Logging & Monitoring lesson |
Staging is a test server that looks like production, where the app is tried before real users use it.
You already know almost all of these checks: SAST and DAST from Secure Code Review, npm audit from Dependency Vulnerabilities, secret scanning from Incident Response, and headers from Complete Security Headers. The only new one is gitleaks, explained below. This lesson joins them into one gate before the merge.
Four checks in the security stage
This is the same case as the Lab: PR #57 in the OurShop repo adds card payments. The security stage runs four jobs, and three of them are red (failed):
| Job | Command | Finding in PR #57 |
|---|---|---|
| dependency-audit | npm audit --audit-level=high |
lodash 4.17.20, severity high (Command Injection in lodash, GHSA-35jh-r3h4-6jhm) |
| secret-scan | gitleaks | stripe-access-token in src/payment.js line 3 |
| sast | semgrep scan --config p/owasp-top-ten --error |
a SQL query built from user input in src/orders.js |
| header-check | curl -sI against staging |
passes: HSTS, CSP, and nosniff are sent |
Each job ends with an exit code. 0 means pass, and the job is green. Any other number means fail, and CI marks that job red.
npm auditmatches every version inpackage-lock.jsonagainst an advisory database.--audit-level=highmeans "fail if there is a high or critical hole".- gitleaks is a program that looks for secret patterns (API keys, tokens) in the code and the git history.
semgrep ... --errormakes Semgrep exit with code 1 when there are findings. Without--error, the findings are printed but the job stays green.
The threshold is the severity at which a job fails. If the threshold is loosened to --audit-level=critical, npm audit only fails for critical holes. The high lodash hole is still printed in the log, but the exit code is 0, the job is green, and the merge passes. That's why high is the usual starting point.
The workflow
This is the GitHub Actions workflow that runs the four jobs:
# .github/workflows/security.yml
name: security
on: [pull_request]
jobs:
dependency-audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- run: npm audit --audit-level=high
secret-scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0
- uses: gitleaks/gitleaks-action@v3
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
sast:
runs-on: ubuntu-latest
container: semgrep/semgrep
steps:
- uses: actions/checkout@v7
- run: semgrep scan --config p/owasp-top-ten --error
header-check:
runs-on: ubuntu-latest
steps:
- run: |
headers=$(curl -sI https://staging.ourshop.com)
for h in strict-transport-security content-security-policy x-content-type-options; do
echo "$headers" | grep -qi "^$h:" || { echo "missing header: $h"; exit 1; }
done
on: [pull_request]runs this workflow on every pull request.- The four jobs run in parallel (at the same time), so the security stage doesn't make CI much slower.
fetch-depth: 0fetches the whole history, so gitleaks also checks older commits on the pull request's branch.- For a repo owned by an organisation,
gitleaks-actionalso needs aGITLEAKS_LICENSEsecret (the licence is free, you only register). container: semgrep/semgrepruns the job inside Semgrep's official Docker image.- In
header-check,curl -sIfetches only the headers of the staging response, andgrep -qilooks for each header name (upper and lower case count as the same). If a header is not found, the script printsmissing header: ..., then runsexit 1(the job fails).
The workflow above is kept short so it is easy to read. It calls its actions by the tags of their latest versions, checkout@v7 and gitleaks-action@v3, which run on Node 24. The old versions, @v4 and @v2, were made for Node 20, and Node 20 has been removed from GitHub's runners. Before you use this workflow, apply the rules from the lesson CI/CD Pipeline Security: pin each action to the commit SHA of its latest version, and limit permissions. For example, for the secret-scan job:
permissions:
contents: read # the limit for every job
jobs:
secret-scan:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write # gitleaks reads the PR commits and writes comments
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
- uses: gitleaks/gitleaks-action@e0c47f4f8be36e29cdc102c57e68cb5cbf0e8d1e # v3.0.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
permissions written at the job level replaces the whole top-level limit, instead of adding to it. That is why contents: read is written again in that job.
The merge gate: branch protection
A red job blocks nothing by itself. On GitHub, turn on branch protection (protection rules for one branch), or a ruleset (GitHub's newer rules feature), for main. Then make the four jobs required with required status checks (checks that must be green): the merge button works only when all of them are green. Without these rules, the security stage's results are only notifications that are easy to ignore.
- Fail fast (fail as early as possible): block in the PR, not on release day.
- Fix the finding, don't switch off the check. Upgrade the dependency (
npm audit fix, or an automatic PR from Dependabot), move the secret to a secret manager and rotate it, use a parameterized query. - Cut the noise (warnings that don't help): a reviewed false positive may be excluded with a written reason, such as a
// nosemgrepcomment on that line. People switch off a tool that is too noisy.
DAST in the pipeline: OWASP ZAP
For DAST, OWASP ZAP has ready-made scripts in its official Docker image:
docker run -t ghcr.io/zaproxy/zaproxy:stable zap-baseline.py -t https://staging.ourshop.com
zap-baseline.pyexplores the site briefly and only observes the responses (a passive scan), without attacking. So it is fast and safe for every PR or every night.-t https://staging.ourshop.comnames the site to test.zap-full-scan.pyalso runs active attacks, so it takes longer. Run it only against your own staging, on a schedule.zap-api-scan.pyis for APIs. It reads an OpenAPI or GraphQL definition (a file that describes your API's endpoints).- The old image,
owasp/zap2docker-stable, is no longer updated. Useghcr.io/zaproxy/zaproxy:stable.
⚠️ Why does the hole still reach main?
- A threshold that is too loose.
--audit-level=criticallets high holes pass without anyone noticing. Start athigh. - Checks that don't block. Without required status checks in branch protection, a pull request with a red job can still be merged.
- Forgetting
--errorin Semgrep. The findings are printed, the job is green, and nobody reads the log. - Switching off a noisy tool. Adjust (tune) its rules and exclude false positives one at a time with a reason, don't switch off the whole check.
TL;DR: DevSecOps builds security checks into every stage, from the laptop to production, on the shift-left principle: check as early as possible. The security stage (a step of security checks in CI) runs four jobs in parallel on every pull request. They are a dependency audit (npm audit --audit-level=high), secret scanning (gitleaks), SAST (semgrep ... --error, which reads code without running it), and a header check. A job that fails exits with a code other than 0. Branch protection (protection rules for a branch) with required status checks (checks that must be green) is what turns a red (failed) job into a blocked merge. Fix the findings, don't loosen the threshold (the severity at which a job fails) or switch off the check. For DAST (testing the running app), use zap-baseline.py from the ghcr.io/zaproxy/zaproxy:stable image on every PR, and zap-full-scan.py on a schedule against your own staging (test server).
⚡ Machines check every change
Meaning: Security automation runs security checks on every code change, instead of waiting for someone to remember. DevSecOps builds security into every DevOps stage, from writing code to running the app on servers. Its principle is shift left: check as early as possible.
⚡ The exit code decides red or green
Write:
- run: npm audit --audit-level=high
- run: semgrep scan --config p/owasp-top-ten --error
Guess first: Semgrep (a tool that reads code to look for patterns of holes) finds problems, but the command is written without --error. Does the CI job go red (fail)?
No, it stays green. The findings are printed, but the exit code is 0. --error is what makes it exit with code 1.
Meaning: Each CI job ends with an exit code. Exit code 0 means pass (green), anything else means fail (red). npm audit matches your dependencies against a list of publicly known holes, and --audit-level=high makes it fail for a high or critical finding.
⚡ The merge gate
Meaning: A red job blocks nothing by itself. Turn on branch protection (protection rules for the main branch) with required status checks (checks that must be green). Then every security check job, such as npm audit and Semgrep, must be green before the merge button works.
⚡ DAST with OWASP ZAP
Meaning: DAST tests the running app, and OWASP ZAP is one of its tools. zap-baseline.py only observes the responses (passive), so it is fast and safe for every PR. zap-full-scan.py also attacks and takes longer, so run it on a schedule, only against your own staging (test server). Fix the finding, don't switch off the check.