Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Security gates that block a merge

After this lesson: I can set up a security gate that stops a risky change being merged.

60 min lesson14 commandsDevSecOps lab: build a security pipeline and a gate

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

Five reports, nobody reading them

The shop now has five scanners. Each one prints a long report, and the team has stopped reading them.

A gate reads every report for them, makes one decision, and stops a change from being merged until the decision is yes.

By the end of this lesson

Note

I can set up a security gate that stops a risky change being merged.

Key words

WordWhat it meansExample
Gatea pipeline step that passes or blocks a change
Policythe rules a gate applies"fail_at": "high"
Baselineknown findings accepted for a time
Commit statusa pass or fail mark on one commit
Protected brancha branch that needs checks before a merge

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

One file runs every scanner

A pipeline is a file in the repository listing each step. The lab's Gitea has no runner attached, so you run the file yourself, as a runner would.

Each scanner writes a JSON report to /work/reports. The last step, gate.py, reads them all and applies the policy.

Write the pipeline file

Command
cat > pipeline.sh <<'EOF'
docker exec gitleaks git -C /work/tuckshop pull -q
docker exec semgrep semgrep scan --config /work/tuckshop/.semgrep.yml --metrics=off --quiet --json-output=/work/reports/semgrep.json /work/tuckshop > /dev/null
docker exec gitleaks gitleaks git /work/tuckshop --no-banner --redact --report-format json --report-path /work/reports/gitleaks.json 2> /dev/null
docker exec ci pip install -q --user --no-warn-script-location pip-audit==2.10.1
docker exec ci python3 -m pip_audit -r /work/tuckshop/requirements.txt --no-deps --disable-pip -f json -o /work/reports/audit.json 2> /dev/null
docker exec trivy trivy config -q -f json -o /work/reports/trivy.json /work/tuckshop
docker exec ci python3 dast.py http://shop:8000/ --json /work/reports/dast.json > /dev/null
docker exec ci python3 gate.py
EOF

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Run the pipeline on main

Command
sh pipeline.sh

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A protected branch needs the status

A protected branch can require a check before anything is merged into it. The gate reports by setting a commit status called security-gate on the newest commit.

Protect main, then let the gate mark main's newest commit. Open Gitea from the lab's tab to see the red cross.

Require security-gate on main

Command
curl -s -o /dev/null -w "%{http_code}\n" -u student:student -H "Content-Type: application/json" -d '{"rule_name":"main","enable_status_check":true,"status_check_contexts":["security-gate"]}' http://gitea:3000/api/v1/repos/student/tuckshop/branch_protections

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Mark main's newest commit

Command
docker exec ci python3 gate.py --post main

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A common mistake

Warning

Many people think a gate should block every finding from day one. On a project full of old findings, that blocks every change, and teams switch the gate off. Start by blocking the worst, accept known debt with a reason and a date, and tighten the policy as it is fixed.

A policy with a reason and a date

The policy file lives in the repository, so a change to it is reviewed like code. This one blocks only critical findings. Its baseline accepts the rotated key until 2030.

It also holds an entry with no reason and a past date. The gate ignores that one, as a gate should.

Clone the shop in your terminal

Command
test -d tuckshop || git clone -q http://student:student@gitea:3000/student/tuckshop.git

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Start a branch for the policy

Command
git -C tuckshop switch -q -c gate-policy

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Write the policy file

Command
cat > tuckshop/security-policy.json <<'EOF'
{
  "fail_at": "critical",
  "baseline": [
    {"key": "gitleaks:generic-api-key:settings.py", "reason": "Rotated on 13 January 2026, old key revoked", "expires": "2030-06-30"},
    {"key": "semgrep:weak-password-hash:app.py", "reason": "", "expires": "2026-01-31"}
  ]
}
EOF

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Commit and push the branch

Command
git -C tuckshop add security-policy.json && git -C tuckshop -c user.name=Student -c [email protected] commit -qm "Add the security policy" && git -C tuckshop push -q -u origin gate-policy

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Open a pull request

Command
curl -s -u student:student -H "Content-Type: application/json" -d '{"head":"gate-policy","base":"main","title":"Add the security policy"}' http://gitea:3000/api/v1/repos/student/tuckshop/pulls | grep -o '"number":[0-9]*' | head -1

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Try to merge before the gate runs

Command
curl -s -u student:student -H "Content-Type: application/json" -d '{"Do":"merge"}' http://gitea:3000/api/v1/repos/student/tuckshop/pulls/1/merge

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check out the branch for the pipeline

Command
docker exec gitleaks sh -c "cd /work/tuckshop && git fetch -q origin && git checkout -q gate-policy"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Run the pipeline on the branch

Command
sh pipeline.sh

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Mark the branch's commit

Command
docker exec ci python3 gate.py --post gate-policy

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Merge now the gate has passed

Command
curl -s -o /dev/null -w "%{http_code}\n" -u student:student -H "Content-Type: application/json" -d '{"Do":"merge"}' http://gitea:3000/api/v1/repos/student/tuckshop/pulls/1/merge

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Assignment: decide like a gate

A finding is a tuple (tool, rule, where, level), with level one of "low", "medium", "high", "critical". A finding's key is tool:rule:where. A policy is a dictionary such as {"fail_at": "high", "baseline": [{"key": "...", "reason": "...", "expires": "2030-06-30"}]}. Dates are YYYY-MM-DD strings. Write three functions.

accepted(policy, today) returns the set of baseline keys that have a non-empty reason and an expires date on or after today. blocking(findings, policy, today) returns the findings, in the order given, whose level is at or above fail_at and whose key is not accepted. decide(findings, policy, today) returns "PASS" when nothing blocks and otherwise "FAIL: " followed by the number of blocking findings.

Code: python
SCALE = ["low", "medium", "high", "critical"]


def accepted(policy, today):
    return set()


def blocking(findings, policy, today):
    return []


def decide(findings, policy, today):
    return ""

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.