Skip to content

Commit

Permalink
https: prevent options object from being mutated
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anto Aravinth <[email protected]>
Reviewed-By: David Carlier <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
vighnesh153 authored and Trott committed Jan 4, 2020
1 parent f64842a commit fa94698
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
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-https-agent-create-connection.js
Original file line number Diff line number Diff line change
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 fa94698

Please sign in to comment.