security(web): replace SHA-256 with Argon2 for password hashing
SHA-256 without salt is vulnerable to rainbow table attacks. Argon2 provides salted slow hashing resistant to brute force.
This commit is contained in:
Generated
+1
-2
@@ -1623,16 +1623,15 @@ dependencies = [
|
||||
name = "dbx-web"
|
||||
version = "0.4.0"
|
||||
dependencies = [
|
||||
"argon2",
|
||||
"async-stream",
|
||||
"axum",
|
||||
"dbx-core",
|
||||
"futures",
|
||||
"hex",
|
||||
"log",
|
||||
"rustls 0.23.40",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha2 0.10.9",
|
||||
"tokio",
|
||||
"tokio-util",
|
||||
"tower-http",
|
||||
|
||||
+1
-2
@@ -15,8 +15,7 @@ tokio = { version = "1", features = ["full"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
uuid = { version = "1", features = ["v4"] }
|
||||
sha2 = "0.10"
|
||||
hex = "0.4"
|
||||
argon2 = "0.5"
|
||||
log = "0.4"
|
||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||
tracing = "0.1"
|
||||
|
||||
+3
-5
@@ -1,12 +1,12 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use argon2::{Argon2, PasswordHash, PasswordVerifier};
|
||||
use axum::extract::State;
|
||||
use axum::http::{Request, StatusCode};
|
||||
use axum::middleware::Next;
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use axum::Json;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::state::WebState;
|
||||
|
||||
@@ -47,11 +47,9 @@ pub async fn login(State(state): State<Arc<WebState>>, Json(body): Json<LoginReq
|
||||
}
|
||||
}
|
||||
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(body.password.as_bytes());
|
||||
let hash = hex::encode(hasher.finalize());
|
||||
let parsed_hash = PasswordHash::new(password_hash).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
|
||||
if hash != *password_hash {
|
||||
if Argon2::default().verify_password(body.password.as_bytes(), &parsed_hash).is_err() {
|
||||
let mut rl = state.login_rate_limit.lock().await;
|
||||
rl.fail_count += 1;
|
||||
if rl.fail_count >= MAX_ATTEMPTS {
|
||||
|
||||
+5
-4
@@ -8,12 +8,14 @@ use std::collections::{HashMap, HashSet};
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
|
||||
use argon2::password_hash::rand_core::OsRng;
|
||||
use argon2::password_hash::SaltString;
|
||||
use argon2::{Argon2, PasswordHasher};
|
||||
use axum::middleware;
|
||||
use axum::routing::{delete, get, post};
|
||||
use axum::Router;
|
||||
use dbx_core::connection::AppState;
|
||||
use dbx_core::storage::Storage;
|
||||
use sha2::{Digest, Sha256};
|
||||
use tokio::sync::RwLock;
|
||||
use tower_http::cors::{Any, CorsLayer};
|
||||
|
||||
@@ -46,9 +48,8 @@ async fn main() {
|
||||
|
||||
// Password hash
|
||||
let password_hash = std::env::var("DBX_PASSWORD").ok().map(|pw| {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(pw.as_bytes());
|
||||
hex::encode(hasher.finalize())
|
||||
let salt = SaltString::generate(&mut OsRng);
|
||||
Argon2::default().hash_password(pw.as_bytes(), &salt).expect("Failed to hash password").to_string()
|
||||
});
|
||||
|
||||
let web_state = Arc::new(WebState {
|
||||
|
||||
Reference in New Issue
Block a user