Skip to content
Permalink
Browse files

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: #31119

PR-URL: #31151
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
vighnesh153 authored and targos committed Jan 2, 2020
1 parent dba2ab7 commit 18acaccf0a8abaa7a7366643cb5237c7883a746a
Showing with 27 additions and 1 deletion.
  1. +3 −1 lib/https.js
  2. +24 −0 test/parallel/test-https-agent-create-connection.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') {
@@ -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();
}));
}));
}

0 comments on commit 18acacc

Please sign in to comment.
You can’t perform that action at this time.