Skip to content

Commit 18acacc

Browse files
vighnesh153targos
authored andcommitted
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 <[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]>
1 parent dba2ab7 commit 18acacc

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/https.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,11 @@ function createConnection(port, host, options) {
9595
if (port !== null && typeof port === 'object') {
9696
options = port;
9797
} else if (host !== null && typeof host === 'object') {
98-
options = host;
98+
options = { ...host };
9999
} else if (options === null || typeof options !== 'object') {
100100
options = {};
101+
} else {
102+
options = { ...options };
101103
}
102104

103105
if (typeof port === 'number') {

test/parallel/test-https-agent-create-connection.js

+24
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,27 @@ function createServer() {
132132
}));
133133
}));
134134
}
135+
136+
// `options` should not be modified
137+
{
138+
const server = createServer();
139+
server.listen(0, common.mustCall(() => {
140+
const port = server.address().port;
141+
const host = 'localhost';
142+
const options = {
143+
port: 3000,
144+
rejectUnauthorized: false
145+
};
146+
147+
const socket = agent.createConnection(port, host, options);
148+
socket.on('connect', common.mustCall((data) => {
149+
socket.end();
150+
}));
151+
socket.on('end', common.mustCall(() => {
152+
assert.deepStrictEqual(options, {
153+
port: 3000, rejectUnauthorized: false
154+
});
155+
server.close();
156+
}));
157+
}));
158+
}

0 commit comments

Comments
 (0)