Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

security(web): replace SHA-256 with Argon2 for password hashing

t8y2d66ac936227c
diff --git a/Cargo.lock b/Cargo.lock
index 74927b7fb6..093a568a8d 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 81a3b55276..efd29afeca 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 75a1ee80c1..11476494d2 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<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 {
diff --git a/src-web/src/main.rs b/src-web/src/main.rs
index 1ef3761d21..843fadfeac 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 {