@@ -47,9 +47,11 @@ const { isArrayBufferView } = require('internal/util/types');
4747
4848const net = require ( 'net' ) ;
4949const { getOptionValue } = require ( 'internal/options' ) ;
50+ const url = require ( 'url' ) ;
5051const { getRootCertificates, getSSLCiphers } = internalBinding ( 'crypto' ) ;
5152const { Buffer } = require ( 'buffer' ) ;
5253const EventEmitter = require ( 'events' ) ;
54+ const { URL } = require ( 'internal/url' ) ;
5355const DuplexPair = require ( 'internal/streams/duplexpair' ) ;
5456const { canonicalizeIP } = internalBinding ( 'cares_wrap' ) ;
5557const _tls_common = require ( '_tls_common' ) ;
@@ -252,10 +254,12 @@ function splitEscapedAltNames(altNames) {
252254 return result ;
253255}
254256
257+ let urlWarningEmitted = false ;
255258exports . checkServerIdentity = function checkServerIdentity ( hostname , cert ) {
256259 const subject = cert . subject ;
257260 const altNames = cert . subjectaltname ;
258261 const dnsNames = [ ] ;
262+ const uriNames = [ ] ;
259263 const ips = [ ] ;
260264
261265 hostname = '' + hostname ;
@@ -267,6 +271,22 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) {
267271 for ( const name of splitAltNames ) {
268272 if ( name . startsWith ( 'DNS:' ) ) {
269273 dnsNames . push ( name . slice ( 4 ) ) ;
274+ } else if ( process . REVERT_CVE_2021_44531 && name . startsWith ( 'URI:' ) ) {
275+ let uri ;
276+ try {
277+ uri = new URL ( name . slice ( 4 ) ) ;
278+ } catch {
279+ uri = url . parse ( name . slice ( 4 ) ) ;
280+ if ( ! urlWarningEmitted && ! process . noDeprecation ) {
281+ urlWarningEmitted = true ;
282+ process . emitWarning (
283+ `The URI ${ name . slice ( 4 ) } found in cert.subjectaltname ` +
284+ 'is not a valid URI, and is supported in the tls module ' +
285+ 'solely for compatibility.' ,
286+ 'DeprecationWarning' , 'DEP0109' ) ;
287+ }
288+ }
289+ uriNames . push ( uri . hostname ) ; // TODO(bnoordhuis) Also use scheme.
270290 } else if ( name . startsWith ( 'IP Address:' ) ) {
271291 ips . push ( canonicalizeIP ( name . slice ( 11 ) ) ) ;
272292 }
@@ -276,19 +296,25 @@ exports.checkServerIdentity = function checkServerIdentity(hostname, cert) {
276296 let valid = false ;
277297 let reason = 'Unknown reason' ;
278298
299+ const hasAltNames =
300+ dnsNames . length > 0 || ips . length > 0 || uriNames . length > 0 ;
301+
279302 hostname = unfqdn ( hostname ) ; // Remove trailing dot for error messages.
280303
281304 if ( net . isIP ( hostname ) ) {
282305 valid = ips . includes ( canonicalizeIP ( hostname ) ) ;
283306 if ( ! valid )
284307 reason = `IP: ${ hostname } is not in the cert's list: ${ ips . join ( ', ' ) } ` ;
285308 // TODO(bnoordhuis) Also check URI SANs that are IP addresses.
286- } else if ( dnsNames . length > 0 || ( subject && subject . CN ) ) {
309+ } else if ( ( process . REVERT_CVE_2021_44531 && ( hasAltNames || subject ) ) ||
310+ ( dnsNames . length > 0 || ( subject && subject . CN ) ) ) {
287311 const hostParts = splitHost ( hostname ) ;
288312 const wildcard = ( pattern ) => check ( hostParts , pattern , true ) ;
289313
290- if ( dnsNames . length > 0 ) {
291- valid = dnsNames . some ( wildcard ) ;
314+ if ( ( process . REVERT_CVE_2021_44531 && hasAltNames ) ||
315+ ( dnsNames . length > 0 ) ) {
316+ const noWildcard = ( pattern ) => check ( hostParts , pattern , false ) ;
317+ valid = dnsNames . some ( wildcard ) || uriNames . some ( noWildcard ) ;
292318 if ( ! valid )
293319 reason =
294320 `Host: ${ hostname } . is not in the cert's altnames: ${ altNames } ` ;
0 commit comments