Skip to content

Commit 18ffed2

Browse files
authored
Fix SCRAM password SASLprep (#4201)
* Fix SCRAM password SASLprep * remove panic!() * fmt * add error description
1 parent dc12a3d commit 18ffed2

File tree

1 file changed

+17
-8
lines changed
  • sqlx-postgres/src/connection

1 file changed

+17
-8
lines changed

sqlx-postgres/src/connection/sasl.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ pub(crate) async fn authenticate(
5656
let username = format!("{}={}", USERNAME_ATTR, options.username);
5757
let username = match saslprep(&username) {
5858
Ok(v) => v,
59-
// TODO(danielakhterov): Remove panic when we have proper support for configuration errors
60-
Err(_) => panic!("Failed to saslprep username"),
59+
Err(error) => {
60+
return Err(Error::Configuration(
61+
format!("Failed to saslprep username: {:?}", error).into(),
62+
))
63+
}
6164
};
6265

6366
// nonce = "r=" c-nonce [s-nonce] ;; Second part provided by server.
@@ -86,13 +89,19 @@ pub(crate) async fn authenticate(
8689
}
8790
};
8891

92+
// Normalize(password):
93+
let password = options.password.as_deref().unwrap_or_default();
94+
let password = match saslprep(password) {
95+
Ok(v) => v,
96+
Err(error) => {
97+
return Err(Error::Configuration(
98+
format!("Failed to saslprep password: {:?}", error).into(),
99+
))
100+
}
101+
};
102+
89103
// SaltedPassword := Hi(Normalize(password), salt, i)
90-
let salted_password = hi(
91-
options.password.as_deref().unwrap_or_default(),
92-
&cont.salt,
93-
cont.iterations,
94-
)
95-
.await?;
104+
let salted_password = hi(&password, &cont.salt, cont.iterations).await?;
96105

97106
// ClientKey := HMAC(SaltedPassword, "Client Key")
98107
let mut mac = Hmac::<Sha256>::new_from_slice(&salted_password).map_err(Error::protocol)?;

0 commit comments

Comments
 (0)