HTTP/1 connection held open indefinitely by a partial second request on a keep-alive connection
Description
Affected: actix-http 3.13.6 (latest) and main; also seen on 3.12.1. Default features, plain TCP, no proxy.
Summary
After a connection finishes one request, a client that sends a single byte of a second request is never timed out. The keep-alive timer is cleared as soon as that byte arrives, and the client request (head) timer only applies to the first request on a connection. No timer governs the connection after that, so it stays open until the client closes it.
Impact
An unauthenticated client can hold one connection (socket, fd, dispatcher task) per byte sent. About 1000 such connections exhaust a default 1024 fd limit, and the server stops accepting new connections until the attacker disconnects. Unlike a classic slowloris, the attacker sends no further traffic after the first byte.
Root cause (actix-http/src/h1/dispatcher.rs, 3.13.6)
- Lines 1329-1333: any read on a keep-alive connection clears ka_timer and the KEEP_ALIVE flag:
// after reading something from stream, clear keep-alive timer
if !inner.read_buf.is_empty() && inner.flags.contains(Flags::KEEP_ALIVE) {
inner.flags.remove(Flags::KEEP_ALIVE);
inner.ka_timer.clear(line!());
}- Lines 1335-1344: head_timer is armed only while STARTED is unset, which happens once per connection.
- Line 905: head_timer is cleared when the first request head decodes ("head timer only applies to first request on connection").
After request 1, one partial byte triggers (1). Because of (2) and (3), head_timer is never armed again.
Reproduction
// actix-web 4.15.0
use actix_web::{App, HttpServer, get};
use std::time::Duration;
#[get("/")]
async fn index() -> &'static str { "ok" }
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().service(index))
.keep_alive(Duration::from_secs(5))
.client_request_timeout(Duration::from_secs(5))
.workers(1)
.bind(("127.0.0.1", 18181))?
.run()
.await
} import socket, time
def probe(second_request_prefix):
s = socket.create_connection(("127.0.0.1", 18181))
s.sendall(b"GET / HTTP/1.1\r\nHost: x\r\n\r\n")
s.recv(4096)
if second_request_prefix:
s.sendall(second_request_prefix)
s.settimeout(30)
start = time.monotonic()
try:
return time.monotonic() - start if s.recv(4096) == b"" else -1
except socket.timeout:
return float("inf")
print("idle keep-alive, closed after: %.1fs" % probe(b""))
print("one byte of request 2, closed after: %.1fs" % probe(b"G"))Output:
idle keep-alive, closed after: 4.6s
one byte of request 2, closed after: infsSuggested fix
When bytes arrive on an idle keep-alive connection (the block at lines 1329-1333), arm head_timer with client_request_deadline(), so every request head gets the same deadline as the first one. The existing clear at line 905 then disarms it once the head decodes.
Workaround HttpServer::keep_alive(KeepAlive::Disabled).