feat(readonly): add read-only mode for all database connections
- Add read_only field to ConnectionConfig with backward-compatible serde defaults - SQL classifier with first-keyword + embedded keyword defense layers - PRAGMA white-listing for safe read-only operations - Guard all write entry points: SQL, Mongo, Redis, etcd, MCP Bridge - 16 unit tests covering edge cases (CTE, case insensitivity, string masking) - 7-language i18n support - Fast-fail at command/route level before pool creation Closes #889
This commit is contained in:
commit
bfec556e9b
533 files changed
+11710
-20243
No files matched your search
@@ -344,6 +344,7 @@ pub fn duckdb_execute_with_max_rows(
|
||||
Ok(db::QueryResult {
|
||||
columns,
|
||||
column_types: Vec::new(),
|
||||
column_sortables: vec![],
|
||||
rows: result_rows,
|
||||
affected_rows: 0,
|
||||
execution_time_ms: start.elapsed().as_millis(),
|
||||
@@ -356,6 +357,7 @@ pub fn duckdb_execute_with_max_rows(
|
||||
Ok(db::QueryResult {
|
||||
columns: vec![],
|
||||
column_types: Vec::new(),
|
||||
column_sortables: vec![],
|
||||
rows: vec![],
|
||||
affected_rows: affected as u64,
|
||||
execution_time_ms: start.elapsed().as_millis(),
|
||||
@@ -692,6 +694,17 @@ pub async fn do_execute(
|
||||
)
|
||||
.await
|
||||
}
|
||||
PoolKind::Turso(client) => {
|
||||
let client = client.clone();
|
||||
let max_rows = options.max_rows;
|
||||
drop(connections);
|
||||
wait_for_query_opt(
|
||||
cancel_token,
|
||||
query_timeout,
|
||||
db::turso_driver::execute_query_with_max_rows(&client, sql, max_rows),
|
||||
)
|
||||
.await
|
||||
}
|
||||
PoolKind::ClickHouse(client) => {
|
||||
let client = client.clone();
|
||||
let database = pool_key.split(':').nth(1).unwrap_or("default").to_string();
|
||||
@@ -971,6 +984,19 @@ pub async fn execute_multi_core_with_options(
|
||||
return execute_multi_sqlserver(state, &pool_key, sql, cancel_token, options).await;
|
||||
}
|
||||
|
||||
let is_turso = {
|
||||
let configs = state.configs.read().await;
|
||||
configs.get(connection_id).is_some_and(|c| c.db_type == DatabaseType::Turso)
|
||||
};
|
||||
|
||||
// Turso sends all statements in one HTTP pipeline for transactional integrity.
|
||||
if is_turso {
|
||||
let result =
|
||||
execute_sql_statement_with_options(state, connection_id, database, sql, schema, cancel_token, options)
|
||||
.await?;
|
||||
return Ok(vec![result]);
|
||||
}
|
||||
|
||||
let db_type = connection_database_type(state, connection_id).await;
|
||||
let statements = db_type.map_or_else(
|
||||
|| split_sql_statements(sql),
|
||||
@@ -1076,6 +1102,7 @@ fn error_query_result(message: String) -> db::QueryResult {
|
||||
db::QueryResult {
|
||||
columns: vec!["Error".to_string()],
|
||||
column_types: Vec::new(),
|
||||
column_sortables: vec![],
|
||||
rows: vec![vec![serde_json::Value::String(message)]],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 0,
|
||||
@@ -1104,6 +1131,7 @@ async fn execute_multi_sqlserver(
|
||||
all_results.push(db::QueryResult {
|
||||
columns: vec!["Error".to_string()],
|
||||
column_types: Vec::new(),
|
||||
column_sortables: vec![],
|
||||
rows: vec![vec![serde_json::Value::String(canceled_error())]],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 0,
|
||||
@@ -1137,6 +1165,7 @@ async fn execute_multi_sqlserver(
|
||||
all_results.push(db::QueryResult {
|
||||
columns: vec!["Error".to_string()],
|
||||
column_types: Vec::new(),
|
||||
column_sortables: vec![],
|
||||
rows: vec![vec![serde_json::Value::String(e)]],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 0,
|
||||
@@ -1152,6 +1181,7 @@ async fn execute_multi_sqlserver(
|
||||
all_results.push(db::QueryResult {
|
||||
columns: vec![],
|
||||
column_types: Vec::new(),
|
||||
column_sortables: vec![],
|
||||
rows: vec![],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 0,
|
||||
@@ -1216,6 +1246,7 @@ pub async fn execute_statements(
|
||||
Ok(db::QueryResult {
|
||||
columns: vec![],
|
||||
column_types: Vec::new(),
|
||||
column_sortables: vec![],
|
||||
rows: vec![],
|
||||
affected_rows: total_affected,
|
||||
execution_time_ms: start.elapsed().as_millis(),
|
||||
@@ -1257,9 +1288,11 @@ pub async fn execute_statements_in_transaction(
|
||||
PoolKind::Postgres(pg) => TxPath::Pg(pg.clone()),
|
||||
PoolKind::Mysql(mp, _mode) => TxPath::Mysql(mp.clone(), false),
|
||||
PoolKind::Sqlite(sq) => TxPath::Sqlite(sq.clone()),
|
||||
PoolKind::ClickHouse(_) | PoolKind::Rqlite(_) | PoolKind::SqlServer(_) | PoolKind::Agent(_) => {
|
||||
TxPath::Explicit
|
||||
}
|
||||
PoolKind::ClickHouse(_)
|
||||
| PoolKind::Rqlite(_)
|
||||
| PoolKind::Turso(_)
|
||||
| PoolKind::SqlServer(_)
|
||||
| PoolKind::Agent(_) => TxPath::Explicit,
|
||||
PoolKind::DuckDb(_)
|
||||
| PoolKind::Redis(_)
|
||||
| PoolKind::MongoDb(_)
|
||||
@@ -1322,6 +1355,7 @@ async fn exec_tx_pg_inner(
|
||||
Ok(total_affected) => Ok(db::QueryResult {
|
||||
columns: vec![],
|
||||
column_types: Vec::new(),
|
||||
column_sortables: vec![],
|
||||
rows: vec![],
|
||||
affected_rows: total_affected,
|
||||
execution_time_ms: start.elapsed().as_millis(),
|
||||
@@ -1370,6 +1404,7 @@ async fn exec_tx_mysql_inner(
|
||||
Ok(db::QueryResult {
|
||||
columns: vec![],
|
||||
column_types: Vec::new(),
|
||||
column_sortables: vec![],
|
||||
rows: vec![],
|
||||
affected_rows: total_affected,
|
||||
execution_time_ms: start.elapsed().as_millis(),
|
||||
@@ -1402,6 +1437,7 @@ async fn exec_tx_sqlite_inner(
|
||||
Ok(db::QueryResult {
|
||||
columns: vec![],
|
||||
column_types: Vec::new(),
|
||||
column_sortables: vec![],
|
||||
rows: vec![],
|
||||
affected_rows: total_affected,
|
||||
execution_time_ms: start.elapsed().as_millis(),
|
||||
@@ -1482,6 +1518,7 @@ async fn exec_tx_explicit_inner(
|
||||
Ok(db::QueryResult {
|
||||
columns: vec![],
|
||||
column_types: Vec::new(),
|
||||
column_sortables: vec![],
|
||||
rows: vec![],
|
||||
affected_rows: total_affected,
|
||||
execution_time_ms: start.elapsed().as_millis(),
|
||||
@@ -1524,6 +1561,7 @@ async fn exec_tx_none_inner(
|
||||
Ok(db::QueryResult {
|
||||
columns: vec![],
|
||||
column_types: Vec::new(),
|
||||
column_sortables: vec![],
|
||||
rows: vec![],
|
||||
affected_rows: total_affected,
|
||||
execution_time_ms: start.elapsed().as_millis(),
|
||||
@@ -1548,6 +1586,7 @@ mod tests {
|
||||
Ok(db::QueryResult {
|
||||
columns: vec![],
|
||||
column_types: Vec::new(),
|
||||
column_sortables: vec![],
|
||||
rows: vec![],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 0,
|
||||
@@ -1568,6 +1607,7 @@ mod tests {
|
||||
Ok(db::QueryResult {
|
||||
columns: vec![],
|
||||
column_types: Vec::new(),
|
||||
column_sortables: vec![],
|
||||
rows: vec![],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 0,
|
||||
@@ -1912,6 +1952,7 @@ mod tests {
|
||||
let result = db::QueryResult {
|
||||
columns: vec!["id".to_string(), "nested".to_string()],
|
||||
column_types: Vec::new(),
|
||||
column_sortables: vec![],
|
||||
rows: vec![vec![
|
||||
serde_json::json!(2_041_797_190_226_354_178_i64),
|
||||
serde_json::json!([1, 2_041_797_190_226_354_178_i64]),
|
||||
|
||||
Reference in new issue
Block a user