Skip to content

Commit 06bcd1a

Browse files
vighnesh153BethGriggs
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 2c85dd9 commit 06bcd1a

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

‎lib/https.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,11 @@ function createConnection(port, host, options){
9696
if(port!==null&&typeofport==='object'){
9797
options=port;
9898
}elseif(host!==null&&typeofhost==='object'){
99-
options=host;
99+
options={ ...host};
100100
}elseif(options===null||typeofoptions!=='object'){
101101
options={};
102+
}else{
103+
options={ ...options};
102104
}
103105

104106
if(typeofport==='number'){

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

Lines changed: 24 additions & 0 deletions
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+
constserver=createServer();
139+
server.listen(0,common.mustCall(()=>{
140+
constport=server.address().port;
141+
consthost='localhost';
142+
constoptions={
143+
port: 3000,
144+
rejectUnauthorized: false
145+
};
146+
147+
constsocket=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)