Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(deps): bump pbkdf2 from 0.11.0 to 0.12.2 #725

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions crates/curp/src/client/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use std::{sync::Arc, time::Duration};
use futures::Future;
use tracing::{debug, warn};

use crate::rpc::{connect::ConnectApi, CurpError, Redirect};

use super::state::State;
use crate::rpc::{connect::ConnectApi, CurpError, Redirect};

/// Stream client config
#[derive(Debug)]
Expand Down
1 change: 1 addition & 0 deletions crates/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ getset = "0.1"
opentelemetry = { version = "0.21.0", features = ["trace"] }
opentelemetry_sdk = { version = "0.21.0", features = ["trace"] }
parking_lot = { version = "0.12.1", optional = true }
pbkdf2 = { version = "0.12.2", features = ["simple"] }
petgraph = "0.6.4"
rand = "0.8.5"
serde = { version = "1.0.137", features = ["derive"] }
Expand Down
1 change: 0 additions & 1 deletion crates/utils/benches/interval_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ extern crate utils;
use std::hint::black_box;

use test::Bencher;

use utils::interval_map::{Interval, IntervalMap};

struct Rng {
Expand Down
23 changes: 23 additions & 0 deletions crates/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ pub mod tracing;

use ::tracing::debug;
pub use parser::*;
use pbkdf2::{
password_hash::{rand_core::OsRng, PasswordHasher, SaltString},
Params, Pbkdf2,
};

/// display all elements for the given vector
#[macro_export]
Expand Down Expand Up @@ -270,3 +274,22 @@ pub fn build_endpoint(
};
Ok(endpoint)
}

/// Hash password
///
/// # Errors
///
/// return `Error` when hash password failed
#[inline]
pub fn hash_password(password: &[u8]) -> Result<String, pbkdf2::password_hash::errors::Error> {
let salt = SaltString::generate(&mut OsRng);
let simple_para = Params {
// The recommended rounds is 600,000 or more
// [OWASP cheat sheet]: https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html
rounds: 200_000,
output_length: 32,
};
let hashed_password =
Pbkdf2.hash_password_customized(password, None, None, simple_para, &salt)?;
Ok(hashed_password.to_string())
}
1 change: 0 additions & 1 deletion crates/xline-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ curp = { path = "../curp" }
futures = "0.3.25"
getrandom = "0.2"
http = "0.2.9"
pbkdf2 = { version = "0.11.0", features = ["std"] }
thiserror = "1.0.37"
tokio = { version = "0.2.23", package = "madsim-tokio", features = ["sync"] }
tonic = { version = "0.4.1", package = "madsim-tonic" }
Expand Down
23 changes: 7 additions & 16 deletions crates/xline-client/src/clients/auth.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use std::{fmt::Debug, sync::Arc};

use pbkdf2::{
password_hash::{rand_core::OsRng, PasswordHasher, SaltString},
Pbkdf2,
};
use tonic::transport::Channel;
use utils::hash_password;
use xlineapi::{
command::Command, AuthDisableResponse, AuthEnableResponse, AuthRoleAddResponse,
AuthRoleDeleteResponse, AuthRoleGetResponse, AuthRoleGrantPermissionResponse,
Expand Down Expand Up @@ -246,7 +243,9 @@ impl AuthClient {
"password is required but not provided",
)));
}
let hashed_password = Self::hash_password(request.inner.password.as_bytes());
let hashed_password = hash_password(request.inner.password.as_bytes()).map_err(|err| {
XlineClientError::InternalError(format!("Failed to hash password: {err}"))
})?;
request.inner.hashed_password = hashed_password;
request.inner.password = String::new();
self.handle_req(request.inner, false).await
Expand Down Expand Up @@ -401,7 +400,9 @@ impl AuthClient {
"role name is empty",
)));
}
let hashed_password = Self::hash_password(request.inner.password.as_bytes());
let hashed_password = hash_password(request.inner.password.as_bytes()).map_err(|err| {
XlineClientError::InternalError(format!("Failed to hash password: {err}"))
})?;
request.inner.hashed_password = hashed_password;
request.inner.password = String::new();
self.handle_req(request.inner, false).await
Expand Down Expand Up @@ -739,14 +740,4 @@ impl AuthClient {

Ok(res_wrapper.into())
}

