On-ramp · Lesson 01
An HTTP call is text in, text out
An agent's "read the file" is, on the wire, about a dozen lines of text and a reply of about as many. The gateway in Module 3 decides on those lines. A learner who has only seen requests.get(...) has never seen them, and then cannot tell which of the lines an attacker controls.
A request has four parts
Save this as s.py and run python3 s.py. It answers a JSON POST on port 8099 and prints nothing.
import http.server, json
class H(http.server.BaseHTTPRequestHandler):
def do_POST(self):
body = self.rfile.read(int(self.headers.get("content-length", 0)))
out = json.dumps({"ok": True, "got": json.loads(body),
"auth": self.headers.get("authorization")}).encode()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(out)))
self.end_headers()
self.wfile.write(out)
def log_message(self, *a): pass
http.server.HTTPServer(("127.0.0.1", 8099), H).serve_forever()
In a second terminal, -v makes curl print the request it sent (lines starting >) and the reply (<):
curl -v -X POST http://127.0.0.1:8099/call -H 'Content-Type: application/json' \
-H 'Authorization: Bearer s3cret' -d '{"tool":"read_file","args":{"path":"notes/q3.txt"}}'
The request curl sends is this (the User-Agent and Accept lines are curl's):
POST /call HTTP/1.1
Host: 127.0.0.1:8099
User-Agent: curl/8.7.1
Accept: */*
Content-Type: application/json
Authorization: Bearer s3cret
Content-Length: 51
{"tool":"read_file","args":{"path":"notes/q3.txt"}}
| Part | Here | What it is for |
|---|---|---|
| Request line | POST /call HTTP/1.1 |
the method, the path and query, the protocol version |
| Headers | Host, Content-Type, Authorization |
metadata, one Name: value per line, names case-insensitive |
| Blank line | ends the headers | |
| Body | the JSON | the payload, Content-Length bytes of it |
The reply has the same shape, with a status line in place of the request line: here HTTP/1.0 200 OK, because BaseHTTPRequestHandler speaks HTTP/1.0 unless its protocol_version is set to "HTTP/1.1". The code is a promise about the outcome (RFC 9110, 2022): 2xx succeeded, 3xx look elsewhere, 4xx the request is at fault, 5xx the server is. The ones the course keeps meeting are 200, 400, 401 (no valid credential), 403 (a valid credential that is not enough), 404, 429 (too many) and 502 (a gateway got a bad reply upstream).
flowchart TD C["Client: method, target, headers, body"] -->|"one TCP connection"| S["Server"] S -->|"status, headers, body"| C
Who controls which line
The Host header, the path, every header and the body come from whoever wrote the request. The one thing the client does not write is the address it connected to: the peer's IP is known to the server's operating system, not claimed in a header. A gateway that logs X-Forwarded-For as "the caller" has logged text the caller typed (lesson 6).
A URL is how a client names where to send this: https://crm.acme.example:8443/mcp?x=1 has a scheme (https), an authority (crm.acme.example:8443, the host and optional port; a leading user@ is allowed and rarely wise), a path and a query (RFC 3986, 2005). The client splits it into the connection (to the host) and the request line plus Host header (the rest). Two URLs that look alike can have different authorities: https://crm.acme.example@evil.example/ connects to evil.example.
Methods say what is meant, not what happens
GET should only read and POST may do anything, but the server decides what a method does. A tool API that deletes on GET /delete?id=7 will be fired by a link preview. A gateway that allows "reads" by method alone has trusted the sender's description of the call.
Expert: one connection, many messages
HTTP/1.1 sends messages on a TCP stream with no delimiters but the headers' own Content-Length or Transfer-Encoding (RFC 9112, 2022). Two parsers that disagree on where one message ends and the next begins are the root of request smuggling; this is why a gateway and the server behind it must use one parser's rules, and why a gateway should reject a message with both headers (RFC 9112, section 6.3, says such a message ought to be handled as an error). The course meets the same disagreement for JSON in Module 3 lesson 6; the next lesson is its smaller cousin.
Check yourself
- A prompt-injected agent writes the request above. Which of its parts can it set to any value it likes, and which single fact about the request reaches the server without passing through the agent's hands?
- A tool service answers
200with{"error": "no such file"}. What will a retry loop that looks only at the status code do, and what will a count of "successful calls" built on status codes report? https://acme.example:443@evil.example/andhttps://acme.example/@evil.examplelook similar. Which host does each connect to?
Answers
- Everything it writes: the method, the path and query, every header (
HostandAuthorizationincluded), the body, and withContent-Lengththe framing too. The one fact the agent does not write is the address of the TCP peer, which the server's operating system knows from the connection itself. - The loop sees
200, counts the call as done and never retries, and the "successful calls" count includes every failure reported in the body, so it overstates success. The status code is the server's promise about the outcome; this server broke it, so anything that matters must read the body too. - The first connects to
evil.example: everything before the@in the authority is userinfo, soacme.exampleis a username and443a password. The second connects toacme.example: the authority ends at the first/, and/@evil.exampleis merely its path.