24
24
const {
25
25
Array,
26
26
ArrayIsArray,
27
+ ArrayPrototypeIncludes,
28
+ ArrayPrototypeJoin,
29
+ ArrayPrototypePush,
30
+ ArrayPrototypeReduce,
31
+ ArrayPrototypeSome,
27
32
ObjectDefineProperty,
28
33
ObjectFreeze,
34
+ RegExpPrototypeTest,
29
35
StringFromCharCode,
30
36
StringPrototypeCharCodeAt,
37
+ StringPrototypeEndsWith,
38
+ StringPrototypeIncludes,
31
39
StringPrototypeReplace,
40
+ StringPrototypeSlice,
32
41
StringPrototypeSplit,
42
+ StringPrototypeStartsWith,
33
43
} = primordials ;
34
44
35
45
const {
@@ -107,7 +117,7 @@ ObjectDefineProperty(exports, 'rootCertificates', {
107
117
// ("\x06spdy/2\x08http/1.1\x08http/1.0")
108
118
function convertProtocols ( protocols ) {
109
119
const lens = new Array ( protocols . length ) ;
110
- const buff = Buffer . allocUnsafe ( protocols . reduce ( ( p , c , i ) => {
120
+ const buff = Buffer . allocUnsafe ( ArrayPrototypeReduce ( protocols , ( p , c , i ) => {
111
121
const len = Buffer . byteLength ( c ) ;
112
122
if ( len > 255 ) {
113
123
throw new ERR_OUT_OF_RANGE ( 'The byte length of the protocol at index ' +
@@ -138,7 +148,7 @@ exports.convertALPNProtocols = function convertALPNProtocols(protocols, out) {
138
148
} ;
139
149
140
150
function unfqdn ( host ) {
141
- return host . replace ( / [ . ] $ / , '' ) ;
151
+ return StringPrototypeReplace ( host , / [ . ] $ / , '' ) ;
142
152
}
143
153
144
154
// String#toLowerCase() is locale-sensitive so we use
@@ -165,15 +175,15 @@ function check(hostParts, pattern, wildcards) {
165
175
return false ;
166
176
167
177
// Pattern has empty components, e.g. "bad..example.com".
168
- if ( patternParts . includes ( '' ) )
178
+ if ( ArrayPrototypeIncludes ( patternParts , '' ) )
169
179
return false ;
170
180
171
181
// RFC 6125 allows IDNA U-labels (Unicode) in names but we have no
172
182
// good way to detect their encoding or normalize them so we simply
173
183
// reject them. Control characters and blanks are rejected as well
174
184
// because nothing good can come from accepting them.
175
- const isBad = ( s ) => / [ ^ \u0021 - \u007F ] / u. test ( s ) ;
176
- if ( patternParts . some ( isBad ) )
185
+ const isBad = ( s ) => RegExpPrototypeTest ( / [ ^ \u0021 - \u007F ] / u, s ) ;
186
+ if ( ArrayPrototypeSome ( patternParts , isBad ) )
177
187
return false ;
178
188
179
189
// Check host parts from right to left first.
@@ -184,12 +194,13 @@ function check(hostParts, pattern, wildcards) {
184
194
185
195
const hostSubdomain = hostParts [ 0 ] ;
186
196
const patternSubdomain = patternParts [ 0 ] ;
187
- const patternSubdomainParts = patternSubdomain . split ( '*' ) ;
197
+ const patternSubdomainParts = StringPrototypeSplit ( patternSubdomain , '*' ) ;
188
198
189
199
// Short-circuit when the subdomain does not contain a wildcard.
190
200
// RFC 6125 does not allow wildcard substitution for components
191
201
// containing IDNA A-labels (Punycode) so match those verbatim.
192
- if ( patternSubdomainParts . length === 1 || patternSubdomain . includes ( 'xn--' ) )
202
+ if ( patternSubdomainParts . length === 1 ||
203
+ StringPrototypeIncludes ( patternSubdomain , 'xn--' ) )
193
204
return hostSubdomain === patternSubdomain ;
194
205
195
206
if ( ! wildcards )
@@ -208,10 +219,10 @@ function check(hostParts, pattern, wildcards) {
208
219
if ( prefix . length + suffix . length > hostSubdomain . length )
209
220
return false ;
210
221
211
- if ( ! hostSubdomain . startsWith ( prefix ) )
222
+ if ( ! StringPrototypeStartsWith ( hostSubdomain , prefix ) )
212
223
return false ;
213
224
214
- if ( ! hostSubdomain . endsWith ( suffix ) )
225
+ if ( ! StringPrototypeEndsWith ( hostSubdomain , suffix ) )
215
226
return false ;
216
227
217
228
return true ;
@@ -228,28 +239,30 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) {
228
239
hostname = '' + hostname ;
229
240
230
241
if ( altNames ) {
231
- for ( const name of altNames . split ( ', ' ) ) {
232
- if ( name . startsWith ( 'DNS:' ) ) {
233
- dnsNames . push ( name . slice ( 4 ) ) ;
234
- } else if ( name . startsWith ( 'URI:' ) ) {
242
+ for ( const name of StringPrototypeSplit ( altNames , ', ' ) ) {
243
+ if ( StringPrototypeStartsWith ( name , 'DNS:' ) ) {
244
+ ArrayPrototypePush ( dnsNames , StringPrototypeSlice ( name , 4 ) ) ;
245
+ } else if ( StringPrototypeStartsWith ( name , 'URI:' ) ) {
235
246
let uri ;
236
247
try {
237
- uri = new URL ( name . slice ( 4 ) ) ;
248
+ uri = new URL ( StringPrototypeSlice ( name , 4 ) ) ;
238
249
} catch {
239
- uri = url . parse ( name . slice ( 4 ) ) ;
250
+ const slicedName = StringPrototypeSlice ( name , 4 ) ;
251
+ uri = url . parse ( slicedName ) ;
240
252
if ( ! urlWarningEmitted && ! process . noDeprecation ) {
241
253
urlWarningEmitted = true ;
242
254
process . emitWarning (
243
- `The URI ${ name . slice ( 4 ) } found in cert.subjectaltname ` +
255
+ `The URI ${ slicedName } found in cert.subjectaltname ` +
244
256
'is not a valid URI, and is supported in the tls module ' +
245
257
'solely for compatibility.' ,
246
258
'DeprecationWarning' , 'DEP0109' ) ;
247
259
}
248
260
}
249
261
250
- uriNames . push ( uri . hostname ) ; // TODO(bnoordhuis) Also use scheme.
251
- } else if ( name . startsWith ( 'IP Address:' ) ) {
252
- ips . push ( canonicalizeIP ( name . slice ( 11 ) ) ) ;
262
+ // TODO(bnoordhuis) Also use scheme.
263
+ ArrayPrototypePush ( uriNames , uri . hostname ) ;
264
+ } else if ( StringPrototypeStartsWith ( name , 'IP Address:' ) ) {
265
+ ArrayPrototypePush ( ips , canonicalizeIP ( StringPrototypeSlice ( name , 11 ) ) ) ;
253
266
}
254
267
}
255
268
}
@@ -263,17 +276,19 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) {
263
276
hostname = unfqdn ( hostname ) ; // Remove trailing dot for error messages.
264
277
265
278
if ( net . isIP ( hostname ) ) {
266
- valid = ips . includes ( canonicalizeIP ( hostname ) ) ;
279
+ valid = ArrayPrototypeIncludes ( ips , canonicalizeIP ( hostname ) ) ;
267
280
if ( ! valid )
268
- reason = `IP: ${ hostname } is not in the cert's list: ${ ips . join ( ', ' ) } ` ;
281
+ reason = `IP: ${ hostname } is not in the cert's list: ` +
282
+ ArrayPrototypeJoin ( ips , ', ' ) ;
269
283
// TODO(bnoordhuis) Also check URI SANs that are IP addresses.
270
284
} else if ( hasAltNames || subject ) {
271
285
const hostParts = splitHost ( hostname ) ;
272
286
const wildcard = ( pattern ) => check ( hostParts , pattern , true ) ;
273
287
274
288
if ( hasAltNames ) {
275
289
const noWildcard = ( pattern ) => check ( hostParts , pattern , false ) ;
276
- valid = dnsNames . some ( wildcard ) || uriNames . some ( noWildcard ) ;
290
+ valid = ArrayPrototypeSome ( dnsNames , wildcard ) ||
291
+ ArrayPrototypeSome ( uriNames , noWildcard ) ;
277
292
if ( ! valid )
278
293
reason =
279
294
`Host: ${ hostname } . is not in the cert's altnames: ${ altNames } ` ;
@@ -282,7 +297,7 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) {
282
297
const cn = subject . CN ;
283
298
284
299
if ( ArrayIsArray ( cn ) )
285
- valid = cn . some ( wildcard ) ;
300
+ valid = ArrayPrototypeSome ( cn , wildcard ) ;
286
301
else if ( cn )
287
302
valid = wildcard ( cn ) ;
288
303
0 commit comments