Skip to content

Commit c23cca2

Browse files
aduh95danielleadams
authored andcommitted
tls: refactor to avoid unsafe array iteration
PR-URL: #36772 Reviewed-By: Rich Trott <[email protected]>
1 parent 37becfd commit c23cca2

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

lib/_tls_common.js

+13-12
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-
function addCACerts(context, ...certs) {
146-
for (const cert of certs) {
146+
function addCACerts(context, certs) {
147+
ArrayPrototypeForEach(certs, (cert) => {
147148
validateKeyOrCertOption('ca', cert);
148149
context.addCACert(cert);
149-
}
150+
});
150151
}
151152

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

159160
exports.createSecureContext = function createSecureContext(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 (const val of pfx) {
322+
ArrayPrototypeForEach(pfx, (val) => {
322323
const raw = val.buf ? val.buf : val;
323324
const pass = 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
} else if (passphrase) {
331332
c.context.loadPKCS12(toBuf(pfx), toBuf(passphrase));
332333
} else {

lib/internal/tls.js

+3-2
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\[email protected]
1415
function parseCertString(s) {
1516
const out = ObjectCreate(null);
16-
for (const part of StringPrototypeSplit(s, '\n')) {
17+
ArrayPrototypeForEach(StringPrototypeSplit(s, '\n'), (part) => {
1718
const sepIndex = StringPrototypeIndexOf(part, '=');
1819
if (sepIndex > 0) {
1920
const key = StringPrototypeSlice(part, 0, sepIndex);
@@ -27,7 +28,7 @@ function parseCertString(s) {
2728
out[key] = value;
2829
}
2930
}
30-
}
31+
});
3132
return out;
3233
}
3334

lib/tls.js

+7-4
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
return false;
216218

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

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

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

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

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

0 commit comments

Comments
 (0)