From d66ac936227cb0783bd7e2f1fbca3316f222afcc Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Tue, 5 May 2026 21:11:21 +0800 Subject: [PATCH] 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. --- Cargo.lock | 3 +-- src-web/Cargo.toml | 3 +-- src-web/src/auth.rs | 8 +++----- src-web/src/main.rs | 9 +++++---- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 74927b7fb..093a568a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/src-web/Cargo.toml b/src-web/Cargo.toml index 81a3b5527..efd29afec 100644 --- a/src-web/Cargo.toml +++ b/src-web/Cargo.toml @@ -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" diff --git a/src-web/src/auth.rs b/src-web/src/auth.rs index 75a1ee80c..11476494d 100644 --- a/src-web/src/auth.rs +++ b/src-web/src/auth.rs @@ -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>, Json(body): Json= MAX_ATTEMPTS { diff --git a/src-web/src/main.rs b/src-web/src/main.rs index 1ef3761d2..843fadfea 100644 --- a/src-web/src/main.rs +++ b/src-web/src/main.rs @@ -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 {