The code passed, the site did not
The shop's code can pass every scan and the live site still send no security headers, or show a crash report to anybody.
Some problems only appear when the app runs. So the pipeline starts it and tests it from outside, as a visitor would.
The one rule
Warning
Scan only your own lab copy. Scanning a site you do not own, without written permission, can be a crime: in the UK under the Computer Misuse Act 1990. Even a gentle scan sends hundreds of requests to someone else's server.
Quick check: the rule
Have a go
Quick check: the rule
3 questionsBy the end of this lesson
Note
I can run a baseline scan against my own app and act on what it finds.
Key words
| Word | What it means |
|---|---|
| DAST | testing a running app from outside |
| Crawl | follow every link to find the pages |
| Passive check | reads replies and sends nothing new |
| Active check | sends a probe to see how the app reacts |
| Baseline | a quick, safe scan run on every change |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
3 questionsDAST looks at the app from outside
DAST sends real requests and reads the replies. It does not need the code, so it finds what SAST cannot see, such as a missing header.
The usual open-source tool is OWASP ZAP. It is too large for this lab, so the lab has dast.py, a small scanner written to work the same way.
Look at the shop's reply headers
curl -s -D - -o /dev/null http://shop:8000/Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Run a passive baseline scan
docker exec ci python3 dast.py http://shop:8000/Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Try it: check headers yourself
headers = {
"Server": "TuckShop/0.9 Python/3.12.15",
"Content-Type": "text/html; charset=utf-8",
"Set-Cookie": "cart=5f2a; Path=/",
}
for wanted in ["Content-Security-Policy", "X-Content-Type-Options", "X-Frame-Options"]:
print(wanted, "present" if wanted in headers else "MISSING")
print("HttpOnly cookie:", "HttpOnly" in headers["Set-Cookie"])
# Add "X-Frame-Options": "DENY" to the headers and run it again.
Make a free account to save your work and track your progress.
Check
Have a go
Check
3 questionsAn active check sends a probe
An active check changes a request and watches what comes back. dast.py --active puts a harmless marker in each query value and looks for it, unescaped, in the reply.
If it comes back as written, the page would run a script put there by an attacker. Run active checks only on your own copy.
Add the active check
docker exec ci python3 dast.py http://shop:8000/ --activeShown here, not run. Press Start lab to type it into your own lab’s terminal.
Scan the hardened shop the same way
docker exec ci python3 dast.py http://shop:8001/ --activeShown here, not run. Press Start lab to type it into your own lab’s terminal.
Read the hardened shop's headers
curl -s -D - -o /dev/null http://shop:8001/Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA common mistake
Warning
Many people think a clean DAST scan means the app is secure. A baseline scan sees replies, not code. The shop's joined SQL, which Semgrep found in lesson 1, never showed in this scan. Use DAST beside SAST, not instead of it.
A rules file turns warnings into failures
In a pipeline, each check is set to IGNORE, WARN or FAIL, with a reason. Anything set to FAIL makes the scan exit 1, and the step fails.
Here a missing policy and a stack trace fail. The server header is ignored, because a proxy in front removes it.
Write the scan's rules file
docker exec -i ci sh -c 'cat > /work/rules/dast.conf' <<'EOF'
1 FAIL every page needs a policy
4 IGNORE the proxy in front removes the header
7 FAIL never show a stack trace
EOFShown here, not run. Press Start lab to type it into your own lab’s terminal.
Run the scan as a pipeline step
docker exec ci python3 dast.py http://shop:8000/ --config /work/rules/dast.conf --json /work/reports/dast.json; echo "exit $?"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Fix it, then spot a scan
The fix is in the app: set the headers and cookie flags on every reply, and show a plain error page. Port 8001 is that fix.
A scan is easy to spot in a server's log: one address, many pages in seconds, and odd values in the query.
Count the probes in the shop's log
docker logs shop 2>&1 | grep -c "probe"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Assignment: a header checker
A scanner sees each reply as a dictionary of headers. Header names are not case-sensitive, so compare them in lower case. Write three functions.
missing(headers) returns the sorted list of names from ["content-security-policy", "x-content-type-options", "x-frame-options"] that the reply lacks, except that x-frame-options is not missing when the policy contains frame-ancestors. cookie_problems(cookie) takes one Set-Cookie value and returns the sorted list of flags it lacks, from HttpOnly, SameSite and Secure, compared in any case. leaks_version(headers) returns True when the Server header contains a digit.
def missing(headers):
return []
def cookie_problems(cookie):
return []
def leaks_version(headers):
return False
Make a free account to save your work and track your progress.
Exit quiz
Have a go