Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions apps/tauri/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apps/tauri/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ sqlx = { version = "0.8", default-features = false, features = [
futures-util = "0.3"
chrono = { version = "0.4", default-features = false, features = ["clock", "std"] }
keyring = { version = "3", features = ["apple-native", "windows-native", "linux-native-sync-persistent"] }
sqlparser = "0.64"

[profile.release]
codegen-units = 1
Expand Down
15 changes: 15 additions & 0 deletions apps/tauri/src-tauri/src/connection/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub enum ConnectionError {
UnsupportedDriver { driver: DriverKind },
Io { message: String },
Driver { driver: DriverKind, message: String },
/// sqlparser / product SQL guard refusal (`rule` is a stable id).
SqlGuard { rule: String, message: String },
}

impl ConnectionError {
Expand All @@ -34,6 +36,16 @@ impl ConnectionError {
message: message.into(),
}
}

pub fn sql_guard(
rule: super::sql_guard::SqlGuardRule,
message: impl Into<String>,
) -> Self {
Self::SqlGuard {
rule: rule.as_str().to_owned(),
message: message.into(),
}
}
}

impl fmt::Display for ConnectionError {
Expand All @@ -50,6 +62,9 @@ impl fmt::Display for ConnectionError {
Self::Driver { driver, message } => {
write!(f, "{driver} error: {message}")
}
Self::SqlGuard { rule, message } => {
write!(f, "sql guard ({rule}): {message}")
}
}
}
}
Expand Down
42 changes: 37 additions & 5 deletions apps/tauri/src-tauri/src/connection/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,16 @@ impl ConnectionRegistry {
limit: Option<usize>,
emit: impl FnMut(QueryEvent) -> ConnectionResult<()>,
) -> ConnectionResult<()> {
match self.driver_of(id) {
Some(DriverKind::Sqlite) => self.execute_sqlite_query(id, sql, limit, emit),
Some(DriverKind::Postgres) => {
let driver = self
.driver_of(id)
.ok_or_else(|| ConnectionError::NotFound { id: id.clone() })?;
super::sql_guard::assert_single_statement(sql, driver)?;
match driver {
DriverKind::Sqlite => self.execute_sqlite_query(id, sql, limit, emit),
DriverKind::Postgres => {
self.with_postgres(id, |s| s.execute_streaming(sql, limit, emit))
}
Some(other) => Err(ConnectionError::UnsupportedDriver { driver: other }),
None => Err(ConnectionError::NotFound { id: id.clone() }),
other => Err(ConnectionError::UnsupportedDriver { driver: other }),
}
}

Expand Down Expand Up @@ -407,6 +410,35 @@ mod tests {
assert!(matches!(err, ConnectionError::InvalidConfig { .. }));
}

#[test]
fn execute_query_rejects_multi_statement() {
let path = std::env::temp_dir().join(format!(
"querya_reg_guard_{}.db",
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
let _ = rusqlite::Connection::open(&path).unwrap();
let reg = ConnectionRegistry::with_builtin_drivers();
let id = reg
.connect(ConnectionConfig::Sqlite {
path: path.to_string_lossy().into(),
id: None,
})
.unwrap();
let err = reg
.execute_query(&id, "SELECT 1; SELECT 2", None, |_| Ok(()))
.unwrap_err();
match err {
ConnectionError::SqlGuard { rule, .. } => {
assert_eq!(rule, "single_statement");
}
other => panic!("expected SqlGuard, got {other:?}"),
}
let _ = std::fs::remove_file(&path);
}

#[test]
fn execute_sqlite_query_streams_via_session() {
let path = std::env::temp_dir().join(format!(
Expand Down
1 change: 1 addition & 0 deletions apps/tauri/src-tauri/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub(crate) mod postgres_query;
mod query;
mod runtime;
mod smoke_slice;
mod sql_guard;
mod sqlite;
mod sqlite_introspect;
mod sqlite_object_tree;
Expand Down
Loading
Loading