Skip to content

Commit 652d349

Browse files
committed
RUBY-657 support URI encoded usernames and passwords
1 parent 7609b76 commit 652d349

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/mongo/util/uri_parser.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
# limitations under the License.
1414

1515
require 'cgi'
16+
require 'uri'
1617

1718
module Mongo
1819
class URIParser
1920

20-
USER_REGEX = /([-.\w:]+)/
21+
USER_REGEX = /(.+)/
2122
PASS_REGEX = /([^@,]+)/
2223
AUTH_REGEX = /(#{USER_REGEX}:#{PASS_REGEX}@)?/
2324

@@ -296,6 +297,9 @@ def parse_hosts(uri_without_proto)
296297
end
297298

298299
if uname && pwd && db
300+
uname = URI.unescape(uname)
301+
pwd = URI.unescape(pwd)
302+
299303
auths << {:db_name => db, :username => uname, :password => pwd}
300304
elsif uname || pwd
301305
raise MongoArgumentError, "MongoDB URI must include username, password, "

test/functional/uri_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ def test_complex_usernames
5353
assert_equal "b:ob", parser.auths[0][:username]
5454
end
5555

56+
def test_username_with_encoded_symbol
57+
parser = Mongo::URIParser.new('mongodb://f%40o:bar@localhost/admin')
58+
username = parser.auths.first[:username]
59+
assert_equal 'f@o', username
60+
end
61+
62+
def test_password_with_encoded_symbol
63+
parser = Mongo::URIParser.new('mongodb://foo:b%40r@localhost/admin')
64+
password = parser.auths.first[:password]
65+
assert_equal 'b@r', password
66+
end
67+
5668
def test_passwords_contain_no_commas
5769
assert_raise MongoArgumentError do
5870
Mongo::URIParser.new('mongodb://bob:a,b@a.example.com:27018/test')

0 commit comments

Comments
 (0)