From 907941d48e48a30b40a2ee73da4ccd974019fb6e Mon Sep 17 00:00:00 2001 From: cjihrig Date: Wed, 20 Feb 2019 10:02:46 -0500 Subject: [PATCH] http: validate timeout in ClientRequest() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Validate the timeout option in the ClientRequest() constructor to prevent asynchronously thrown validation errors. PR-URL: https://github.com/nodejs/node/pull/26214 Fixes: https://github.com/nodejs/node/issues/26143 Reviewed-By: James M Snell Reviewed-By: Wyatt Preul Reviewed-By: Ali Ijaz Sheikh Reviewed-By: Michaƫl Zasso --- lib/_http_client.js | 4 +++- test/parallel/test-http-client-timeout-option.js | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/_http_client.js b/lib/_http_client.js index e073e973bbf205..de7b01cbd1b9bd 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -140,7 +140,9 @@ function ClientRequest(input, options, cb) { var setHost = (options.setHost === undefined || Boolean(options.setHost)); this.socketPath = options.socketPath; - this.timeout = options.timeout; + + if (options.timeout !== undefined) + this.timeout = validateTimerDuration(options.timeout, 'timeout'); var method = options.method; var methodIsString = (typeof method === 'string'); diff --git a/test/parallel/test-http-client-timeout-option.js b/test/parallel/test-http-client-timeout-option.js index 6741fbe0bd1e35..6351a2ca4a3ae1 100644 --- a/test/parallel/test-http-client-timeout-option.js +++ b/test/parallel/test-http-client-timeout-option.js @@ -3,6 +3,10 @@ const common = require('../common'); const assert = require('assert'); const http = require('http'); +assert.throws(() => { + http.request({ timeout: null }); +}, /The "timeout" argument must be of type number/); + const options = { method: 'GET', port: undefined,