Skip to content

Commit c23cca2

Browse files
aduh95danielleadams
authored andcommitted
tls: refactor to avoid unsafe array iteration
PR-URL: #36772 Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 37becfd commit c23cca2

3 files changed

Lines changed: 23 additions & 18 deletions

File tree

‎lib/_tls_common.js‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
const{
2525
ArrayIsArray,
2626
ArrayPrototypeFilter,
27+
ArrayPrototypeForEach,
2728
ArrayPrototypeJoin,
2829
ArrayPrototypePush,
2930
ObjectCreate,
@@ -142,18 +143,18 @@ function processCiphers(ciphers) {
142143
return{ cipherList, cipherSuites };
143144
}
144145

145-
functionaddCACerts(context,...certs){
146-
for(constcertofcerts){
146+
functionaddCACerts(context,certs){
147+
ArrayPrototypeForEach(certs,(cert)=>{
147148
validateKeyOrCertOption('ca',cert);
148149
context.addCACert(cert);
149-
}
150+
});
150151
}
151152

152-
functionsetCerts(context,...certs){
153-
for(constcertofcerts){
153+
functionsetCerts(context,certs){
154+
ArrayPrototypeForEach(certs,(cert)=>{
154155
validateKeyOrCertOption('cert',cert);
155156
context.setCert(cert);
156-
}
157+
});
157158
}
158159

159160
exports.createSecureContext=functioncreateSecureContext(options){
@@ -196,18 +197,18 @@ exports.createSecureContext = function createSecureContext(options) {
196197
// change the checks to !== undefined checks.
197198
if(ca){
198199
if(ArrayIsArray(ca))
199-
addCACerts(c.context, ...ca);
200-
else
201200
addCACerts(c.context,ca);
201+
else
202+
addCACerts(c.context,[ca]);
202203
}else{
203204
c.context.addRootCerts();
204205
}
205206

206207
if(cert){
207208
if(ArrayIsArray(cert))
208-
setCerts(c.context, ...cert);
209-
else
210209
setCerts(c.context,cert);
210+
else
211+
setCerts(c.context,[cert]);
211212
}
212213

213214
// Set the key after the cert.
@@ -318,15 +319,15 @@ exports.createSecureContext = function createSecureContext(options) {
318319

319320
if(pfx!==undefined){
320321
if(ArrayIsArray(pfx)){
321-
for(constvalofpfx){
322+
ArrayPrototypeForEach(pfx,(val)=>{
322323
constraw=val.buf ? val.buf : val;
323324
constpass=val.passphrase||passphrase;
324325
if(pass!==undefined){
325326
c.context.loadPKCS12(toBuf(raw),toBuf(pass));
326327
}else{
327328
c.context.loadPKCS12(toBuf(raw));
328329
}
329-
}
330+
});
330331
}elseif(passphrase){
331332
c.context.loadPKCS12(toBuf(pfx),toBuf(passphrase));
332333
}else{

‎lib/internal/tls.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const{
44
ArrayIsArray,
5+
ArrayPrototypeForEach,
56
ArrayPrototypePush,
67
StringPrototypeIndexOf,
78
StringPrototypeSlice,
@@ -13,7 +14,7 @@ const {
1314
// C=US\nST=CA\nL=SF\nO=Joyent\nOU=Node.js\nCN=ca1\nemailAddress=ry@clouds.org
1415
functionparseCertString(s){
1516
constout=ObjectCreate(null);
16-
for(constpartofStringPrototypeSplit(s,'\n')){
17+
ArrayPrototypeForEach(StringPrototypeSplit(s,'\n'),(part)=>{
1718
constsepIndex=StringPrototypeIndexOf(part,'=');
1819
if(sepIndex>0){
1920
constkey=StringPrototypeSlice(part,0,sepIndex);
@@ -27,7 +28,7 @@ function parseCertString(s) {
2728
out[key]=value;
2829
}
2930
}
30-
}
31+
});
3132
returnout;
3233
}
3334

‎lib/tls.js‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
const{
2525
Array,
2626
ArrayIsArray,
27+
ArrayPrototypeForEach,
2728
ArrayPrototypeIncludes,
2829
ArrayPrototypeJoin,
2930
ArrayPrototypePush,
3031
ArrayPrototypeReduce,
3132
ArrayPrototypeSome,
3233
ObjectDefineProperty,
3334
ObjectFreeze,
35+
ReflectConstruct,
3436
RegExpPrototypeTest,
3537
StringFromCharCode,
3638
StringPrototypeCharCodeAt,
@@ -214,7 +216,7 @@ function check(hostParts, pattern, wildcards) {
214216
if(patternParts.length<=2)
215217
returnfalse;
216218

217-
const[prefix,suffix]=patternSubdomainParts;
219+
const{0: prefix,1: suffix}=patternSubdomainParts;
218220

219221
if(prefix.length+suffix.length>hostSubdomain.length)
220222
returnfalse;
@@ -239,7 +241,8 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) {
239241
hostname=''+hostname;
240242

241243
if(altNames){
242-
for(constnameofStringPrototypeSplit(altNames,', ')){
244+
constsplitAltNames=StringPrototypeSplit(altNames,', ');
245+
ArrayPrototypeForEach(splitAltNames,(name)=>{
243246
if(StringPrototypeStartsWith(name,'DNS:')){
244247
ArrayPrototypePush(dnsNames,StringPrototypeSlice(name,4));
245248
}elseif(StringPrototypeStartsWith(name,'URI:')){
@@ -264,7 +267,7 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) {
264267
}elseif(StringPrototypeStartsWith(name,'IP Address:')){
265268
ArrayPrototypePush(ips,canonicalizeIP(StringPrototypeSlice(name,11)));
266269
}
267-
}
270+
});
268271
}
269272

270273
letvalid=false;
@@ -359,7 +362,7 @@ exports.connect = _tls_wrap.connect;
359362

360363
exports.createSecurePair=internalUtil.deprecate(
361364
functioncreateSecurePair(...args){
362-
returnnewSecurePair(...args);
365+
returnReflectConstruct(SecurePair,args);
363366
},
364367
'tls.createSecurePair() is deprecated. Please use '+
365368
'tls.TLSSocket instead.','DEP0064');

0 commit comments

Comments
 (0)