Defending AI Agents
עברית Join waitlist
Menu

On-ramp · Lesson 06

A proxy is a server that is also a client

Acme's agent talks to gateway.acme.example, which forwards the call to the CRM. The CRM's log shows every request from the gateway's address. To see who really called, an engineer trusts the X-Forwarded-For header the gateway passes along. An attacker who sends their own X-Forwarded-For: 10.0.0.5 straight to a gateway that appends to it, rather than replacing it, now appears in the log as an internal host.

Three words for one idea

An intermediary receives a request and sends another (RFC 9110, 2022). Three shapes matter:

  • Forward proxy: configured in the client (HTTP_PROXY, a browser setting). The client names the real destination, and the proxy fetches it. Egress controls live here.
  • Reverse proxy: placed in front of servers. The client thinks it is talking to the server. Load balancers, API gateways and the course's gateway are reverse proxies.
  • Tunnel: after a CONNECT crm.acme.example:443 request the proxy relays bytes both ways and sees only ciphertext (lesson 7).
flowchart TD
  A["Agent (client)"] -->|"POST /call"| G["Gateway: server to the agent, client to the CRM"]
  G -->|"POST /records, own connection, own headers"| T["CRM (server)"]
  T -->|"reply"| G
  G -->|"reply"| A

There are two connections, and two sets of headers. The gateway decides what crosses from one to the other. That is the whole security idea: a proxy is a place where one thing is read, a decision is made, and a possibly different thing is written.

Which headers you may believe

Header Says Who wrote it
Host the name the client addressed the client, anything it likes
X-Forwarded-For, Forwarded (RFC 7239) the chain of client addresses each hop appends; the first value is the client's own claim
Authorization a credential the client; useless to forward unchanged (Module 3, lesson 10)
Connection, Upgrade, TE hop-by-hop: for this connection only an intermediary MUST remove Connection and the fields it names, and SHOULD remove TE, Upgrade and Transfer-Encoding (RFC 9110, section 7.6.1)
Content-Length, Transfer-Encoding message framing; Transfer-Encoding is hop-by-hop too whoever sent it on this hop; see lesson 1

The rule: believe the address of the TCP peer you accepted the connection from, and the forwarding headers only if that peer is a proxy you run. A gateway that sits directly on the internet should discard any X-Forwarded-* it receives and write its own. A service behind a trusted proxy should read the rightmost entry the proxy added, not the leftmost one the client typed.

What a gateway forwards

The course's gateway passes an allow-list of headers upstream, five of them, and never Authorization or cookies. An allow-list is correct for the same reason as before: the list of headers an attacker may add is unbounded, the list you know the server needs is short. A deny-list of dangerous headers is always one entry behind.

Expert: the proxy is also where limits live

Timeouts, body-size caps, connection reuse and retries are the proxy's, and each changes meaning. A gateway that retries a POST on a timeout can run a side effect twice (Module 21, lesson 7: loops that must not run twice). A body cap lower upstream than at the gateway turns into a confusing 502. Hop-by-hop handling was the root of cache-poisoning and smuggling bugs because two proxies in a chain disagreed on which headers were whose.

Check yourself

  1. The CRM sits behind the gateway and reads the leftmost X-Forwarded-For. Give the fix at the gateway and the fix at the CRM, and say what each fix still assumes about the gateway.
  2. An egress allow-list decides which outside hosts the agent's tools may reach. Is that a forward or a reverse proxy's job, and which connection does it examine?
  3. Why can a reverse proxy that reads each request and reply see a prompt injection in a tool result, when a tunnel opened with CONNECT cannot? What can the tunnel still decide on?
Answers
  1. At the gateway: discard any X-Forwarded-For the client sent and write a fresh one holding the TCP peer's address. At the CRM: read the rightmost entry, the one the gateway added, and only on connections whose peer is the gateway. Both assume the gateway is the only path to the CRM and is itself honest; a compromised gateway, or a second route in, can write anything.
  2. A forward proxy's: it sits on the client side and sees the destination the tool names. It examines the outbound connection from the tool to the outside host (the request's target, or the CONNECT line for HTTPS), not the agent's connection to the gateway.
  3. A reverse proxy terminates the connection and writes its own request, so it holds the plaintext of every reply, tool results included. A CONNECT tunnel only relays ciphertext. It can still decide on the target host and port in the CONNECT line, the TLS server name in the clear, and the addresses, sizes and timing of the traffic.