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。

feat(dbx-core): 新增 SQL 安全风险分类

Illuminated2020bc6898cd22aa
diff --git a/crates/dbx-core/src/lib.rs b/crates/dbx-core/src/lib.rs
index 65ae7335a5..0636c7e432 100644
--- a/crates/dbx-core/src/lib.rs
+++ b/crates/dbx-core/src/lib.rs
@@ -14,6 +14,7 @@ pub mod redis_ops;
pub mod saved_sql;
pub mod schema;
pub mod sql;
+pub mod sql_safety;
pub mod storage;
pub mod table_import;
pub mod transfer;
diff --git a/crates/dbx-core/src/sql_safety.rs b/crates/dbx-core/src/sql_safety.rs
new file mode 100644
index 0000000000..5f24ada2dd
--- /dev/null
+++ b/crates/dbx-core/src/sql_safety.rs
@@ -0,0 +1,139 @@
+use serde::{Deserialize, Serialize};
+
+#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
+#[serde(rename_all = "lowercase")]
+pub enum OperationClass {
+ Read,
+ Write,
+ Ddl,
+ Unknown,
+}
+
+#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
+#[serde(rename_all = "lowercase")]
+pub enum RiskLevel {
+ Low,
+ Medium,
+ High,
+ Critical,
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
+#[serde(rename_all = "camelCase")]
+pub struct RiskMetadata {
+ pub operation_class: OperationClass,
+ pub risk_level: RiskLevel,
+ pub is_production: bool,
+ pub production_reason: Option<String>,
+ pub first_token: Option<String>,
+}
+
+pub fn classify_sql(sql: &str) -> OperationClass {
+ let token = first_executable_token(sql).map(|s| s.to_ascii_uppercase());
+ match token.as_deref() {
+ Some("SELECT" | "SHOW" | "DESCRIBE" | "EXPLAIN" | "WITH") => OperationClass::Read,
+ Some("INSERT" | "UPDATE" | "DELETE" | "MERGE" | "REPLACE") => OperationClass::Write,
+ Some("CREATE" | "ALTER" | "DROP" | "TRUNCATE" | "RENAME") => OperationClass::Ddl,
+ _ => OperationClass::Unknown,
+ }
+}
+
+pub fn risk_for(sql: &str, connection_name: &str, color: Option<&str>) -> RiskMetadata {
+ let operation_class = classify_sql(sql);
+ let (is_production, production_reason) = production_signal(connection_name, color);
+ let risk_level = match (operation_class, is_production) {
+ (OperationClass::Read, _) => RiskLevel::Low,
+ (OperationClass::Write, false) => RiskLevel::Medium,
+ (OperationClass::Write, true) => RiskLevel::High,
+ (OperationClass::Ddl, _) => RiskLevel::Critical,
+ (OperationClass::Unknown, _) => RiskLevel::High,
+ };
+
+ RiskMetadata {
+ operation_class,
+ risk_level,
+ is_production,
+ production_reason,
+ first_token: first_executable_token(sql).map(str::to_string),
+ }
+}
+
+fn production_signal(connection_name: &str, color: Option<&str>) -> (bool, Option<String>) {
+ if matches!(color, Some("#ef4444")) {
+ return (true, Some("red connection color".to_string()));
+ }
+
+ let name = connection_name.to_ascii_lowercase();
+ if ["prod", "production", "live"]
+ .iter()
+ .any(|needle| name.contains(needle))
+ {
+ return (true, Some("connection name fallback".to_string()));
+ }
+
+ (false, None)
+}
+
+fn first_executable_token(sql: &str) -> Option<&str> {
+ let bytes = sql.as_bytes();
+ let mut i = 0;
+
+ while i < bytes.len() {
+ while i < bytes.len() && bytes[i].is_ascii_whitespace() {
+ i += 1;
+ }
+
+ if i + 1 < bytes.len() && bytes[i] == b'-' && bytes[i + 1] == b'-' {
+ i += 2;
+ while i < bytes.len() && bytes[i] != b'\n' {
+ i += 1;
+ }
+ continue;
+ }
+
+ if i + 1 < bytes.len() && bytes[i] == b'/' && bytes[i + 1] == b'*' {
+ i += 2;
+ while i + 1 < bytes.len() && !(bytes[i] == b'*' && bytes[i + 1] == b'/') {
+ i += 1;
+ }
+ i = (i + 2).min(bytes.len());
+ continue;
+ }
+
+ break;
+ }
+
+ let start = i;
+ while i < bytes.len() && (bytes[i].is_ascii_alphabetic() || bytes[i] == b'_') {
+ i += 1;
+ }
+
+ (i > start).then_some(&sql[start..i])
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn comments_do_not_hide_read_token() {
+ assert_eq!(classify_sql("-- comment\nSELECT 1"), OperationClass::Read);
+ assert_eq!(classify_sql("/* DROP TABLE x */ SELECT 1"), OperationClass::Read);
+ }
+
+ #[test]
+ fn classifies_write_and_ddl() {
+ assert_eq!(
+ classify_sql("update users set name = 'a'"),
+ OperationClass::Write
+ );
+ assert_eq!(classify_sql("DROP TABLE users"), OperationClass::Ddl);
+ }
+
+ #[test]
+ fn red_color_marks_production() {
+ let risk = risk_for("SELECT * FROM orders", "prod-main", Some("#ef4444"));
+ assert!(risk.is_production);
+ assert_eq!(risk.risk_level, RiskLevel::Low);
+ }
+}