diff options
author | Matteo Collina <hello@matteocollina.com> | 2020-05-14 20:21:34 +0200 |
---|---|---|
committer | Richard Lau <riclau@uk.ibm.com> | 2020-09-15 15:39:54 -0400 |
commit | df08d527c2083b852d8456b88b39114f30525236 (patch) | |
tree | 3957f9865debe958271ab09a941303dc90225f70 /test/parallel/test-http-server-request-timeout-upgrade.js | |
parent | cb90248c145763502ee8fae67960d45293c9e0bf (diff) | |
download | node-new-df08d527c2083b852d8456b88b39114f30525236.tar.gz |
http: add requestTimeout
This commits introduces a new http.Server option called requestTimeout
with a default value in milliseconds of 0.
If requestTimeout is set to a positive value, the server will start a new
timer set to expire in requestTimeout milliseconds when a new connection
is established. The timer is also set again if new requests after the
first are received on the socket (this handles pipelining and keep-alive
cases).
The timer is cancelled when:
1. the request body is completely received by the server.
2. the response is completed. This handles the case where the
application responds to the client without consuming the request body.
3. the connection is upgraded, like in the WebSocket case.
If the timer expires, then the server responds with status code 408 and
closes the connection.
CVE-2020-8251
PR-URL: https://github.com/nodejs-private/node-private/pull/208
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Mary Marchini <oss@mmarchini.me>
Co-Authored-By: Paolo Insogna <paolo@cowtech.it>
Co-Authored-By: Robert Nagy <ronagy@icloud.com>
Diffstat (limited to 'test/parallel/test-http-server-request-timeout-upgrade.js')
-rw-r--r-- | test/parallel/test-http-server-request-timeout-upgrade.js | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/test/parallel/test-http-server-request-timeout-upgrade.js b/test/parallel/test-http-server-request-timeout-upgrade.js new file mode 100644 index 0000000000..dd7269621e --- /dev/null +++ b/test/parallel/test-http-server-request-timeout-upgrade.js @@ -0,0 +1,55 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const { createServer } = require('http'); +const { connect } = require('net'); + +// This test validates that the requestTimeoout +// is disabled after the connection is upgraded. + +const server = createServer(common.mustNotCall()); + +// 0 seconds is the default +assert.strictEqual(server.requestTimeout, 0); +const requestTimeout = common.platformTimeout(1000); +server.requestTimeout = requestTimeout; +assert.strictEqual(server.requestTimeout, requestTimeout); + +server.on('upgrade', common.mustCall((req, socket, head) => { + socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n'); + socket.write('Upgrade: WebSocket\r\n'); + socket.write('Connection: Upgrade\r\n\r\n'); + socket.pipe(socket); +})); + +server.listen(0, common.mustCall(() => { + const client = connect(server.address().port); + let response = ''; + + client.on('data', common.mustCallAtLeast((chunk) => { + response += chunk.toString('utf-8'); + }, 1)); + + client.on('end', common.mustCall(() => { + assert.strictEqual( + response, + 'HTTP/1.1 101 Web Socket Protocol Handshake\r\n' + + 'Upgrade: WebSocket\r\n' + + 'Connection: Upgrade\r\n\r\n' + + '12345678901234567890' + ); + + server.close(); + })); + + client.resume(); + client.write('GET / HTTP/1.1\r\n'); + client.write('Upgrade: WebSocket\r\n'); + client.write('Connection: Upgrade\r\n\r\n'); + + setTimeout(() => { + client.write('12345678901234567890'); + client.end(); + }, common.platformTimeout(2000)).unref(); +})); |