@@ -4,6 +4,7 @@ import { ObjectId } from 'bson';
44import { expect } from 'chai' ;
55import * as sinon from 'sinon' ;
66
7+ import { DEFAULT_ALLOWED_HOSTS } from '../../src/cmap/auth/mongo_credentials' ;
78import { LEGACY_HELLO_COMMAND } from '../../src/constants' ;
89import { MongoInvalidArgumentError , MongoRuntimeError } from '../../src/error' ;
910import { decorateWithExplain , Explain } from '../../src/explain' ;
@@ -148,6 +149,26 @@ describe('driver utils', function () {
148149 } ) ;
149150 } ) ;
150151
152+ context ( 'when the wildcard starts with *.' , function ( ) {
153+ it ( 'returns false' , function ( ) {
154+ expect ( hostMatchesWildcards ( 'test-mongodb.com' , [ '*.mongodb.com' , 'test2' ] ) ) . to . be
155+ . false ;
156+ } ) ;
157+ } ) ;
158+
159+ context ( 'when using default allowed hosts' , function ( ) {
160+ it ( 'returns false' , function ( ) {
161+ for ( const host of DEFAULT_ALLOWED_HOSTS ) {
162+ // Only test the wildcard hosts, the non-wildcard hosts are tested in other test cases
163+ if ( ! host . startsWith ( '*.' ) ) {
164+ continue ;
165+ }
166+ const wrongHost = host . replace ( '*.' , 'test-' ) ;
167+ expect ( hostMatchesWildcards ( wrongHost , DEFAULT_ALLOWED_HOSTS ) ) . to . be . false ;
168+ }
169+ } ) ;
170+ } ) ;
171+
151172 context ( 'when the host matches a FQDN' , function ( ) {
152173 it ( 'returns true' , function ( ) {
153174 expect ( hostMatchesWildcards ( 'mongodb.net' , [ '*.mongodb.net' , 'other' ] ) ) . to . be . true ;
@@ -221,6 +242,14 @@ describe('driver utils', function () {
221242 . to . be . false ;
222243 } ) ;
223244 } ) ;
245+
246+ context ( 'when the host does not match partial matches' , function ( ) {
247+ it ( 'returns false' , function ( ) {
248+ expect (
249+ hostMatchesWildcards ( '/tmp/test-mongodb-27017.sock' , [ '*/mongodb-27017.sock' , 'test2' ] )
250+ ) . to . be . false ;
251+ } ) ;
252+ } ) ;
224253 } ) ;
225254 } ) ;
226255
0 commit comments