File tree Expand file tree Collapse file tree 2 files changed +34
-5
lines changed
Expand file tree Collapse file tree 2 files changed +34
-5
lines changed Original file line number Diff line number Diff line change @@ -83,13 +83,27 @@ export function isUint8Array(value: unknown): value is Uint8Array {
8383 */
8484export function hostMatchesWildcards ( host : string , wildcards : string [ ] ) : boolean {
8585 for ( const wildcard of wildcards ) {
86- if (
87- host === wildcard ||
88- ( wildcard . startsWith ( '*.' ) && host ?. endsWith ( wildcard . substring ( 2 , wildcard . length ) ) ) ||
89- ( wildcard . startsWith ( '*/' ) && host ?. endsWith ( wildcard . substring ( 2 , wildcard . length ) ) )
90- ) {
86+ // Exact match always wins
87+ if ( host === wildcard ) {
9188 return true ;
9289 }
90+
91+ // Wildcard match with leading *.
92+ if ( wildcard . startsWith ( '*.' ) ) {
93+ const suffix = wildcard . substring ( 2 ) ;
94+ // Exact match or strict subdomain match
95+ if ( host === suffix || host . endsWith ( `.${ suffix } ` ) ) {
96+ return true ;
97+ }
98+ }
99+ // Wildcard match with leading */
100+ if ( wildcard . startsWith ( '*/' ) ) {
101+ const suffix = wildcard . substring ( 2 ) ;
102+ // Exact match or strict subpath match
103+ if ( host === suffix || host . endsWith ( `/${ suffix } ` ) ) {
104+ return true ;
105+ }
106+ }
93107 }
94108 return false ;
95109}
Original file line number Diff line number Diff line change @@ -148,6 +148,13 @@ describe('driver utils', function () {
148148 } ) ;
149149 } ) ;
150150
151+ context ( 'when the wildcard starts with *.' , function ( ) {
152+ it ( 'returns false' , function ( ) {
153+ expect ( hostMatchesWildcards ( 'test-mongodb.com' , [ '*.mongodb.com' , 'test2' ] ) ) . to . be
154+ . false ;
155+ } ) ;
156+ } ) ;
157+
151158 context ( 'when the host matches a FQDN' , function ( ) {
152159 it ( 'returns true' , function ( ) {
153160 expect ( hostMatchesWildcards ( 'mongodb.net' , [ '*.mongodb.net' , 'other' ] ) ) . to . be . true ;
@@ -221,6 +228,14 @@ describe('driver utils', function () {
221228 . to . be . false ;
222229 } ) ;
223230 } ) ;
231+
232+ context ( 'when the host does not match partial matches' , function ( ) {
233+ it ( 'returns false' , function ( ) {
234+ expect (
235+ hostMatchesWildcards ( '/tmp/test-mongodb-27017.sock' , [ '*/mongodb-27017.sock' , 'test2' ] )
236+ ) . to . be . false ;
237+ } ) ;
238+ } ) ;
224239 } ) ;
225240 } ) ;
226241
You can’t perform that action at this time.
0 commit comments