DAST with ZAP
Test the running app as a job service, then fix what it finds.
cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-gitlab/dast.tar.gz && tar -xzf dast.tar.gz, which creates ~/lab/dast/. SHA-256: fa91b1b463ec2930edd03529d7099aeba6b1e62e34379c6112eca51e30cc7901SAST reads the code, and dependency and image scans read package lists. None of them sees what the running application sends back: the headers, the cookies, the error pages. Dynamic application security testing (DAST) sends requests to a running app and checks the responses. GitLab's own DAST needs the Ultimate tier. ZAP, the open-source scanner GitLab's earlier DAST analyzer was built on, runs in an ordinary job on any tier.
The app
"""A tiny web app for the DAST lab. It sets a session cookie and sends no security headers."""from http.server import BaseHTTPRequestHandler, HTTPServerHARDENED = False # the solution flips thisclass App(BaseHTTPRequestHandler):def do_GET(self):body = b"<html><head><title>Lab shop</title></head><body><h1>Lab shop</h1><a href='/about'>About</a></body></html>"self.send_response(200)self.send_header("Content-Type", "text/html; charset=utf-8")if HARDENED:self.send_header("Set-Cookie", "session=abc123; HttpOnly; Secure; SameSite=Strict; Path=/")self.send_header("Content-Security-Policy", "default-src 'self'; frame-ancestors 'none'")self.send_header("X-Content-Type-Options", "nosniff")else:self.send_header("Set-Cookie", "session=abc123; Path=/")self.end_headers()self.wfile.write(body)def log_message(self, *args):passHTTPServer(("0.0.0.0", 8000), App).serve_forever()
A shop page with a session cookie and no security headers. Build its image on the runner's host, so a job can start it as a service, and check what it sends. That works only because the lab runner uses a local image when one exists. In production, push the app image to a registry and start the service from it by digest:
ZAP against a job service
# DAST against the running app: the app starts as a service, ZAP's baseline scan crawls it and# checks the responses passively. GitLab's own DAST needs the Ultimate tier; ZAP runs on any tier.dast:image:name: zaproxy/zap-stable:2.17.0@sha256:9624669db72f54d86c4b46a7064518bd9bf79d8c61a4c31befa9923489f328afentrypoint: [""]services:- name: secopslog-dastapp:1alias: appscript:- mkdir -p /zap/wrk && cp zap-rules.tsv /zap/wrk/# One scan. Exit codes: 0 clean, 1 a FAIL rule matched, 2 warnings only, 3 the scan itself broke.- zap-baseline.py -t http://app:8000 -c zap-rules.tsv -J zap.json -m 1 > zap.log 2>&1 || rc=$?- cp /zap/wrk/zap.json . || true- grep -E '^(FAIL-NEW|WARN-NEW|Total of)' zap.log | sed -E 's/ x [0-9]+.*$//'- echo "zap-baseline exit code ${rc:-0}"- test "${rc:-0}" -eq 0 || test "${rc:-0}" -eq 2artifacts:when: alwayspaths: [zap.json, zap.log]
services starts the app next to the job, reachable as app. zap-baseline.py crawls it for a minute and checks every response passively: it reads what comes back and sends nothing an attack would. The rules file decides what each check means for the pipeline:
10010 FAIL (Cookie No HttpOnly Flag)10038 FAIL (Content Security Policy Header Not Set)
Checks listed as FAIL fail the job, and everything else stays a warning. The baseline script exits 2 when there are only warnings, which the job accepts, and 1 on a failure.
ZAP crawled four URLs and reported nine problems. The two in the rules file are FAIL-NEW: the cookie has no HttpOnly flag, so page scripts can read it, and there is no Content Security Policy. The other seven are warnings, among them X-Content-Type-Options missing and a Server header that gives away the server version. zap-baseline exit code 1 failed the job, and zap.json and zap.log were kept as artifacts, because they are when: always.
Fix the app
The fix sets HttpOnly, Secure and SameSite on the cookie and sends a Content Security Policy and X-Content-Type-Options. Rebuild the image and run the pipeline again:
No failures, and the job passed with zap-baseline exit code 2: warnings only. Both failing checks and three of the warnings are gone, and a new warning appeared, CSP: Failure to Define Directive with No Fallback. ZAP can only judge a policy once there is one. Five warnings remain, and they stay warnings until you decide otherwise.
| Test | Looks at | Finds |
|---|---|---|
| SAST | Source code | Dangerous calls, weak crypto |
| Dependency scanning | Pinned package versions | Known vulnerable libraries |
| Container scanning | Packages in an image | Known vulnerable OS packages |
| DAST baseline (passive) | Responses of the running app | Missing headers, weak cookies, information leaks |
| DAST active scan | The app under attack | Injection and other exploitable bugs |
Hands-on
The fixed app still produces warnings. Make one of them fail the pipeline: add the line 10036\tFAIL\t(Server Leaks Version Information) to app/zap-rules.tsv (the separators are tabs, so printf it rather than typing it), push with ./commit.sh and run ./pipeline.sh app/. What does the app reveal, and how would you remove it? Finish with ./down.sh.
What you should see: FAIL-NEW: Server Leaks Version Information via "Server" HTTP Response Header Field [10036], exit code 1, and a failed pipeline. Python's built-in HTTP server sends a Server header with its own name and the Python version. To fix it, set the header yourself (in http.server, override version_string()), or let the reverse proxy in front of the app replace it. The rules file is where a team records which checks it has decided to enforce.
Remember
Run a passive baseline scan against the app as a job service on every pipeline, decide in a rules file which checks fail the build, and keep active scans for environments you own and can reset.