GeezerVison is showing github.com — tls: refactor to avoid unsafe array iteration · nodejs/node@c23cca2
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/_tls_common.js +13-12Lines changed: 13 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 24 | 24 | const { | 25 | 25 | ArrayIsArray, | 26 | 26 | ArrayPrototypeFilter, | | 27 | + ArrayPrototypeForEach, | 27 | 28 | ArrayPrototypeJoin, | 28 | 29 | ArrayPrototypePush, | 29 | 30 | ObjectCreate, | @@ -142,18 +143,18 @@ function processCiphers(ciphers) { | 142 | 143 | return { cipherList, cipherSuites }; | 143 | 144 | } | 144 | 145 |
| 145 | | -function addCACerts(context, ...certs) { | 146 | | - for (const cert of certs) { | | 146 | +function addCACerts(context, certs) { | | 147 | + ArrayPrototypeForEach(certs, (cert) => { | 147 | 148 | validateKeyOrCertOption('ca', cert); | 148 | 149 | context.addCACert(cert); | 149 | | - } | | 150 | + }); | 150 | 151 | } | 151 | 152 |
| 152 | | -function setCerts(context, ...certs) { | 153 | | - for (const cert of certs) { | | 153 | +function setCerts(context, certs) { | | 154 | + ArrayPrototypeForEach(certs, (cert) => { | 154 | 155 | validateKeyOrCertOption('cert', cert); | 155 | 156 | context.setCert(cert); | 156 | | - } | | 157 | + }); | 157 | 158 | } | 158 | 159 |
| 159 | 160 | exports.createSecureContext = function createSecureContext(options) { | @@ -196,18 +197,18 @@ exports.createSecureContext = function createSecureContext(options) { | 196 | 197 | // change the checks to !== undefined checks. | 197 | 198 | if (ca) { | 198 | 199 | if (ArrayIsArray(ca)) | 199 | | - addCACerts(c.context, ...ca); | 200 | | - else | 201 | 200 | addCACerts(c.context, ca); | | 201 | + else | | 202 | + addCACerts(c.context, [ca]); | 202 | 203 | } else { | 203 | 204 | c.context.addRootCerts(); | 204 | 205 | } | 205 | 206 |
| 206 | 207 | if (cert) { | 207 | 208 | if (ArrayIsArray(cert)) | 208 | | - setCerts(c.context, ...cert); | 209 | | - else | 210 | 209 | setCerts(c.context, cert); | | 210 | + else | | 211 | + setCerts(c.context, [cert]); | 211 | 212 | } | 212 | 213 |
| 213 | 214 | // Set the key after the cert. | @@ -318,15 +319,15 @@ exports.createSecureContext = function createSecureContext(options) { | 318 | 319 |
| 319 | 320 | if (pfx !== undefined) { | 320 | 321 | if (ArrayIsArray(pfx)) { | 321 | | - for (const val of pfx) { | | 322 | + ArrayPrototypeForEach(pfx, (val) => { | 322 | 323 | const raw = val.buf ? val.buf : val; | 323 | 324 | const pass = val.passphrase || passphrase; | 324 | 325 | if (pass !== undefined) { | 325 | 326 | c.context.loadPKCS12(toBuf(raw), toBuf(pass)); | 326 | 327 | } else { | 327 | 328 | c.context.loadPKCS12(toBuf(raw)); | 328 | 329 | } | 329 | | - } | | 330 | + }); | 330 | 331 | } else if (passphrase) { | 331 | 332 | c.context.loadPKCS12(toBuf(pfx), toBuf(passphrase)); | 332 | 333 | } else { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/tls.js +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | const { | 4 | 4 | ArrayIsArray, | | 5 | + ArrayPrototypeForEach, | 5 | 6 | ArrayPrototypePush, | 6 | 7 | StringPrototypeIndexOf, | 7 | 8 | StringPrototypeSlice, | @@ -13,7 +14,7 @@ const { | 13 | 14 | | 14 | 15 | function parseCertString(s) { | 15 | 16 | const out = ObjectCreate(null); | 16 | | - for (const part of StringPrototypeSplit(s, '\n')) { | | 17 | + ArrayPrototypeForEach(StringPrototypeSplit(s, '\n'), (part) => { | 17 | 18 | const sepIndex = StringPrototypeIndexOf(part, '='); | 18 | 19 | if (sepIndex > 0) { | 19 | 20 | const key = StringPrototypeSlice(part, 0, sepIndex); | @@ -27,7 +28,7 @@ function parseCertString(s) { | 27 | 28 | out[key] = value; | 28 | 29 | } | 29 | 30 | } | 30 | | - } | | 31 | + }); | 31 | 32 | return out; | 32 | 33 | } | 33 | 34 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/tls.js +7-4Lines changed: 7 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 24 | 24 | const { | 25 | 25 | Array, | 26 | 26 | ArrayIsArray, | | 27 | + ArrayPrototypeForEach, | 27 | 28 | ArrayPrototypeIncludes, | 28 | 29 | ArrayPrototypeJoin, | 29 | 30 | ArrayPrototypePush, | 30 | 31 | ArrayPrototypeReduce, | 31 | 32 | ArrayPrototypeSome, | 32 | 33 | ObjectDefineProperty, | 33 | 34 | ObjectFreeze, | | 35 | + ReflectConstruct, | 34 | 36 | RegExpPrototypeTest, | 35 | 37 | StringFromCharCode, | 36 | 38 | StringPrototypeCharCodeAt, | @@ -214,7 +216,7 @@ function check(hostParts, pattern, wildcards) { | 214 | 216 | if (patternParts.length <= 2) | 215 | 217 | return false; | 216 | 218 |
| 217 | | - const [prefix, suffix] = patternSubdomainParts; | | 219 | + const { 0: prefix, 1: suffix } = patternSubdomainParts; | 218 | 220 |
| 219 | 221 | if (prefix.length + suffix.length > hostSubdomain.length) | 220 | 222 | return false; | @@ -239,7 +241,8 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) { | 239 | 241 | hostname = '' + hostname; | 240 | 242 |
| 241 | 243 | if (altNames) { | 242 | | - for (const name of StringPrototypeSplit(altNames, ', ')) { | | 244 | + const splitAltNames = StringPrototypeSplit(altNames, ', '); | | 245 | + ArrayPrototypeForEach(splitAltNames, (name) => { | 243 | 246 | if (StringPrototypeStartsWith(name, 'DNS:')) { | 244 | 247 | ArrayPrototypePush(dnsNames, StringPrototypeSlice(name, 4)); | 245 | 248 | } else if (StringPrototypeStartsWith(name, 'URI:')) { | @@ -264,7 +267,7 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) { | 264 | 267 | } else if (StringPrototypeStartsWith(name, 'IP Address:')) { | 265 | 268 | ArrayPrototypePush(ips, canonicalizeIP(StringPrototypeSlice(name, 11))); | 266 | 269 | } | 267 | | - } | | 270 | + }); | 268 | 271 | } | 269 | 272 |
| 270 | 273 | let valid = false; | @@ -359,7 +362,7 @@ exports.connect = _tls_wrap.connect; | 359 | 362 |
| 360 | 363 | exports.createSecurePair = internalUtil.deprecate( | 361 | 364 | function createSecurePair(...args) { | 362 | | - return new SecurePair(...args); | | 365 | + return ReflectConstruct(SecurePair, args); | 363 | 366 | }, | 364 | 367 | 'tls.createSecurePair() is deprecated. Please use ' + | 365 | 368 | 'tls.TLSSocket instead.', 'DEP0064'); |
|