Connect middleware that helps maintain a high quality of service during heavy traffic. The basic idea is to identify bad actors and not penalize legitimate traffic more than necessary until proper mitigation can be activated.
While this library provides some basic HTTP (Layer 7) flood attack protection, it does NOT remove the need for proper multi-layered DDoS defenses.
It's recommended to monitor for 5xx errors and alarm if threshold exceeded -- otherwise you may face an attack and not know about it.
Using Connect or Express?
const
connect = require("connect"),
http = require("http");
const { ConnectQOS } = require("connect-qos");
var app = connect()
.use(new ConnectQOS().getMiddleware())
.use(function(req, res) {
res.end("Hello World!");
});
http.createServer(app).listen(8392);
Real coders don't use middleware? We've got you covered too...
const http = require("http");
const { ConnectQOS } = require("connect-qos");
var qos = new ConnectQOS();
var qosMiddleware = qos.getMiddleware();
http.createServer(function(req, res) {
qosMiddleware(req, res, function() {
res.end("Hello World!");
});
}).listen(8392);
beforeThrottle(qosInstance, req, reason)- If a function is provided it will be invoked prior to throttling a request in case a decision is desired. Only if the function explicitly returnsfalsewill the throttle request be denied, not resulting in a503status.destroySocket(default:true) - If denying bad actor also destroy the socket to prevent reuse.
Users may also invoke methods isBadHost(host), isBadIp(ip), or isBadSubnet(subnet) on the qos instance to check the status of a given host, IP, or subnet. These methods will return true or false indicating whether the actor is currently considered to be a bad actor. This can be done for TLS/SNI to provide additional layer 5 mitigations.
Subnet keys match the format produced by resolveSubnetFromIp — the full 4-octet network address with host bits zeroed (e.g. 103.142.223.0 for a /24). IPv4-mapped IPv6 (::ffff:a.b.c.d) is unwrapped to IPv4 first; pure IPv6 is used as-is.
- Identify potential bad actors
- Respond with 503 (BUSY) during heavy traffic for bad actors only
- Very light weight, no complex algorithms
For you tweakers out there, here's some levers to pull:
- minLag (default:
70) - Lag time in milliseconds before throttling kicks in. Default should typically suffice unless you support cpu-intensive operations. - maxLag (default:
300) - The highest lag threshold which will block the greatest amount of traffic determined bymaxBadHostThresholdormaxBadIpThreshold. - minHostRate (default:
20) - Minimum rate if lag is >= maxLag. Disable rate limiting by setting to0. - maxHostRate (default:
40) - Maximum rate if lag is <= minLag. - maxHostRatio (default:
0) - If a given host receives the specified threshold (0.1= 10%) ahostViolationwill be returned. This prevents a single host from accounting for excessive traffic and is an effective method for combating very large attacks. - minIpRate (default:
0) - Minimum rate if lag is >= maxLag. Disable rate limiting by setting to0. - maxIpRate (default:
0) - Maximum rate if lag is <= minLag. - maxIpRateHostViolation (default:
0) - Maximum rate if target host is currently exceeding the configuredmaxHostRatio. This can be used to increase IP throttling if a particular host is being targeted by a large number of IPs. Requests hitting this max rate will receivehostViolationwhile requests below the rate threshold but hitting the target host will not be flagged. - subnetMaskBits (default:
24, allowed:20–30) - CIDR prefix length used to derive subnet keys from IP addresses./24groups up to 256 IPs;/20groups up to 4,096./30is the finest granularity (4 IPs per group). Useful for catching distributed attacks spread across many IPs in the same subnet. - minSubnetRate (default:
0) - Minimum subnet request rate (req/s) before a subnet can be flagged. Setting to0disables subnet tracking entirely. - maxSubnetRate (default:
0) - Maximum subnet rate (req/s) if lag is <= minLag. Disable by setting to0. - maxSubnetRateHostViolation (default:
0) - Maximum subnet rate (req/s) when the target host is exceedingmaxHostRatio. When set, only subnets exceeding this rate against a violated host receivehostViolation; subnets below the threshold are not flagged. Disable by setting to0. - subnetWhitelist
Set<string>([])- Subnet keys that are never flagged as bad actors. Key format matches the derived subnet key (e.g.103.142.223.0for a/24). - errorStatusCode (default:
503) - The HTTP status code to return if the request has been throttled. - errorResponseDelay (default:
0) - Number of milliseconds to delay sending an error response to bad actors. A value of 0 will result in the response being sent synchronously before returning from the middleware. - historySize (default:
200) - The LRU history size to use in tracking bad actors. Hosts and IPs both get their own dedicated LRU. - maxAge (default:
10000) - Time (in ms) before history is purged. 10 seconds is generally more than adequate to capture an accurate hit rate. - hostWhitelist
Set<string>(['localhost'])- If provided will never flag hosts as bad actors. - ipWhitelist
Set<string>([])- If provided will never flag IPs as bad actors. - httpBehindProxy (default:
false) -x-forwarded-forheader only supported if this option is set totrue. - httpsBehindProxy (default:
false) -x-forwarded-forheader only supported if this option is set totrue.
With quality of service being the entire purpose of this library needless to say
performance is a critical influence in every decision. You can expect connect-qos
to never be a bottleneck of any kind. Local tests on modest laptop easily
exceeds 3.5K req/sec on a hello world http server. See for yourself with
npm run bench.
