HTTP Requests and Responses for Testers
Every web attack in this course is, in the end, an HTTP request that the developer didn't expect. To find those requests, and to read the logs that record them, you need to see HTTP the way the server sees it: a request line, headers, an optional body, and nothing else. Networking for Security showed HTTP on the wire in a capture. This lesson looks at the same messages from the tester's side: which parts the client controls (all of them), which headers matter for security, and how a careless error page turns an ordinary request into an information leak.
Definitions
- Request: a request line
METHOD target HTTP/version, header linesName: value, a blank line, an optional body. HTTP/1.1 is defined in RFC 9112, the semantics shared by all versions (methods, status codes, headers) in RFC 9110. HTTP/2 and HTTP/3 change the framing, not the meaning. - Safe and idempotent methods:
GET,HEAD,OPTIONSare safe (must not change state);PUTandDELETEare idempotent (repeating them has the same effect). An application that changes data onGETbreaks this contract, and CSRF defences that only coverPOSTthen miss it. - Status classes: 2xx success, 3xx redirect (
Location), 4xx client error (400, 401 unauthenticated, 403 forbidden, 404, 405 method not allowed, 429 too many requests), 5xx server error. - Security-relevant request headers:
Host(which site the client wants; used for virtual hosting and, dangerously, for building links),Cookie,Authorization,Origin(sent by browsers on cross-origin and state-changing requests),Referer,Content-Type,X-Forwarded-For(set by proxies, forgeable by clients). - Security-relevant response headers:
Set-Cookie,Location,Content-Type,Cache-Control, and the security headers of a later lesson.
Seeing the whole exchange
The session starts a toy app on 127.0.0.1 in your sandbox. curl -v prints the request lines it sends (>) and the response lines it receives (<); the Date line is filtered out because it changes every run.
cat > app.py <<'PY'
import sys, traceback
from urllib.parse import parse_qs
from wsgiref.simple_server import make_server, WSGIRequestHandler
DEBUG = sys.argv[1] == "debug"
class Quiet(WSGIRequestHandler):
def log_message(self, *args):
pass
def app(environ, start_response):
q = parse_qs(environ.get("QUERY_STRING", ""))
try:
if environ["PATH_INFO"] == "/split":
n = int(q["parts"][0])
body = f"each part gets {100 // n}%\n"
else:
body = f"{environ['REQUEST_METHOD']} {environ['PATH_INFO']} host={environ.get('HTTP_HOST')}\n"
start_response("200 OK", [("Content-Type", "text/plain")])
return [body.encode()]
except Exception:
if DEBUG:
start_response("500 Internal Server Error", [("Content-Type", "text/plain")])
return [traceback.format_exc().encode()]
rid = "7f3a9c01" # fixed for a stable transcript; real code: a fresh unpredictable id
with open("errors.log", "a") as f:
f.write(f"request {rid}: {traceback.format_exc().splitlines()[-1]}\n")
start_response("500 Internal Server Error", [("Content-Type", "text/plain")])
return [f"Something went wrong. Reference: {rid}\n".encode()]
make_server("127.0.0.1", 8000, app, handler_class=Quiet).serve_forever()
PY
python3 app.py debug &
for i in $(seq 50); do curl -s -o /dev/null http://127.0.0.1:8000/ && break; sleep 0.1; done
echo "== a plain GET"
curl -sv http://127.0.0.1:8000/hello 2>&1 | grep -E '^[<>]' | grep -v '^< Date'
echo "== the client controls every header, including Host"
curl -s -H 'Host: admin.lab.test' -X DELETE http://127.0.0.1:8000/hello
echo "== debug mode: an unexpected input"
curl -s 'http://127.0.0.1:8000/split?parts=0'
kill %1; wait 2>/dev/null
python3 app.py production &
for i in $(seq 50); do curl -s -o /dev/null http://127.0.0.1:8000/ && break; sleep 0.1; done
echo "== production mode: the same input"
curl -s -w '[%{http_code}]\n' 'http://127.0.0.1:8000/split?parts=0'
echo "== what the server kept for itself"
cat errors.log
kill %1== a plain GET
> GET /hello HTTP/1.1
> Host: 127.0.0.1:8000
> User-Agent: curl/8.5.0
> Accept: */*
>
< HTTP/1.0 200 OK
< Server: WSGIServer/0.2 CPython/3.12.3
< Content-Type: text/plain
< Content-Length: 31
<
== the client controls every header, including Host
DELETE /hello host=admin.lab.test
== debug mode: an unexpected input
Traceback (most recent call last):
File "/home/learner/app.py", line 16, in app
body = f"each part gets {100 // n}%\n"
~~~~^^~~
ZeroDivisionError: integer division or modulo by zero
== production mode: the same input
Something went wrong. Reference: 7f3a9c01
[500]
== what the server kept for itself
request 7f3a9c01: ZeroDivisionError: integer division or modulo by zero
Three things to notice:
- The request is just text.
curlchose the method, theHostheader and theUser-Agent; a browser chooses them for ordinary users, but an attacker chooses freely. The app echoedhost=admin.lab.testbecause it believed the header. Applications that build password-reset links fromHostsend victims links to the attacker's domain (Host header injection); the fix is to build absolute URLs from configuration, never from the request. - The
Serverheader names the software and its version (WSGIServer/0.2 CPython/3.12.3). That alone isn't a vulnerability, but it saves an attacker a step; most production servers can hide or shorten it. - The debug traceback leaks the file path, the source line and the exception type. In real frameworks a debug page shows environment variables, settings, SQL queries and sometimes an interactive console. The production version returns a generic message with a reference, logs the detail server-side, and the reference lets support find the log line.
How information leaks are detected
- Testing: send unexpected input to every parameter (empty, zero, very long, wrong type, a quote), unexpected methods (
PUT,DELETE,TRACE,OPTIONS) and a foreignHost. Look for stack traces, framework names, internal paths and versions in responses and headers. OWASP WSTG's information-gathering and error-handling tests list these. - Configuration review: debug flags (
DEBUG = True,app.run(debug=True),ASPNETCORE_ENVIRONMENT=Development) in production settings. - Monitoring: a spike of 5xx responses from one client is often someone fuzzing parameters.
How it is fixed
- Debug off in production, enforced by deployment checks; generic error pages with a reference id; details only in server logs.
- Validate input before use (
partsmust be an integer from 1 to 100), returning 400 rather than crashing with 500. Unhandled exceptions are their own OWASP 2025 category, A10 Mishandling of Exceptional Conditions. - Don't trust
HostorX-Forwarded-*: configure the allowed host names (Django'sALLOWED_HOSTS, for example), and accept forwarding headers only from your own proxies. - Reject unused methods with 405, and never change state on
GET.
Pitfalls
- Assuming a header can't be set by the client. Only a handful of headers are "forbidden" for JavaScript in browsers (
Host,Cookie,Origin…); outside a browser every header is free. - Logging the traceback and returning it. Logs are fine; responses are public.
- Reference ids that are sequential reveal request volume; use random ones (here it is fixed only so the transcript is stable).
Practice
1. A password-reset email contains https://{Host}/reset?token=…. What does an attacker send, and what happens?
A reset request for the victim's account with Host: attacker.lab.test. The victim receives a genuine email whose link points to the attacker's server; clicking it hands over the token. Fix: build the link from a configured base URL.
2. Which status should /split?parts=abc return in a well-written app?
400 Bad Request with a short message ("parts must be an integer 1–100"). A 500 means the input reached code that didn't expect it.
Test yourself
Which part of an HTTP request can an attacker using curl NOT control?
- The method
- The Host header
- The body
- None: they control all of it
What did the production version of the app do differently on an error?
- It crashed
- It returned a generic message with a reference and logged the details server-side
- It returned the traceback in a header
- It redirected to the home page
Why must a GET request never change state?
- GET is slower
- GET is defined as safe; links, prefetchers and cross-site images trigger it freely, and CSRF defences often cover only POST
- GET can't carry parameters
- Browsers cache all GET responses forever