Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(databases): reject unavailable SQL database scopes (#7611)

Wolf Mermelstein5e1c7eeed893
diff --git a/crates/databases_sql/src/service.rs b/crates/databases_sql/src/service.rs
index 2ce37b1c49..02a9d14eb3 100644
--- a/crates/databases_sql/src/service.rs
+++ b/crates/databases_sql/src/service.rs
@@ -225,11 +225,6 @@ where
) -> Result<SavedQuery, SqlError> {
let sql = definition.sql();
let catalog = self.catalog(&viewer, database_id).await?;
- if let Some(database_id) = database_id
- && !catalog.has_database(database_id)
- {
- return Err(SqlError::NotFound);
- }
let Query::Select(select) = database_sql::compile(catalog.catalog(), sql)? else {
return Err(SqlError::SavedQueryNotSelect);
};
@@ -343,7 +338,13 @@ where
SqlError::Infrastructure(rootcause::Report::new(other).into_dynamic())
}
})?;
- Ok(ViewerCatalog::new(databases, scope))
+ let catalog = ViewerCatalog::new(databases, scope);
+ if let Some(database_id) = scope
+ && !catalog.has_database(database_id)
+ {
+ return Err(SqlError::NotFound);
+ }
+ Ok(catalog)
}
}
diff --git a/crates/databases_sql/src/service/test.rs b/crates/databases_sql/src/service/test.rs
index 59f7bfb396..06c0dae0e4 100644
--- a/crates/databases_sql/src/service/test.rs
+++ b/crates/databases_sql/src/service/test.rs
@@ -1279,3 +1279,5 @@ async fn schema_version_conflicts_keep_the_retry_signal() {
}
}
}
+
+mod scopes;
diff --git a/crates/databases_sql/src/service/test/scopes.rs b/crates/databases_sql/src/service/test/scopes.rs
new file mode 100644
index 0000000000..1539508da5
--- /dev/null
+++ b/crates/databases_sql/src/service/test/scopes.rs
@@ -0,0 +1,102 @@
+//! An explicitly selected database must remain visible before SQL resolves.
+
+use super::*;
+
+#[tokio::test]
+async fn an_unavailable_scope_does_not_read_another_accessible_database() {
+ for scope in [DatabaseId::new(), SECRET] {
+ let world = world();
+ let result = sql(&world)
+ .execute(
+ agent_for(OWNER),
+ SqlRequest {
+ sql: "SELECT \"Name\" FROM \"Guests\"".into(),
+ scope: Some(scope),
+ base_versions: HashMap::new(),
+ },
+ )
+ .await;
+
+ assert!(matches!(result, Err(SqlError::NotFound)), "{result:?}");
+ assert!(world.lock().unwrap().soup_reads.is_empty());
+ }
+}
+
+#[tokio::test]
+async fn an_unavailable_scope_does_not_update_another_accessible_database() {
+ for scope in [DatabaseId::new(), SECRET] {
+ let world = world();
+ world
+ .lock()
+ .unwrap()
+ .op_answers
+ .push_back(Ok(vec![OpResult::Rows {
+ table: GUESTS,
+ table_version: TableVersion(2),
+ change: RowsResult::Updated { affected: 1 },
+ }]));
+ let result = sql(&world)
+ .execute(
+ agent_for(OWNER),
+ SqlRequest {
+ sql:
+ "UPDATE \"Guests\" SET \"Name\" = 'Wrong database' WHERE \"Name\" = 'Maria'"
+ .into(),
+ scope: Some(scope),
+ base_versions: HashMap::new(),
+ },
+ )
+ .await;
+
+ assert!(matches!(result, Err(SqlError::NotFound)), "{result:?}");
+ let world = world.lock().unwrap();
+ assert!(world.applied.is_empty());
+ assert!(world.soup_reads.is_empty());
+ }
+}
+
+#[tokio::test]
+async fn an_unavailable_scope_does_not_rename_another_accessible_database() {
+ for scope in [DatabaseId::new(), SECRET] {
+ let world = world();
+ let result = sql(&world)
+ .execute(
+ agent_for(OWNER),
+ SqlRequest {
+ sql: "ALTER DATABASE Offsite RENAME TO Wrong".into(),
+ scope: Some(scope),
+ base_versions: HashMap::new(),
+ },
+ )
+ .await;
+
+ assert!(matches!(result, Err(SqlError::NotFound)), "{result:?}");
+ assert_eq!(world.lock().unwrap().databases[0].database.name, "Offsite");
+ }
+}
+
+#[tokio::test]
+async fn an_available_scope_keeps_other_accessible_databases_available_for_joins() {
+ let world = world();
+ let outcome = sql(&world)
+ .execute(
+ agent_for(OWNER),
+ SqlRequest {
+ sql: "SELECT g.\"Name\", h.\"Name\" AS hall FROM \"Guests\" g \
+ JOIN \"Venues\".\"Halls\" h ON g.\"Hall\" = h.row_id"
+ .into(),
+ scope: Some(OFFSITE),
+ base_versions: HashMap::new(),
+ },
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(
+ outcome.result.unwrap().rows,
+ vec![vec![
+ Some(Cell::Text("Maria".into())),
+ Some(Cell::Text("Ballroom".into())),
+ ]]
+ );
+}
diff --git a/docs/AGENT_GUIDE/databases.md b/docs/AGENT_GUIDE/databases.md
index cd49bd7c6c..50fb9ddf9d 100644
--- a/docs/AGENT_GUIDE/databases.md
+++ b/docs/AGENT_GUIDE/databases.md
@@ -378,6 +378,9 @@ has six database tools: `ListDatabases`, `DescribeDatabase`, `QueryDatabase`,
`QueryDatabase` reads and changes rows and handles schema changes through SQL
(`CREATE`, `ALTER`, and `DROP`). The assistant reads the current schema before
editing and checks actual results before reporting success.
+A supplied `databaseId` must refer to an accessible database before rows or
+schema changes resolve. Valid selections still allow qualified joins across
+accessible databases.
Query tool rows say what the query did in words (**Read Invites**, **Updated 3
rows in Guests**, **Changed Price to number**, or **Queried Party Planner**) and