Skip to content

Commit 3fe37e6

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 f281946 commit 3fe37e6

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
@@ -96,9 +96,11 @@ function createConnection(port, host, options) {
9696
if (port !== null && typeof port === 'object') {
9797
options = port;
9898
} else if (host !== null && typeof host === 'object') {
99-
options = host;
99+
options = { ...host };
100100
} else if (options === null || typeof options !== 'object') {
101101
options = {};
102+
} else {
103+
options = { ...options };
102104
}
103105

104106
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)