-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: prevent slowloris with keepalive connections
Fixes: nodejs-private/security#214 PR-URL: nodejs-private/node-private#158 Reviewed-By: Rod Vagg <[email protected]> Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const http = require('http'); | ||
const net = require('net'); | ||
const { finished } = require('stream'); | ||
|
||
const headers = | ||
'GET / HTTP/1.1\r\n' + | ||
'Host: localhost\r\n' + | ||
'Connection: keep-alive' + | ||
'Agent: node\r\n'; | ||
|
||
let sendCharEvery = 1000; | ||
|
||
const server = http.createServer(common.mustCall((req, res) => { | ||
res.writeHead(200); | ||
res.end(); | ||
})); | ||
|
||
// Pass a REAL env variable to shortening up the default | ||
// value which is 40s otherwise this is useful for manual | ||
// testing | ||
if (!process.env.REAL) { | ||
sendCharEvery = common.platformTimeout(10); | ||
server.headersTimeout = 2 * sendCharEvery; | ||
} | ||
|
||
server.once('timeout', common.mustCall((socket) => { | ||
socket.destroy(); | ||
})); | ||
|
||
server.listen(0, () => { | ||
const client = net.connect(server.address().port); | ||
client.write(headers); | ||
// finish the first request | ||
client.write('\r\n'); | ||
// second request | ||
client.write(headers); | ||
client.write('X-CRASH: '); | ||
|
||
const interval = setInterval(() => { | ||
client.write('a'); | ||
}, sendCharEvery); | ||
|
||
client.resume(); | ||
finished(client, common.mustCall((err) => { | ||
clearInterval(interval); | ||
server.close(); | ||
})); | ||
}); |