/// Generate hash of the password
fn hash_password(password: &[u8]) -> String {
let salt = SaltString::generate(&mut OsRng);
#[allow(clippy::panic)] // This doesn't seems to be fallible
let hashed_password = Pbkdf2
.hash_password(password, salt.as_ref())
.unwrap_or_else(|e| panic!("Failed to hash password: {e}"));
hashed_password.to_string()
}
}
2 changes: 1 addition & 1 deletion crates/xline/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ opentelemetry-otlp = { version = "0.14.0", features = [
opentelemetry-prometheus = { version = "0.14.1" }
opentelemetry_sdk = { version = "0.21.0", features = ["metrics", "rt-tokio"] }
parking_lot = "0.12.0"
pbkdf2 = { version = "0.11.0", features = ["std"] }
pbkdf2 = { version = "0.12.2", features = ["simple"] }
priority-queue = "1.3.0"
prometheus = "0.13.3"
prost = "0.12.3"
Expand Down
20 changes: 5 additions & 15 deletions crates/xline/src/server/auth_server.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use std::sync::Arc;

use pbkdf2::{
password_hash::{rand_core::OsRng, PasswordHasher, SaltString},
Pbkdf2,
};
use tonic::metadata::MetadataMap;
use tracing::debug;
use utils::hash_password;
use xlineapi::{
command::{Command, CommandResponse, CurpClient, SyncResponse},
request_validation::RequestValidator,
Expand Down Expand Up @@ -72,15 +69,6 @@ where
Ok(res)
}

/// Hash password
fn hash_password(password: &[u8]) -> String {
let salt = SaltString::generate(&mut OsRng);
let hashed_password = Pbkdf2
.hash_password(password, salt.as_ref())
.unwrap_or_else(|e| panic!("Failed to hash password: {e}"));
hashed_password.to_string()
}

/// Propose request and make a response
async fn handle_req<Req, Res>(
&self,
Expand Down Expand Up @@ -145,7 +133,8 @@ where
let user_add_req = request.get_mut();
debug!("Receive AuthUserAddRequest {}", user_add_req);
user_add_req.validation()?;
let hashed_password = Self::hash_password(user_add_req.password.as_bytes());
let hashed_password = hash_password(user_add_req.password.as_bytes())
.map_err(|err| tonic::Status::internal(format!("Failed to hash password: {err}")))?;
user_add_req.hashed_password = hashed_password;
user_add_req.password = String::new();
self.handle_req(request, false).await
Expand Down Expand Up @@ -183,7 +172,8 @@ where
) -> Result<tonic::Response<AuthUserChangePasswordResponse>, tonic::Status> {
debug!("Receive AuthUserChangePasswordRequest {:?}", request);
let user_change_password_req = request.get_mut();
let hashed_password = Self::hash_password(user_change_password_req.password.as_bytes());
let hashed_password = hash_password(user_change_password_req.password.as_bytes())
.map_err(|err| tonic::Status::internal(format!("Failed to hash password: {err}")))?;
user_change_password_req.hashed_password = hashed_password;
user_change_password_req.password = String::new();
self.handle_req(request, false).await
Expand Down
2 changes: 2 additions & 0 deletions workspace-hack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ axum = { version = "0.6" }
bitflags = { version = "2", default-features = false, features = ["std"] }
bytes = { version = "1" }
clap = { version = "4", features = ["derive"] }
crypto-common = { version = "0.1", default-features = false, features = ["std"] }
digest = { version = "0.10", features = ["mac", "std"] }
either = { version = "1" }
futures-channel = { version = "0.3", features = ["sink"] }
Expand All @@ -35,6 +36,7 @@ petgraph = { version = "0.6" }
rand = { version = "0.8", features = ["small_rng"] }
serde = { version = "1", features = ["derive", "rc"] }
serde_json = { version = "1", features = ["raw_value"] }
sha2 = { version = "0.10" }
time = { version = "0.3", features = ["formatting", "macros", "parsing"] }
tokio = { version = "1", features = ["fs", "io-std", "io-util", "macros", "net", "rt-multi-thread", "signal", "sync", "time"] }
tokio-util = { version = "0.7", features = ["codec", "io"] }
Expand Down
Loading