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
@@ -272,6 +272,8 @@ pub enum DatabaseType {
|
||||
Etcd,
|
||||
#[serde(rename = "iris")]
|
||||
Iris,
|
||||
#[serde(rename = "turso")]
|
||||
Turso,
|
||||
Jdbc,
|
||||
}
|
||||
|
||||
@@ -559,7 +561,7 @@ impl ConnectionConfig {
|
||||
},
|
||||
DatabaseType::Redshift => Some("dev"),
|
||||
DatabaseType::ClickHouse => Some("default"),
|
||||
DatabaseType::Rqlite => Some("main"),
|
||||
DatabaseType::Rqlite | DatabaseType::Turso => Some("main"),
|
||||
DatabaseType::Gaussdb | DatabaseType::OpenGauss => Some("postgres"),
|
||||
DatabaseType::Kwdb => Some("defaultdb"),
|
||||
DatabaseType::Kingbase | DatabaseType::Vastbase => Some("postgres"),
|
||||
@@ -647,6 +649,7 @@ impl ConnectionConfig {
|
||||
}
|
||||
DatabaseType::ClickHouse => clickhouse_http_url(self, raw_host, port),
|
||||
DatabaseType::Rqlite => rqlite_http_url(self, raw_host, port),
|
||||
DatabaseType::Turso => turso_http_url(self, raw_host, port),
|
||||
DatabaseType::SqlServer => {
|
||||
format!("server=tcp:{host},{port};database={}", self.database.as_deref().unwrap_or("master"))
|
||||
}
|
||||
@@ -763,6 +766,7 @@ impl ConnectionConfig {
|
||||
}
|
||||
DatabaseType::ClickHouse => clickhouse_http_url(self, raw_host, port),
|
||||
DatabaseType::Rqlite => rqlite_http_url(self, raw_host, port),
|
||||
DatabaseType::Turso => turso_http_url(self, raw_host, port),
|
||||
DatabaseType::SqlServer => format!(
|
||||
"server=tcp:{host},{port};user={};password={};database={}",
|
||||
self.username,
|
||||
@@ -1140,6 +1144,19 @@ fn rqlite_http_url(config: &ConnectionConfig, host: &str, port: u16) -> String {
|
||||
format!("{scheme}://{}:{port}", bracket_ipv6(trimmed))
|
||||
}
|
||||
|
||||
fn turso_http_url(config: &ConnectionConfig, host: &str, port: u16) -> String {
|
||||
let trimmed = host.trim();
|
||||
if let Some(rest) = trimmed.strip_prefix("https://").or_else(|| trimmed.strip_prefix("libsql://")) {
|
||||
return format!("https://{}", trim_http_host_port(rest, port));
|
||||
}
|
||||
if let Some(rest) = trimmed.strip_prefix("http://") {
|
||||
let scheme = if config.ssl { "https" } else { "http" };
|
||||
return format!("{scheme}://{}", trim_http_host_port(rest, port));
|
||||
}
|
||||
let scheme = if config.ssl { "https" } else { "http" };
|
||||
format!("{scheme}://{}:{port}", bracket_ipv6(trimmed))
|
||||
}
|
||||
|
||||
fn trim_http_host_port(value: &str, default_port: u16) -> String {
|
||||
let authority = value.trim_end_matches('/').split('/').next().unwrap_or(value).split('?').next().unwrap_or(value);
|
||||
if authority.starts_with('[') && !authority.contains("]:") {
|
||||
@@ -1691,7 +1708,7 @@ mod tests {
|
||||
|
||||
assert_eq!(
|
||||
config.connection_url(),
|
||||
"mysql://root:secret@gateway01.us-west-2.prod.aws.tidbcloud.com:4000/test?require_ssl=true&verify_ca=false&verify_identity=false&charset=utf8mb4"
|
||||
"mysql://root:secret@gateway01.us-west-2.prod.aws.tidbcloud.com:4000/test?require_ssl=true&charset=utf8mb4&verify_ca=false&verify_identity=false"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user