Skip to content

Commit 2b835c0

Browse files
committed
RUBY-227 passwords in URIs can contain all
characters excepts commas now.
1 parent 428d959 commit 2b835c0

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/mongo/util/uri_parser.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module Mongo
2020
class URIParser
2121

2222
DEFAULT_PORT = 27017
23-
MONGODB_URI_MATCHER = /(([-_.\w\d]+):([-_\w\d]+)@)?([-.\w\d]+)(:([\w\d]+))?(\/([-\d\w]+))?/
23+
MONGODB_URI_MATCHER = /(([-_.\w\d]+):([^@]+)@)?([-.\w\d]+)(:([\w\d]+))?(\/([-\d\w]+))?/
2424
MONGODB_URI_SPEC = "mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/database]"
2525
SPEC_ATTRS = [:nodes, :auths]
2626
OPT_ATTRS = [:connect, :replicaset, :slaveok, :safe, :w, :wtimeout, :fsync]
@@ -57,6 +57,8 @@ class URIParser
5757
# Parse a MongoDB URI. This method is used by Connection.from_uri.
5858
# Returns an array of nodes and an array of db authorizations, if applicable.
5959
#
60+
# Note: passwords can contain any character except for a ','.
61+
#
6062
# @core connections
6163
def initialize(string)
6264
if string =~ /^mongodb:\/\//

test/uri_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ def test_multiple_uris
2626
assert_equal 27017, parser.nodes[1][1]
2727
end
2828

29+
def test_complex_passwords
30+
parser = Mongo::URIParser.new('mongodb://bob:[email protected]:27018/test')
31+
assert_equal "bob", parser.auths[0]["username"]
32+
assert_equal "secret.word", parser.auths[0]["password"]
33+
34+
parser = Mongo::URIParser.new('mongodb://bob:s-_3#%[email protected]:27018/test')
35+
assert_equal "bob", parser.auths[0]["username"]
36+
assert_equal "s-_3#%R.t", parser.auths[0]["password"]
37+
end
38+
39+
def test_passwords_contain_no_commas
40+
assert_raise MongoArgumentError do
41+
Mongo::URIParser.new('mongodb://bob:a,[email protected]:27018/test')
42+
end
43+
end
44+
2945
def test_multiple_uris_with_auths
3046
parser = Mongo::URIParser.new('mongodb://bob:[email protected]:27018/test,joe:[email protected]/test2')
3147
assert_equal 2, parser.nodes.length

0 commit comments

Comments
 (0)