diff --git a/lib/https.js b/lib/https.js index 4b5fbea0b3ba18..48f9b42dc95b7e 100644 --- a/lib/https.js +++ b/lib/https.js @@ -96,9 +96,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(); + })); + })); +}