Skip to content

Commit dd746f4

Browse files
Divjot Aroralijun4727
andcommitted
GODRIVER-1625 Allow and validate single character credentials (#424)
Co-authored-by: Jun Li <[email protected]>
1 parent 3553c77 commit dd746f4

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

mongo/options/clientoptions_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,14 @@ func TestClientOptions(t *testing.T) {
253253
"mongodb://foo@localhost/",
254254
baseClient().SetAuth(Credential{AuthSource: "admin", Username: "foo"}),
255255
},
256+
{
257+
"Unescaped slash in username",
258+
"mongodb:///:pwd@localhost",
259+
&ClientOptions{err: internal.WrapErrorf(
260+
errors.New("unescaped slash in username"),
261+
"error parsing uri",
262+
)},
263+
},
256264
{
257265
"Password",
258266
"mongodb://foo:bar@localhost/",
@@ -261,6 +269,14 @@ func TestClientOptions(t *testing.T) {
261269
Password: "bar", PasswordSet: true,
262270
}),
263271
},
272+
{
273+
"Single character username and password",
274+
"mongodb://f:b@localhost/",
275+
baseClient().SetAuth(Credential{
276+
AuthSource: "admin", Username: "f",
277+
Password: "b", PasswordSet: true,
278+
}),
279+
},
264280
{
265281
"Connect",
266282
"mongodb://localhost/?connect=direct",

x/mongo/driver/connstring/connstring.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -162,27 +162,25 @@ func (p *parser) parse(original string) error {
162162
p.PasswordSet = true
163163
}
164164

165-
if len(username) > 1 {
166-
if strings.Contains(username, "/") {
167-
return fmt.Errorf("unescaped slash in username")
168-
}
165+
// Validate and process the username.
166+
if strings.Contains(username, "/") {
167+
return fmt.Errorf("unescaped slash in username")
169168
}
170-
171169
p.Username, err = url.QueryUnescape(username)
172170
if err != nil {
173171
return internal.WrapErrorf(err, "invalid username")
174172
}
175-
if len(password) > 1 {
176-
if strings.Contains(password, ":") {
177-
return fmt.Errorf("unescaped colon in password")
178-
}
179-
if strings.Contains(password, "/") {
180-
return fmt.Errorf("unescaped slash in password")
181-
}
182-
p.Password, err = url.QueryUnescape(password)
183-
if err != nil {
184-
return internal.WrapErrorf(err, "invalid password")
185-
}
173+
174+
// Validate and process the password.
175+
if strings.Contains(password, ":") {
176+
return fmt.Errorf("unescaped colon in password")
177+
}
178+
if strings.Contains(password, "/") {
179+
return fmt.Errorf("unescaped slash in password")
180+
}
181+
p.Password, err = url.QueryUnescape(password)
182+
if err != nil {
183+
return internal.WrapErrorf(err, "invalid password")
186184
}
187185
}
188186

0 commit comments

Comments
 (0)