Skip to content

Commit ffa0b66

Browse files
authored
minor: AWS auth spec test fixes (#232)
1 parent c703464 commit ffa0b66

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

src/client/auth/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ impl AuthMechanism {
122122
};
123123
Ok(())
124124
}
125+
#[cfg(feature = "tokio-runtime")]
126+
AuthMechanism::MongoDbAws => {
127+
if credential.username.is_some() && credential.password.is_none() {
128+
return Err(ErrorKind::ArgumentError {
129+
message: "Username cannot be provided without password for MONGODB-AWS \
130+
authentication"
131+
.to_string(),
132+
}
133+
.into());
134+
}
135+
Ok(())
136+
}
125137
_ => Ok(()),
126138
}
127139
}

src/client/options/mod.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,19 @@ fn validate_userinfo(s: &str, userinfo_type: &str) -> Result<()> {
834834
}
835835
.into());
836836
}
837+
838+
// All instances of '%' in the username must be part of an percent-encoded substring. This means
839+
// that there must be two hexidecimal digits following any '%' in the username.
840+
if s.split('%')
841+
.skip(1)
842+
.any(|part| part.len() < 2 || part[0..2].chars().any(|c| !c.is_ascii_hexdigit()))
843+
{
844+
return Err(ErrorKind::ArgumentError {
845+
message: "username/password cannot contain unescaped %".to_string(),
846+
}
847+
.into());
848+
}
849+
837850
Ok(())
838851
}
839852

@@ -1018,12 +1031,6 @@ impl ClientOptionsParser {
10181031
let mut credential = options.credential.get_or_insert_with(Default::default);
10191032
validate_userinfo(u, "username")?;
10201033
let decoded_u = percent_decode(u, "username must be URL encoded")?;
1021-
if decoded_u.chars().any(|c| c == '%') {
1022-
return Err(ErrorKind::ArgumentError {
1023-
message: "username/passowrd cannot contain unescaped %".to_string(),
1024-
}
1025-
.into());
1026-
}
10271034

10281035
credential.username = Some(decoded_u);
10291036

src/test/spec/auth.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ async fn run_auth_test(test_file: TestFile) {
5555
"MONGODB-X509",
5656
"PLAIN",
5757
"MONGODB-CR",
58+
#[cfg(not(feature = "tokio-runtime"))]
5859
"MONGODB-AWS",
5960
];
6061

@@ -84,7 +85,11 @@ async fn run_auth_test(test_file: TestFile) {
8485
None => assert!(options.credential.is_none(), "{}", test_case.description),
8586
}
8687
}
87-
Err(_) => assert!(!test_case.valid, "{}", test_case.description),
88+
Err(e) => assert!(
89+
!test_case.valid,
90+
"got error {:?}: {}",
91+
e, test_case.description
92+
),
8893
};
8994
}
9095
}

0 commit comments

Comments
 (0)