diff --git a/lib/MongoDB/_URI.pm b/lib/MongoDB/_URI.pm index 7d845fef..6815f2e0 100644 --- a/lib/MongoDB/_URI.pm +++ b/lib/MongoDB/_URI.pm @@ -655,7 +655,7 @@ sub __userinfo_invalid_chars { # redact user credentials when stringifying use overload '""' => sub { - (my $s = $_[0]->uri) =~ s{^(\w+)://[^/]+\@}{$1://[**REDACTED**]\@}; + (my $s = $_[0]->uri) =~ s{^([^:]+)://[^/]+\@}{$1://[**REDACTED**]\@}; return $s }, 'fallback' => 1; diff --git a/t/unit/uri.t b/t/unit/uri.t index b584eb4b..9681e83a 100644 --- a/t/unit/uri.t +++ b/t/unit/uri.t @@ -279,4 +279,21 @@ subtest "auth credentials" => sub { ); }; +subtest "stringification" => sub { + # For srv uri parsing and tests, see uri_srv.t + my $uri; + + $uri = new_ok( $class, [ uri => 'mongodb://fred:foobar@localhost' ] ); + is( "$uri", 'mongodb://[**REDACTED**]@localhost', "stringify correct" ); + + $uri = new_ok( $class, [ uri => 'mongodb://:@localhost' ] ); + is( "$uri", 'mongodb://[**REDACTED**]@localhost', "stringify correct" ); + + $uri = new_ok( $class, [ uri => 'mongodb://@localhost' ] ); + is( "$uri", 'mongodb://@localhost', "stringify correct" ); + + $uri = new_ok( $class, [ uri => 'mongodb://localhost' ] ); + is( "$uri", 'mongodb://localhost', "stringify correct" ); +}; + done_testing; diff --git a/t/unit/uri_srv.t b/t/unit/uri_srv.t index df8ca977..7a4dfc76 100644 --- a/t/unit/uri_srv.t +++ b/t/unit/uri_srv.t @@ -90,7 +90,23 @@ subtest "boolean params unchanged" => sub { is $sslVal, 'true', 'SSL true'; is $expires, 1, 'expires as expected'; - } + }; +}; + +subtest "stringification" => sub { + my $uri = new_ok( $class, [ + uri => 'mongodb+srv://testpass@testmongo.example.com', + _test_seedlist_return => [ + [{ target => 'localhost', port => 27017 }], + { + retryWrites => 'true', + retryReads => 'false', + }, + 0 + ] + ]); + + is "$uri", 'mongodb+srv://[**REDACTED**]@testmongo.example.com', 'Stringification for SRV urls correct'; }; done_testing; \ No newline at end of file