@@ -161,13 +161,82 @@ describe('mongodb-redact', function () {
161161 expect ( res ) . to . equal ( '<url>' ) ;
162162 } ) ;
163163
164- it ( 'should redact MongoDB connection URIs' , function ( ) {
165- let res = redact (
166- 'mongodb://db1.example.net,db2.example.net:2500/?replicaSet=test&connectTimeoutMS=300000' ,
167- ) ;
168- expect ( res ) . to . equal ( '<mongodb uri>' ) ;
169- res = redact ( 'mongodb://localhost,localhost:27018,localhost:27019' ) ;
170- expect ( res ) . to . equal ( '<mongodb uri>' ) ;
164+ describe ( 'MongoDB connection strings' , function ( ) {
165+ it ( 'should redact MongoDB connection URIs' , function ( ) {
166+ let res = redact (
167+ 'mongodb://db1.example.net,db2.example.net:2500/?replicaSet=test&connectTimeoutMS=300000' ,
168+ ) ;
169+ expect ( res ) . to . equal ( '<mongodb uri>' ) ;
170+ res = redact ( 'mongodb://localhost,localhost:27018,localhost:27019' ) ;
171+ expect ( res ) . to . equal ( '<mongodb uri>' ) ;
172+ } ) ;
173+
174+ it ( 'should redact MongoDB URIs with credentials' , function ( ) {
175+ let res = redact ( 'mongodb://user:password@localhost:27017/admin' ) ;
176+ expect ( res ) . to . equal ( '<mongodb uri>' ) ;
177+ res = redact ( 'mongodb://admin:[email protected] /mydb' ) ; 178+ expect ( res ) . to . equal ( '<mongodb uri>' ) ;
179+ } ) ;
180+
181+ it ( 'should redact MongoDB URIs with special characters in usernames and passwords' , function ( ) {
182+ let res = redact ( 'mongodb://user:p%40ss!word@localhost:27017/' ) ;
183+ expect ( res ) . to . equal ( '<mongodb uri>' ) ;
184+ res = redact ( 'mongodb://ad!min:te%st#[email protected] :27017/' ) ; 185+ expect ( res ) . to . equal ( '<mongodb uri>' ) ;
186+ res = redact ( 'mongodb://!user:my%20pass@localhost/mydb' ) ;
187+ expect ( res ) . to . equal ( '<mongodb uri>' ) ;
188+ res = redact (
189+ 'mongodb://user:p&ssw!rd#[email protected] :27017/db?authSource=admin' , 190+ ) ;
191+ expect ( res ) . to . equal ( '<mongodb uri>' ) ;
192+ } ) ;
193+
194+ it ( 'should redact MongoDB SRV URIs' , function ( ) {
195+ let res = redact (
196+ 'mongodb+srv://user:[email protected] /test' , 197+ ) ;
198+ expect ( res ) . to . equal ( '<mongodb uri>' ) ;
199+ res = redact (
200+ 'mongodb+srv://admin:[email protected] /mydb?retryWrites=true' , 201+ ) ;
202+ expect ( res ) . to . equal ( '<mongodb uri>' ) ;
203+ } ) ;
204+
205+ it ( 'should redact MongoDB URIs with query parameters' , function ( ) {
206+ let res = redact (
207+ 'mongodb://localhost:27017/mydb?ssl=true&replicaSet=rs0' ,
208+ ) ;
209+ expect ( res ) . to . equal ( '<mongodb uri>' ) ;
210+ res = redact (
211+ 'mongodb://user:[email protected] /db?authSource=admin&readPreference=primary' , 212+ ) ;
213+ expect ( res ) . to . equal ( '<mongodb uri>' ) ;
214+ } ) ;
215+
216+ it ( 'should redact MongoDB URIs with replica sets' , function ( ) {
217+ let res = redact (
218+ 'mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myReplSet' ,
219+ ) ;
220+ expect ( res ) . to . equal ( '<mongodb uri>' ) ;
221+ res = redact ( 'mongodb://user:pass@host1,host2,host3/db?replicaSet=rs0' ) ;
222+ expect ( res ) . to . equal ( '<mongodb uri>' ) ;
223+ } ) ;
224+
225+ it ( 'should redact MongoDB URIs with IP addresses' , function ( ) {
226+ let res = redact ( 'mongodb://192.168.1.100:27017/mydb' ) ;
227+ expect ( res ) . to . equal ( '<mongodb uri>' ) ;
228+ res = redact ( 'mongodb://user:[email protected] :27017/admin' ) ; 229+ expect ( res ) . to . equal ( '<mongodb uri>' ) ;
230+ } ) ;
231+
232+ it ( 'should redact simple MongoDB URIs' , function ( ) {
233+ let res = redact ( 'mongodb://localhost' ) ;
234+ expect ( res ) . to . equal ( '<mongodb uri>' ) ;
235+ res = redact ( 'mongodb://localhost:27017' ) ;
236+ expect ( res ) . to . equal ( '<mongodb uri>' ) ;
237+ res = redact ( 'mongodb://localhost/mydb' ) ;
238+ expect ( res ) . to . equal ( '<mongodb uri>' ) ;
239+ } ) ;
171240 } ) ;
172241
173242 it ( 'should redact general linux/unix user paths' , function ( ) {
0 commit comments