From 9296cddde67baea3b7088bd9a32bb22b7d9d0472 Mon Sep 17 00:00:00 2001 From: Vighnesh Raut Date: Thu, 2 Jan 2020 08:42:19 +0530 Subject: [PATCH] https: prevent options object from being mutated Previously, when passing options object to the agent.createConnection method, the same options object got modified within the method. Now, any modification will happen on only a copy of the object. Fixes: https://github.com/nodejs/node/issues/31119 --- lib/https.js | 4 +++- .../test-https-agent-create-connection.js | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/https.js b/lib/https.js index d1c2a88411e3c8..77d3a241eb5946 100644 --- a/lib/https.js +++ b/lib/https.js @@ -95,9 +95,11 @@ function createConnection(port, host, options) { if (port !== null && typeof port === 'object') { options = port; } else if (host !== null && typeof host === 'object') { - options = host; + options = { ...host }; } else if (options === null || typeof options !== 'object') { options = {}; + } else { + options = { ...options }; } if (typeof port === 'number') { diff --git a/test/parallel/test-https-agent-create-connection.js b/test/parallel/test-https-agent-create-connection.js index 1bb3da5f1e1501..d4840298aa6e08 100644 --- a/test/parallel/test-https-agent-create-connection.js +++ b/test/parallel/test-https-agent-create-connection.js @@ -132,3 +132,27 @@ function createServer() { })); })); } + +// `options` should not be modified +{ + const server = createServer(); + server.listen(0, common.mustCall(() => { + const port = server.address().port; + const host = 'localhost'; + const options = { + port: 3000, + rejectUnauthorized: false + }; + + const socket = agent.createConnection(port, host, options); + socket.on('connect', common.mustCall((data) => { + socket.end(); + })); + socket.on('end', common.mustCall(() => { + assert.deepStrictEqual(options, { + port: 3000, rejectUnauthorized: false + }); + server.close(); + })); + })); +}