Skip to content

Commit 3446ea5

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

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
@@ -216,27 +216,25 @@ func (p *parser) parse(original string) error {
216216
p.PasswordSet = true
217217
}
218218

219-
if len(username) > 1 {
220-
if strings.Contains(username, "/") {
221-
return fmt.Errorf("unescaped slash in username")
222-
}
219+
// Validate and process the username.
220+
if strings.Contains(username, "/") {
221+
return fmt.Errorf("unescaped slash in username")
223222
}
224-
225223
p.Username, err = url.QueryUnescape(username)
226224
if err != nil {
227225
return internal.WrapErrorf(err, "invalid username")
228226
}
229-
if len(password) > 1 {
230-
if strings.Contains(password, ":") {
231-
return fmt.Errorf("unescaped colon in password")
232-
}
233-
if strings.Contains(password, "/") {
234-
return fmt.Errorf("unescaped slash in password")
235-
}
236-
p.Password, err = url.QueryUnescape(password)
237-
if err != nil {
238-
return internal.WrapErrorf(err, "invalid password")
239-
}
227+
228+
// Validate and process the password.
229+
if strings.Contains(password, ":") {
230+
return fmt.Errorf("unescaped colon in password")
231+
}
232+
if strings.Contains(password, "/") {
233+
return fmt.Errorf("unescaped slash in password")
234+
}
235+
p.Password, err = url.QueryUnescape(password)
236+
if err != nil {
237+
return internal.WrapErrorf(err, "invalid password")
240238
}
241239
}
242240

0 commit comments

Comments
 (0)