Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 BethGriggs committed Feb 6, 2020
1 parent 2c85dd9 commit 06bcd1a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/https.js
Expand Up @@ -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') {
Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-https-agent-create-connection.js
Expand Up @@ -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 06bcd1a

Please sign in to comment.