Back to results for “Cloud & DevOps”
Node.js › HTTP & Web Servers

How would you implement HTTP request timeout and connection management on the server side in Node.js to protect against slow-loris style attacks?

Advanced

Answer

A slow-loris attack sends HTTP requests very slowly (a few bytes at a time) to exhaust a server's available connections, since each connection stays open waiting for the request to complete -- Node's http.Server exposes several relevant timeout settings (headersTimeout, requestTimeout, and the server-level timeout for the overall connection) that can be configured to forcibly close connections that take too long, protecting against both malicious slow attacks and simply misbehaving or extremely slow legitimate clients.
const server = http.createServer(app);
server.headersTimeout = 60000;  // max time to receive complete headers
server.requestTimeout = 300000; // max time for the entire request
server.timeout = 120000;        // socket inactivity timeout
server.listen(3000);
Real-world example A public-facing API configures explicit headersTimeout and requestTimeout values on its underlying HTTP server, closing connections that take unreasonably long to send their headers or complete their request body, specifically hardening the server against slow-loris-style resource-exhaustion attacks.

Common follow-ups: What's an appropriate timeout value that protects against attacks without inadvertently disconnecting legitimate slow clients (like those on poor mobile connections)?;How does a reverse proxy like Nginx sitting in front of Node.js provide an additional layer of protection against this same attack pattern?