feat(safety): add production environment safeguards
This commit is contained in:
1 parent
be66d6f46b
commit
1be66fb115
80 files changed
+4220
-234
No files matched your search
@@ -620,6 +620,8 @@ mod tests {
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: false,
|
||||
is_production: false,
|
||||
production_databases: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -456,6 +456,12 @@ async fn execute_execute_query(
|
||||
// Classify SQL risk using sqlparser AST
|
||||
let db_type_str = format!("{:?}", db_type).to_lowercase();
|
||||
let risk = crate::sql_risk::classify_sql_risk(sql, &db_type_str)?;
|
||||
let connection_config = state.configs.read().await.get(connection_id).cloned();
|
||||
if let Some(config) = connection_config {
|
||||
if risk != SqlRisk::ReadOnly && crate::production_safety::targets_production_database(&config, database, sql) {
|
||||
return Err("Blocked: AI agents cannot execute writes or DDL on a production database. Return the SQL for the user to review and execute manually in DBX.".to_string());
|
||||
}
|
||||
}
|
||||
if !sql_risk_allowed(risk, sql_permissions) {
|
||||
if risk == SqlRisk::Transaction {
|
||||
return Err("Blocked: transaction control statements are not available to the AI agent.".to_string());
|
||||
|
||||
@@ -1058,6 +1058,8 @@ mod tests {
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: false,
|
||||
is_production: false,
|
||||
production_databases: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1116,6 +1118,8 @@ mod tests {
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: false,
|
||||
is_production: false,
|
||||
production_databases: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1220,6 +1224,8 @@ mod tests {
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: false,
|
||||
is_production: false,
|
||||
production_databases: vec![],
|
||||
};
|
||||
scrub_connection_secrets(&mut config);
|
||||
assert!(config.password.is_empty());
|
||||
|
||||
@@ -3144,6 +3144,8 @@ mod tests {
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: false,
|
||||
is_production: false,
|
||||
production_databases: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -763,6 +763,8 @@ mod tests {
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: false,
|
||||
is_production: false,
|
||||
production_databases: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3108,6 +3108,8 @@ mod tests {
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: false,
|
||||
is_production: false,
|
||||
production_databases: vec![],
|
||||
};
|
||||
|
||||
assert_eq!(redis_database_index(&config), 4);
|
||||
|
||||
@@ -36,6 +36,7 @@ pub mod object_source_sql;
|
||||
pub mod path_utils;
|
||||
pub mod plugins;
|
||||
pub mod process;
|
||||
pub mod production_safety;
|
||||
pub mod query;
|
||||
pub mod query_cancel;
|
||||
pub mod query_execution_sql;
|
||||
|
||||
@@ -90,6 +90,12 @@ pub struct ConnectionConfig {
|
||||
pub one_time: bool,
|
||||
#[serde(default, skip_serializing_if = "is_false")]
|
||||
pub read_only: bool,
|
||||
/// Explicitly marks every database reachable through this connection as production.
|
||||
#[serde(default, skip_serializing_if = "is_false")]
|
||||
pub is_production: bool,
|
||||
/// Database-level production markers for multi-database connections.
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub production_databases: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
@@ -457,6 +463,10 @@ struct ConnectionConfigData {
|
||||
pub one_time: bool,
|
||||
#[serde(default)]
|
||||
pub read_only: bool,
|
||||
#[serde(default)]
|
||||
pub is_production: bool,
|
||||
#[serde(default)]
|
||||
pub production_databases: Vec<String>,
|
||||
}
|
||||
|
||||
impl From<ConnectionConfigData> for ConnectionConfig {
|
||||
@@ -507,6 +517,8 @@ impl From<ConnectionConfigData> for ConnectionConfig {
|
||||
jdbc_driver_paths: data.jdbc_driver_paths,
|
||||
one_time: data.one_time,
|
||||
read_only: data.read_only,
|
||||
is_production: data.is_production,
|
||||
production_databases: data.production_databases,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1834,6 +1846,8 @@ mod tests {
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: false,
|
||||
is_production: false,
|
||||
production_databases: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -144,6 +144,8 @@ mod tests {
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: false,
|
||||
is_production: false,
|
||||
production_databases: vec![],
|
||||
};
|
||||
cfg.redis_key_separator = ":".to_string();
|
||||
cfg
|
||||
|
||||
@@ -596,6 +596,8 @@ mod tests {
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only,
|
||||
is_production: false,
|
||||
production_databases: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -169,6 +169,8 @@ mod tests {
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: false,
|
||||
is_production: false,
|
||||
production_databases: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -213,6 +213,8 @@ mod tests {
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: true,
|
||||
is_production: false,
|
||||
production_databases: Vec::new(),
|
||||
};
|
||||
cfg.read_only = true;
|
||||
state.configs.write().await.insert(cfg.id.clone(), cfg);
|
||||
@@ -278,6 +280,8 @@ mod tests {
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: true,
|
||||
is_production: false,
|
||||
production_databases: Vec::new(),
|
||||
};
|
||||
state.configs.write().await.insert(cfg.id.clone(), cfg);
|
||||
let err = nacos_rollback_config_core(
|
||||
|
||||
@@ -0,0 +1,687 @@
|
||||
use crate::models::connection::{ConnectionConfig, DatabaseType};
|
||||
use regex::Regex;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::LazyLock;
|
||||
|
||||
const IDENTIFIER_PATTERN: &str = r"[A-Za-z0-9_@$#-]*[A-Za-z_@$#][A-Za-z0-9_@$#-]*";
|
||||
const TARGET_NAME_PATTERN: &str = r"[A-Za-z0-9_@$#-]*[A-Za-z_@$#][A-Za-z0-9_@$#-]*(?:\s*\.\s*(?:\*|[A-Za-z0-9_@$#-]*[A-Za-z_@$#][A-Za-z0-9_@$#-]*)){0,2}";
|
||||
const QUALIFIED_NAME_PATTERN: &str = r"[A-Za-z0-9_@$#-]*[A-Za-z_@$#][A-Za-z0-9_@$#-]*\s*\.\s*(?:\*|[A-Za-z0-9_@$#-]*[A-Za-z_@$#][A-Za-z0-9_@$#-]*)(?:\s*\.\s*(?:\*|[A-Za-z0-9_@$#-]*[A-Za-z_@$#][A-Za-z0-9_@$#-]*))?";
|
||||
|
||||
static DML_TARGET_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(&format!(r"(?is)\b(?:FROM|JOIN|UPDATE|INTO|REFERENCES)\s+({TARGET_NAME_PATTERN})"))
|
||||
.expect("valid DML target regex")
|
||||
});
|
||||
static DDL_OBJECT_TARGET_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(&format!(
|
||||
r"(?is)\b(?:CREATE|ALTER|DROP)\s+(?:OR\s+REPLACE\s+)?(?:TABLE|VIEW|MATERIALIZED\s+VIEW|INDEX|SEQUENCE|FUNCTION|PROCEDURE|ROUTINE|TRIGGER|EVENT|TYPE|SYNONYM)\s+(?:IF\s+(?:NOT\s+)?EXISTS\s+)?(?:ONLY\s+)?({TARGET_NAME_PATTERN})"
|
||||
))
|
||||
.expect("valid DDL object target regex")
|
||||
});
|
||||
static INDEX_ON_TARGET_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(&format!(r"(?is)\b(?:CREATE|ALTER|DROP)\s+(?:UNIQUE\s+)?INDEX\b.*?\bON\s+({TARGET_NAME_PATTERN})"))
|
||||
.expect("valid index target regex")
|
||||
});
|
||||
static DATABASE_TARGET_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(&format!(
|
||||
r"(?is)\b(?:CREATE|ALTER|DROP)\s+(DATABASE|SCHEMA|CATALOG)\s+(?:IF\s+(?:NOT\s+)?EXISTS\s+)?({IDENTIFIER_PATTERN})"
|
||||
))
|
||||
.expect("valid database target regex")
|
||||
});
|
||||
static USE_RE: LazyLock<Regex> =
|
||||
LazyLock::new(|| Regex::new(&format!(r"(?is)^\s*USE\s+({IDENTIFIER_PATTERN})")).expect("valid USE regex"));
|
||||
static COPY_TARGET_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(&format!(r"(?is)^\s*COPY\s+({TARGET_NAME_PATTERN})\s+FROM\b")).expect("valid COPY target regex")
|
||||
});
|
||||
static TRUNCATE_TARGET_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(&format!(r"(?is)\bTRUNCATE\s+(?:TABLE\s+)?({TARGET_NAME_PATTERN})"))
|
||||
.expect("valid truncate target regex")
|
||||
});
|
||||
static RENAME_TABLE_TARGET_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(&format!(r"(?is)\bRENAME\s+TABLE\s+({TARGET_NAME_PATTERN})\s+TO\s+({TARGET_NAME_PATTERN})"))
|
||||
.expect("valid rename table target regex")
|
||||
});
|
||||
static MAINTENANCE_TABLE_TARGET_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(&format!(
|
||||
r"(?is)\b(?:ANALYZE|OPTIMIZE|REPAIR|CHECK)\s+(?:NO_WRITE_TO_BINLOG\s+|LOCAL\s+)?TABLE\s+({TARGET_NAME_PATTERN})"
|
||||
))
|
||||
.expect("valid maintenance table target regex")
|
||||
});
|
||||
static COMMENT_TARGET_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(&format!(
|
||||
r"(?is)\bCOMMENT\s+ON\s+(?:TABLE|VIEW|COLUMN|INDEX|SEQUENCE|FUNCTION|PROCEDURE|TYPE)\s+({TARGET_NAME_PATTERN})"
|
||||
))
|
||||
.expect("valid comment target regex")
|
||||
});
|
||||
static ROUTINE_CALL_TARGET_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(&format!(r"(?is)\b(?:CALL|EXEC|EXECUTE)\s+({QUALIFIED_NAME_PATTERN})"))
|
||||
.expect("valid routine call target regex")
|
||||
});
|
||||
static PRIVILEGE_TARGET_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(&format!(
|
||||
r"(?is)\b(?:GRANT|REVOKE|DENY)\b.*?\bON\s+(?:(?:TABLE|SEQUENCE|FUNCTION|PROCEDURE|ROUTINE|OBJECT)\s+|OBJECT\s*::\s*)?({QUALIFIED_NAME_PATTERN})"
|
||||
))
|
||||
.expect("valid privilege target regex")
|
||||
});
|
||||
static PRIVILEGE_DATABASE_TARGET_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(&format!(
|
||||
r"(?is)\b(?:GRANT|REVOKE|DENY)\b.*?\bON\s+(?:DATABASE|CATALOG)(?:::|\s+)\s*({IDENTIFIER_PATTERN})"
|
||||
))
|
||||
.expect("valid privilege database target regex")
|
||||
});
|
||||
static GLOBAL_PRIVILEGE_TARGET_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(r"(?is)\b(?:GRANT|REVOKE|DENY)\b.*?\bON\s+\*\s*\.\s*\*").expect("valid global privilege target regex")
|
||||
});
|
||||
static GLOBAL_DDL_TARGET_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(r"(?is)^\s*(?:CREATE|ALTER|DROP)\s+(?:USER|ROLE|LOGIN|SERVER|TABLESPACE|RESOURCE|PROFILE|ACCOUNT)\b")
|
||||
.expect("valid global DDL target regex")
|
||||
});
|
||||
static MULTI_TARGET_MUTATION_RE: LazyLock<Regex> = LazyLock::new(|| {
|
||||
Regex::new(r"(?is)^\s*(?:DROP\s+(?:TEMPORARY\s+)?TABLE\b.*,|RENAME\s+TABLE\b.*,)")
|
||||
.expect("valid multi-target mutation regex")
|
||||
});
|
||||
static FIRST_KEYWORD_RE: LazyLock<Regex> =
|
||||
LazyLock::new(|| Regex::new(IDENTIFIER_PATTERN).expect("valid first keyword regex"));
|
||||
|
||||
#[derive(Default)]
|
||||
struct ReferencedDatabaseAssessment {
|
||||
databases: HashSet<String>,
|
||||
uncertain: bool,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct SqlTargetSafetyText {
|
||||
text: String,
|
||||
quoted_identifiers: HashMap<String, String>,
|
||||
}
|
||||
|
||||
/// Returns whether the selected database inherits an explicit production marker.
|
||||
pub fn is_production_database(config: &ConnectionConfig, database: &str) -> bool {
|
||||
config.is_production
|
||||
|| (!database.trim().is_empty()
|
||||
&& config
|
||||
.production_databases
|
||||
.iter()
|
||||
.any(|name| normalize_database_name(name) == normalize_database_name(database)))
|
||||
}
|
||||
|
||||
/// Returns whether a non-read SQL statement targets production scope.
|
||||
///
|
||||
/// Agent execution already classifies SQL risk with `sql_risk`; this function
|
||||
/// focuses only on production scope, including qualified cross-database writes
|
||||
/// such as `DELETE FROM prod_app.users` while the selected database is staging.
|
||||
pub fn targets_production_database(config: &ConnectionConfig, active_database: &str, sql: &str) -> bool {
|
||||
if is_production_database(config, active_database) {
|
||||
return true;
|
||||
}
|
||||
|
||||
let marked: HashSet<String> =
|
||||
config.production_databases.iter().map(|name| normalize_database_name(name)).collect();
|
||||
if marked.is_empty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
let assessment = referenced_databases(sql, &config.db_type, active_database);
|
||||
assessment.databases.into_iter().any(|database| marked.contains(&database)) || assessment.uncertain
|
||||
}
|
||||
|
||||
fn normalize_database_name(value: &str) -> String {
|
||||
value.trim().trim_matches(|ch| matches!(ch, '`' | '"' | '[' | ']')).to_ascii_lowercase()
|
||||
}
|
||||
|
||||
fn referenced_databases(sql: &str, db_type: &DatabaseType, active_database: &str) -> ReferencedDatabaseAssessment {
|
||||
let mut assessment = ReferencedDatabaseAssessment::default();
|
||||
let cleaned = sql_target_safety_text(sql);
|
||||
let mut use_database = String::new();
|
||||
let normalized_active_database = normalize_database_name(active_database);
|
||||
|
||||
for statement in cleaned.text.split(';').map(str::trim).filter(|statement| !statement.is_empty()) {
|
||||
let mut statement_databases = HashSet::new();
|
||||
let statement_is_mutation = crate::query_execution_sql::is_write_sql(statement);
|
||||
if let Some(database) = USE_RE
|
||||
.captures(statement)
|
||||
.and_then(|capture| capture.get(1))
|
||||
.map(|value| normalize_target_database_name(value.as_str(), &cleaned.quoted_identifiers))
|
||||
.filter(|value| !value.is_empty())
|
||||
{
|
||||
use_database = database;
|
||||
continue;
|
||||
}
|
||||
|
||||
if !statement_is_mutation {
|
||||
continue;
|
||||
}
|
||||
|
||||
let current_database =
|
||||
if use_database.is_empty() { normalized_active_database.as_str() } else { use_database.as_str() };
|
||||
|
||||
collect_qualified_target_databases(
|
||||
statement,
|
||||
db_type,
|
||||
&cleaned.quoted_identifiers,
|
||||
current_database,
|
||||
&mut statement_databases,
|
||||
&[
|
||||
&DML_TARGET_RE,
|
||||
&DDL_OBJECT_TARGET_RE,
|
||||
&INDEX_ON_TARGET_RE,
|
||||
&TRUNCATE_TARGET_RE,
|
||||
&MAINTENANCE_TABLE_TARGET_RE,
|
||||
&COMMENT_TARGET_RE,
|
||||
&ROUTINE_CALL_TARGET_RE,
|
||||
&PRIVILEGE_TARGET_RE,
|
||||
],
|
||||
);
|
||||
collect_qualified_target_database_groups(
|
||||
statement,
|
||||
db_type,
|
||||
&cleaned.quoted_identifiers,
|
||||
current_database,
|
||||
&mut statement_databases,
|
||||
&RENAME_TABLE_TARGET_RE,
|
||||
&[1, 2],
|
||||
);
|
||||
for capture in DATABASE_TARGET_RE.captures_iter(statement) {
|
||||
if let Some(database) = capture
|
||||
.get(1)
|
||||
.filter(|kind| database_target_kind_means_database(kind.as_str(), db_type))
|
||||
.and_then(|_| capture.get(2))
|
||||
.map(|value| normalize_target_database_name(value.as_str(), &cleaned.quoted_identifiers))
|
||||
.filter(|value| !value.is_empty())
|
||||
{
|
||||
statement_databases.insert(database);
|
||||
}
|
||||
}
|
||||
for capture in PRIVILEGE_DATABASE_TARGET_RE.captures_iter(statement) {
|
||||
if let Some(database) = capture
|
||||
.get(1)
|
||||
.map(|value| normalize_target_database_name(value.as_str(), &cleaned.quoted_identifiers))
|
||||
.filter(|value| !value.is_empty())
|
||||
{
|
||||
statement_databases.insert(database);
|
||||
}
|
||||
}
|
||||
if let Some(database) = COPY_TARGET_RE
|
||||
.captures(statement)
|
||||
.and_then(|capture| capture.get(1))
|
||||
.and_then(|target| {
|
||||
database_from_qualified_name(target.as_str(), db_type, &cleaned.quoted_identifiers, current_database)
|
||||
})
|
||||
.filter(|value| !value.is_empty())
|
||||
{
|
||||
statement_databases.insert(database);
|
||||
}
|
||||
let has_resolved_target = !statement_databases.is_empty();
|
||||
assessment.databases.extend(statement_databases);
|
||||
// The target regexes intentionally extract one object at a time. Until all
|
||||
// list forms are parsed, never let a resolved first target disable fallback.
|
||||
assessment.uncertain = assessment.uncertain
|
||||
|| GLOBAL_PRIVILEGE_TARGET_RE.is_match(statement)
|
||||
|| MULTI_TARGET_MUTATION_RE.is_match(statement)
|
||||
|| is_ambiguous_production_target_statement(statement, has_resolved_target);
|
||||
}
|
||||
assessment
|
||||
}
|
||||
|
||||
fn collect_qualified_target_databases(
|
||||
statement: &str,
|
||||
db_type: &DatabaseType,
|
||||
quoted_identifiers: &HashMap<String, String>,
|
||||
current_database: &str,
|
||||
databases: &mut HashSet<String>,
|
||||
patterns: &[&LazyLock<Regex>],
|
||||
) {
|
||||
for pattern in patterns {
|
||||
collect_qualified_target_database_groups(
|
||||
statement,
|
||||
db_type,
|
||||
quoted_identifiers,
|
||||
current_database,
|
||||
databases,
|
||||
pattern,
|
||||
&[1],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn collect_qualified_target_database_groups(
|
||||
statement: &str,
|
||||
db_type: &DatabaseType,
|
||||
quoted_identifiers: &HashMap<String, String>,
|
||||
current_database: &str,
|
||||
databases: &mut HashSet<String>,
|
||||
pattern: &LazyLock<Regex>,
|
||||
capture_indexes: &[usize],
|
||||
) {
|
||||
for capture in pattern.captures_iter(statement) {
|
||||
for capture_index in capture_indexes {
|
||||
if let Some(database) = capture
|
||||
.get(*capture_index)
|
||||
.and_then(|target| {
|
||||
database_from_qualified_name(target.as_str(), db_type, quoted_identifiers, current_database)
|
||||
})
|
||||
.filter(|value| !value.is_empty())
|
||||
{
|
||||
databases.insert(database);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn database_from_qualified_name(
|
||||
qualified_name: &str,
|
||||
db_type: &DatabaseType,
|
||||
quoted_identifiers: &HashMap<String, String>,
|
||||
current_database: &str,
|
||||
) -> Option<String> {
|
||||
let parts: Vec<String> = qualified_name
|
||||
.split('.')
|
||||
.map(|part| normalize_target_database_name(part, quoted_identifiers))
|
||||
.filter(|part| !part.is_empty())
|
||||
.collect();
|
||||
if parts.len() < 2 {
|
||||
return (!current_database.is_empty()).then(|| current_database.to_string());
|
||||
}
|
||||
if qualified_first_part_is_database(db_type, parts.len()) {
|
||||
return parts.first().cloned();
|
||||
}
|
||||
(!current_database.is_empty()).then(|| current_database.to_string())
|
||||
}
|
||||
|
||||
fn normalize_target_database_name(value: &str, quoted_identifiers: &HashMap<String, String>) -> String {
|
||||
let normalized = normalize_database_name(value);
|
||||
quoted_identifiers.get(&normalized).map(|quoted| normalize_database_name(quoted)).unwrap_or(normalized)
|
||||
}
|
||||
|
||||
fn qualified_first_part_is_database(db_type: &DatabaseType, part_count: usize) -> bool {
|
||||
if part_count >= 3
|
||||
&& matches!(
|
||||
db_type,
|
||||
DatabaseType::SqlServer
|
||||
| DatabaseType::Snowflake
|
||||
| DatabaseType::Trino
|
||||
| DatabaseType::PrestoSql
|
||||
| DatabaseType::Databricks
|
||||
| DatabaseType::Bigquery
|
||||
)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if schema_first_qualifier_type(db_type) {
|
||||
return false;
|
||||
}
|
||||
part_count >= 2
|
||||
}
|
||||
|
||||
fn database_target_kind_means_database(kind: &str, db_type: &DatabaseType) -> bool {
|
||||
if kind.eq_ignore_ascii_case("database") || kind.eq_ignore_ascii_case("catalog") {
|
||||
return true;
|
||||
}
|
||||
kind.eq_ignore_ascii_case("schema") && !schema_first_qualifier_type(db_type)
|
||||
}
|
||||
|
||||
fn schema_first_qualifier_type(db_type: &DatabaseType) -> bool {
|
||||
matches!(
|
||||
db_type,
|
||||
DatabaseType::Postgres
|
||||
| DatabaseType::Redshift
|
||||
| DatabaseType::Gaussdb
|
||||
| DatabaseType::Kwdb
|
||||
| DatabaseType::OpenGauss
|
||||
| DatabaseType::Kingbase
|
||||
| DatabaseType::Highgo
|
||||
| DatabaseType::Vastbase
|
||||
| DatabaseType::Yashandb
|
||||
| DatabaseType::Oracle
|
||||
| DatabaseType::OceanbaseOracle
|
||||
| DatabaseType::Dameng
|
||||
| DatabaseType::Firebird
|
||||
| DatabaseType::Exasol
|
||||
| DatabaseType::Teradata
|
||||
| DatabaseType::Vertica
|
||||
| DatabaseType::Db2
|
||||
| DatabaseType::Informix
|
||||
| DatabaseType::H2
|
||||
| DatabaseType::Iris
|
||||
| DatabaseType::Xugu
|
||||
| DatabaseType::Oscar
|
||||
| DatabaseType::Gbase
|
||||
| DatabaseType::SapHana
|
||||
| DatabaseType::SqlServer
|
||||
| DatabaseType::Snowflake
|
||||
| DatabaseType::Trino
|
||||
| DatabaseType::PrestoSql
|
||||
| DatabaseType::Databricks
|
||||
| DatabaseType::Bigquery
|
||||
)
|
||||
}
|
||||
|
||||
fn is_ambiguous_production_target_statement(statement: &str, has_resolved_target: bool) -> bool {
|
||||
if !crate::query_execution_sql::is_write_sql(statement) {
|
||||
return false;
|
||||
}
|
||||
let Some(first_keyword) = first_keyword(statement) else {
|
||||
return true;
|
||||
};
|
||||
if is_transaction_keyword(&first_keyword) {
|
||||
return false;
|
||||
}
|
||||
GLOBAL_DDL_TARGET_RE.is_match(statement) || !has_resolved_target
|
||||
}
|
||||
|
||||
fn first_keyword(statement: &str) -> Option<String> {
|
||||
FIRST_KEYWORD_RE.find(statement).map(|value| value.as_str().to_ascii_lowercase())
|
||||
}
|
||||
|
||||
fn is_transaction_keyword(keyword: &str) -> bool {
|
||||
matches!(keyword, "begin" | "start" | "commit" | "rollback" | "abort" | "savepoint" | "release")
|
||||
}
|
||||
|
||||
fn sql_target_safety_text(sql: &str) -> SqlTargetSafetyText {
|
||||
let chars: Vec<char> = sql.chars().collect();
|
||||
let mut result = SqlTargetSafetyText { text: String::with_capacity(sql.len()), quoted_identifiers: HashMap::new() };
|
||||
append_sql_target_safety_text(&chars, &mut result);
|
||||
result
|
||||
}
|
||||
|
||||
fn append_sql_target_safety_text(chars: &[char], result: &mut SqlTargetSafetyText) {
|
||||
let mut index = 0usize;
|
||||
|
||||
while index < chars.len() {
|
||||
let ch = chars[index];
|
||||
let next = chars.get(index + 1).copied();
|
||||
|
||||
if ch == '-' && next == Some('-') {
|
||||
index += 2;
|
||||
while index < chars.len() && chars[index] != '\n' && chars[index] != '\r' {
|
||||
index += 1;
|
||||
}
|
||||
result.text.push(' ');
|
||||
continue;
|
||||
}
|
||||
if ch == '#' {
|
||||
index += 1;
|
||||
while index < chars.len() && chars[index] != '\n' && chars[index] != '\r' {
|
||||
index += 1;
|
||||
}
|
||||
result.text.push(' ');
|
||||
continue;
|
||||
}
|
||||
if ch == '/' && next == Some('*') {
|
||||
if let Some((body, close_index)) = mysql_executable_comment_body(&chars, index) {
|
||||
result.text.push(' ');
|
||||
let body_chars: Vec<char> = body.chars().collect();
|
||||
append_sql_target_safety_text(&body_chars, result);
|
||||
result.text.push(' ');
|
||||
index = close_index;
|
||||
} else {
|
||||
index += 2;
|
||||
while index + 1 < chars.len() && !(chars[index] == '*' && chars[index + 1] == '/') {
|
||||
index += 1;
|
||||
}
|
||||
index = (index + 2).min(chars.len());
|
||||
result.text.push(' ');
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some((tag, tag_len)) = dollar_quote_tag_at(&chars, index) {
|
||||
index += tag_len;
|
||||
while index + tag_len <= chars.len() && !chars[index..index + tag_len].iter().collect::<String>().eq(&tag) {
|
||||
index += 1;
|
||||
}
|
||||
index = (index + tag_len).min(chars.len());
|
||||
result.text.push(' ');
|
||||
continue;
|
||||
}
|
||||
if ch == '\'' {
|
||||
index = skip_string_literal(&chars, index, '\'', '\'');
|
||||
result.text.push(' ');
|
||||
continue;
|
||||
}
|
||||
if ch == '"' || ch == '`' || ch == '[' {
|
||||
let close = if ch == '[' { ']' } else { ch };
|
||||
index = append_quoted_identifier_token(chars, index, close, result);
|
||||
continue;
|
||||
}
|
||||
|
||||
result.text.push(ch);
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn mysql_executable_comment_body(chars: &[char], start: usize) -> Option<(String, usize)> {
|
||||
if chars.get(start) != Some(&'/') || chars.get(start + 1) != Some(&'*') {
|
||||
return None;
|
||||
}
|
||||
let mut index = start + 2;
|
||||
match chars.get(index).copied() {
|
||||
Some('!') => index += 1,
|
||||
Some('M') if chars.get(index + 1) == Some(&'!') => index += 2,
|
||||
_ => return None,
|
||||
}
|
||||
while matches!(chars.get(index), Some(ch) if ch.is_ascii_digit() || ch.is_whitespace()) {
|
||||
index += 1;
|
||||
}
|
||||
let body_start = index;
|
||||
while index + 1 < chars.len() {
|
||||
if chars[index] == '*' && chars[index + 1] == '/' {
|
||||
return Some((chars[body_start..index].iter().collect(), index + 2));
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
Some((chars[body_start..].iter().collect(), chars.len()))
|
||||
}
|
||||
|
||||
fn dollar_quote_tag_at(chars: &[char], start: usize) -> Option<(String, usize)> {
|
||||
if chars.get(start) != Some(&'$') {
|
||||
return None;
|
||||
}
|
||||
let mut index = start + 1;
|
||||
if chars.get(index) == Some(&'$') {
|
||||
return Some(("$$".to_string(), 2));
|
||||
}
|
||||
if !matches!(chars.get(index), Some(ch) if ch.is_ascii_alphabetic() || *ch == '_') {
|
||||
return None;
|
||||
}
|
||||
index += 1;
|
||||
while matches!(chars.get(index), Some(ch) if ch.is_ascii_alphanumeric() || *ch == '_') {
|
||||
index += 1;
|
||||
}
|
||||
if chars.get(index) != Some(&'$') {
|
||||
return None;
|
||||
}
|
||||
let tag: String = chars[start..=index].iter().collect();
|
||||
Some((tag, index - start + 1))
|
||||
}
|
||||
|
||||
fn skip_string_literal(chars: &[char], start: usize, open: char, close: char) -> usize {
|
||||
let mut index = start + 1;
|
||||
while index < chars.len() {
|
||||
if chars[index] == '\\' && matches!(open, '\'' | '"') {
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
if chars[index] == close {
|
||||
if chars.get(index + 1) == Some(&close) {
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
return index + 1;
|
||||
}
|
||||
index += 1;
|
||||
}
|
||||
chars.len()
|
||||
}
|
||||
|
||||
fn append_quoted_identifier_token(
|
||||
chars: &[char],
|
||||
start: usize,
|
||||
close: char,
|
||||
result: &mut SqlTargetSafetyText,
|
||||
) -> usize {
|
||||
let mut index = start + 1;
|
||||
let mut identifier = String::new();
|
||||
while index < chars.len() {
|
||||
if chars[index] == close {
|
||||
if chars.get(index + 1) == Some(&close) {
|
||||
identifier.push(close);
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
let token = format!("__dbxq{}__", result.quoted_identifiers.len());
|
||||
result.quoted_identifiers.insert(token.to_ascii_lowercase(), identifier);
|
||||
result.text.push(' ');
|
||||
result.text.push_str(&token);
|
||||
result.text.push(' ');
|
||||
return index + 1;
|
||||
}
|
||||
identifier.push(if chars[index] == ';' { ' ' } else { chars[index] });
|
||||
index += 1;
|
||||
}
|
||||
let token = format!("__dbxq{}__", result.quoted_identifiers.len());
|
||||
result.quoted_identifiers.insert(token.to_ascii_lowercase(), identifier);
|
||||
result.text.push(' ');
|
||||
result.text.push_str(&token);
|
||||
result.text.push(' ');
|
||||
chars.len()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{is_production_database, targets_production_database};
|
||||
use crate::models::connection::{ConnectionConfig, DatabaseType};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct ProductionSafetyCorpusCase {
|
||||
name: String,
|
||||
dialect: DatabaseType,
|
||||
production_databases: Vec<String>,
|
||||
active_database: String,
|
||||
sql: String,
|
||||
active: bool,
|
||||
}
|
||||
|
||||
fn config() -> ConnectionConfig {
|
||||
ConnectionConfig {
|
||||
id: "conn".to_string(),
|
||||
name: "test".to_string(),
|
||||
db_type: DatabaseType::Mysql,
|
||||
driver_profile: None,
|
||||
driver_label: None,
|
||||
url_params: None,
|
||||
agent_java_options: vec![],
|
||||
host: "localhost".to_string(),
|
||||
port: 3306,
|
||||
username: "root".to_string(),
|
||||
password: String::new(),
|
||||
database: None,
|
||||
visible_databases: None,
|
||||
visible_schemas: None,
|
||||
attached_databases: vec![],
|
||||
color: None,
|
||||
transport_layers: vec![],
|
||||
connect_timeout_secs: 10,
|
||||
query_timeout_secs: 30,
|
||||
idle_timeout_secs: 60,
|
||||
keepalive_interval_secs: 30,
|
||||
ssl: false,
|
||||
ca_cert_path: String::new(),
|
||||
client_cert_path: String::new(),
|
||||
client_key_path: String::new(),
|
||||
sysdba: false,
|
||||
oracle_connection_type: None,
|
||||
connection_string: None,
|
||||
jdbc_driver_class: None,
|
||||
jdbc_driver_paths: vec![],
|
||||
redis_connection_mode: None,
|
||||
redis_sentinel_master: String::new(),
|
||||
redis_sentinel_nodes: String::new(),
|
||||
redis_sentinel_username: String::new(),
|
||||
redis_sentinel_password: String::new(),
|
||||
redis_sentinel_tls: false,
|
||||
redis_cluster_nodes: String::new(),
|
||||
redis_key_separator: ":".to_string(),
|
||||
redis_scan_page_size: Some(1000),
|
||||
etcd_endpoints: String::new(),
|
||||
gbase_server: String::new(),
|
||||
informix_server: String::new(),
|
||||
external_config: None,
|
||||
one_time: false,
|
||||
read_only: false,
|
||||
is_production: false,
|
||||
production_databases: vec!["prod_app".to_string()],
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn matches_marked_database_case_insensitively() {
|
||||
assert!(is_production_database(&config(), "`PROD_APP`"));
|
||||
assert!(!is_production_database(&config(), "staging"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn detects_cross_database_production_targets() {
|
||||
assert!(targets_production_database(&config(), "staging", "DELETE FROM prod_app.users WHERE id = 1"));
|
||||
assert!(targets_production_database(&config(), "staging", "USE prod_app; DELETE FROM users WHERE id = 1"));
|
||||
assert!(targets_production_database(&config(), "staging", "COPY prod_app.users FROM '/tmp/users.csv'"));
|
||||
assert!(targets_production_database(&config(), "staging", "DROP DATABASE IF EXISTS `prod_app`"));
|
||||
assert!(targets_production_database(&config(), "staging", "CALL prod_app.purge_users()"));
|
||||
assert!(targets_production_database(&config(), "staging", "CALL `prod_app`.`purge_users`()"));
|
||||
assert!(targets_production_database(&config(), "staging", "GRANT ALL ON prod_app.* TO 'u'@'%'"));
|
||||
assert!(targets_production_database(
|
||||
&config(),
|
||||
"staging",
|
||||
"GRANT EXECUTE ON PROCEDURE prod_app.purge_users TO 'u'@'%'"
|
||||
));
|
||||
assert!(!targets_production_database(&config(), "staging", "DELETE FROM staging.users WHERE id = 1"));
|
||||
assert!(!targets_production_database(&config(), "staging", "CALL staging.purge_users()"));
|
||||
assert!(!targets_production_database(&config(), "staging", "GRANT ALL ON staging.* TO 'u'@'%'"));
|
||||
assert!(!targets_production_database(
|
||||
&config(),
|
||||
"staging",
|
||||
"DELETE FROM staging.users WHERE note = 'FROM prod_app.users'"
|
||||
));
|
||||
assert!(!targets_production_database(
|
||||
&config(),
|
||||
"staging",
|
||||
"SELECT * FROM prod_app.users; DELETE FROM staging.users WHERE id = 1"
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn matches_shared_sql_target_safety_corpus() {
|
||||
let corpus: Vec<ProductionSafetyCorpusCase> =
|
||||
serde_json::from_str(include_str!("../../../tests/fixtures/production-safety-corpus.json"))
|
||||
.expect("production safety corpus is valid JSON");
|
||||
|
||||
for corpus_case in corpus {
|
||||
let mut config = config();
|
||||
config.db_type = corpus_case.dialect;
|
||||
config.production_databases = corpus_case.production_databases;
|
||||
assert_eq!(
|
||||
targets_production_database(&config, &corpus_case.active_database, &corpus_case.sql),
|
||||
corpus_case.active,
|
||||
"{}",
|
||||
corpus_case.name
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn conservatively_blocks_ambiguous_production_targets() {
|
||||
assert!(targets_production_database(&config(), "staging", "CALL purge_users()"));
|
||||
assert!(targets_production_database(&config(), "staging", "GRANT PROCESS ON *.* TO 'u'@'%'"));
|
||||
assert!(targets_production_database(&config(), "staging", "GRANT ALL ON users TO 'u'@'%'"));
|
||||
assert!(targets_production_database(&config(), "staging", "CREATE USER 'u'@'%'"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolves_sqlserver_database_qualifiers_dialect_aware() {
|
||||
let mut sqlserver = config();
|
||||
sqlserver.db_type = DatabaseType::SqlServer;
|
||||
|
||||
assert!(targets_production_database(&sqlserver, "staging", "DELETE FROM prod_app.dbo.users WHERE id = 1"));
|
||||
assert!(!targets_production_database(&sqlserver, "staging", "DELETE FROM prod_app.users WHERE id = 1"));
|
||||
}
|
||||
}
|
||||
@@ -3049,6 +3049,8 @@ mod tests {
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: false,
|
||||
is_production: false,
|
||||
production_databases: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3856,6 +3858,8 @@ mod tests {
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: false,
|
||||
is_production: false,
|
||||
production_databases: vec![],
|
||||
};
|
||||
|
||||
let params = external_driver_query_params(
|
||||
|
||||
@@ -201,11 +201,47 @@ pub fn is_write_sql(sql: &str) -> bool {
|
||||
return !is_safe_read_pragma(&upper);
|
||||
}
|
||||
|
||||
if starts_with_keyword(&upper, "SELECT") && select_contains_top_level_into(&upper) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// A statement is a write if it doesn't start with a read keyword,
|
||||
// or if it contains embedded dangerous keywords (e.g. CTE-wrapped writes like WITH ... AS (DELETE FROM ...))
|
||||
!starts_with_read || contains_dangerous_sql_keyword(sql)
|
||||
}
|
||||
|
||||
fn select_contains_top_level_into(upper: &str) -> bool {
|
||||
let mut token = String::new();
|
||||
let mut depth = 0usize;
|
||||
let mut saw_select = false;
|
||||
|
||||
for ch in upper.chars().chain(std::iter::once(' ')) {
|
||||
if ch.is_ascii_alphanumeric() || ch == '_' {
|
||||
token.push(ch);
|
||||
continue;
|
||||
}
|
||||
|
||||
if !token.is_empty() {
|
||||
if depth == 0 {
|
||||
if token == "SELECT" {
|
||||
saw_select = true;
|
||||
} else if saw_select && token == "INTO" {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
token.clear();
|
||||
}
|
||||
|
||||
if ch == '(' {
|
||||
depth += 1;
|
||||
} else if ch == ')' {
|
||||
depth = depth.saturating_sub(1);
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
/// Check if a PRAGMA statement is a safe read-only form.
|
||||
/// Allows: PRAGMA table_info(...), PRAGMA index_list(...), etc.
|
||||
/// Blocks: PRAGMA name = value, PRAGMA name(value), or unknown PRAGMA names.
|
||||
@@ -321,7 +357,13 @@ fn strip_sql_comments(sql: &str) -> String {
|
||||
}
|
||||
if ch == '/' && chars.peek() == Some(&'*') {
|
||||
chars.next();
|
||||
in_block_comment = true;
|
||||
if let Some(body) = read_mysql_executable_comment_body(&mut chars) {
|
||||
output.push(' ');
|
||||
output.push_str(&strip_sql_comments(&body));
|
||||
output.push(' ');
|
||||
} else {
|
||||
in_block_comment = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -392,7 +434,13 @@ pub fn strip_sql_comments_and_literals(sql: &str) -> String {
|
||||
}
|
||||
if ch == '/' && chars.peek() == Some(&'*') {
|
||||
chars.next();
|
||||
in_block_comment = true;
|
||||
if let Some(body) = read_mysql_executable_comment_body(&mut chars) {
|
||||
output.push(' ');
|
||||
output.push_str(&strip_sql_comments_and_literals(&body));
|
||||
output.push(' ');
|
||||
} else {
|
||||
in_block_comment = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if ch == '\'' {
|
||||
@@ -412,6 +460,39 @@ pub fn strip_sql_comments_and_literals(sql: &str) -> String {
|
||||
output
|
||||
}
|
||||
|
||||
fn read_mysql_executable_comment_body<I>(chars: &mut std::iter::Peekable<I>) -> Option<String>
|
||||
where
|
||||
I: Iterator<Item = char>,
|
||||
{
|
||||
let marker = chars.peek().copied()?;
|
||||
if marker == '!' {
|
||||
chars.next();
|
||||
} else if marker == 'M' {
|
||||
chars.next();
|
||||
if chars.peek() != Some(&'!') {
|
||||
return None;
|
||||
}
|
||||
chars.next();
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut body = String::new();
|
||||
let mut skipping_version = true;
|
||||
while let Some(ch) = chars.next() {
|
||||
if ch == '*' && chars.peek() == Some(&'/') {
|
||||
chars.next();
|
||||
return Some(body);
|
||||
}
|
||||
if skipping_version && (ch.is_ascii_digit() || ch.is_whitespace()) {
|
||||
continue;
|
||||
}
|
||||
skipping_version = false;
|
||||
body.push(ch);
|
||||
}
|
||||
Some(body)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -609,6 +690,11 @@ mod tests {
|
||||
assert!(is_write_sql("CREATE TABLE users (id INT)"));
|
||||
assert!(is_write_sql("ALTER TABLE users ADD COLUMN age INT"));
|
||||
assert!(is_write_sql("TRUNCATE TABLE users"));
|
||||
assert!(is_write_sql("EXPLAIN ANALYZE DELETE FROM users"));
|
||||
assert!(is_write_sql("SELECT * INTO backup_users FROM users"));
|
||||
assert!(is_write_sql("SELECT * FROM users INTO OUTFILE '/tmp/users.csv'"));
|
||||
assert!(is_write_sql("COPY users FROM '/tmp/users.csv'"));
|
||||
assert!(is_write_sql("/*! DELETE FROM users */"));
|
||||
assert!(is_write_sql(
|
||||
"MERGE INTO target USING source ON t.id = s.id WHEN MATCHED THEN UPDATE SET t.name = s.name"
|
||||
));
|
||||
|
||||
@@ -2352,6 +2352,8 @@ mod tests {
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: false,
|
||||
is_production: false,
|
||||
production_databases: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -188,6 +188,8 @@ mod tests {
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: false,
|
||||
is_production: false,
|
||||
production_databases: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlparser::ast::Statement;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlparser::ast::{Query, SetExpr, Statement};
|
||||
use sqlparser::dialect::{
|
||||
ClickHouseDialect, DuckDbDialect, GenericDialect, MsSqlDialect, MySqlDialect, PostgreSqlDialect, SQLiteDialect,
|
||||
};
|
||||
@@ -60,8 +60,20 @@ fn resolve_dialect(dialect: &str) -> Box<dyn sqlparser::dialect::Dialect> {
|
||||
fn classify_statement(stmt: &Statement) -> SqlRisk {
|
||||
match stmt {
|
||||
// Pure reads
|
||||
Statement::Query(_) => SqlRisk::ReadOnly,
|
||||
Statement::Explain { .. } => SqlRisk::ReadOnly,
|
||||
Statement::Query(query) => {
|
||||
if query_contains_select_into(query) {
|
||||
SqlRisk::Write
|
||||
} else {
|
||||
SqlRisk::ReadOnly
|
||||
}
|
||||
}
|
||||
Statement::Explain { analyze, statement, .. } => {
|
||||
if *analyze {
|
||||
classify_statement(statement)
|
||||
} else {
|
||||
SqlRisk::ReadOnly
|
||||
}
|
||||
}
|
||||
Statement::ExplainTable { .. } => SqlRisk::ReadOnly,
|
||||
|
||||
// Show/Describe variants
|
||||
@@ -101,10 +113,11 @@ fn classify_statement(stmt: &Statement) -> SqlRisk {
|
||||
SqlRisk::Transaction
|
||||
}
|
||||
|
||||
// Copy (PostgreSQL) 鈥?treat as write
|
||||
// COPY FROM mutates data; keep COPY conservative because sqlparser does
|
||||
// not expose enough dialect-specific direction detail here.
|
||||
Statement::Copy { .. } => SqlRisk::Write,
|
||||
|
||||
// Pragma (SQLite/DuckDB) 鈥?conservative: treat as write unless known-safe
|
||||
// SQLite/DuckDB PRAGMA statements can mutate database/session state.
|
||||
Statement::Pragma { .. } => SqlRisk::Write,
|
||||
|
||||
// Catch-all: conservative write classification
|
||||
@@ -112,6 +125,21 @@ fn classify_statement(stmt: &Statement) -> SqlRisk {
|
||||
}
|
||||
}
|
||||
|
||||
fn query_contains_select_into(query: &Query) -> bool {
|
||||
set_expr_contains_select_into(&query.body)
|
||||
}
|
||||
|
||||
fn set_expr_contains_select_into(expr: &SetExpr) -> bool {
|
||||
match expr {
|
||||
SetExpr::Select(select) => select.into.is_some(),
|
||||
SetExpr::Query(query) => query_contains_select_into(query),
|
||||
SetExpr::SetOperation { left, right, .. } => {
|
||||
set_expr_contains_select_into(left) || set_expr_contains_select_into(right)
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Classify SQL risk using sqlparser AST analysis.
|
||||
///
|
||||
/// If parsing fails (non-standard SQL, non-SQL databases), falls back to
|
||||
@@ -173,6 +201,13 @@ mod tests {
|
||||
assert_eq!(classify_sql_risk("INSERT INTO users VALUES (1)", "postgres").unwrap(), SqlRisk::Write);
|
||||
assert_eq!(classify_sql_risk("UPDATE users SET name = 'x'", "postgres").unwrap(), SqlRisk::Write);
|
||||
assert_eq!(classify_sql_risk("DELETE FROM users", "postgres").unwrap(), SqlRisk::Write);
|
||||
assert_eq!(classify_sql_risk("EXPLAIN ANALYZE DELETE FROM users", "postgres").unwrap(), SqlRisk::Write);
|
||||
assert_eq!(classify_sql_risk("SELECT * INTO backup_users FROM users", "postgres").unwrap(), SqlRisk::Write);
|
||||
assert_eq!(
|
||||
classify_sql_risk("SELECT * FROM users INTO OUTFILE '/tmp/users.csv'", "mysql").unwrap(),
|
||||
SqlRisk::Write
|
||||
);
|
||||
assert_eq!(classify_sql_risk("/*! DELETE FROM users */", "mysql").unwrap(), SqlRisk::Write);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -2461,6 +2461,8 @@ mod tests {
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: false,
|
||||
is_production: false,
|
||||
production_databases: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2519,6 +2521,8 @@ mod tests {
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: false,
|
||||
is_production: false,
|
||||
production_databases: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4680,6 +4680,8 @@ mod tests {
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: false,
|
||||
is_production: false,
|
||||
production_databases: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -61,6 +61,8 @@ fn live_postgres_config(
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: false,
|
||||
is_production: false,
|
||||
production_databases: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -55,6 +55,8 @@ fn postgres_test_config(id: &str, database: &str) -> ConnectionConfig {
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: false,
|
||||
is_production: false,
|
||||
production_databases: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -58,6 +58,8 @@ fn live_sqlserver_config(id: &str, database: &str) -> dbx_core::models::connecti
|
||||
jdbc_driver_paths: Vec::new(),
|
||||
one_time: false,
|
||||
read_only: false,
|
||||
is_production: false,
|
||||
production_databases: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user