The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Refuse more side effects in Metabot's read-only SQL check (#84492)
Chris Trutera1ef50381407
diff --git a/resources/python-sources/sql_tools.py b/resources/python-sources/sql_tools.pyindex 7d9202d6a67..77ea22b4788 100644--- a/resources/python-sources/sql_tools.py+++ b/resources/python-sources/sql_tools.py@@ -1872,13 +1872,22 @@ _NOT_READ_ONLY_NODES = (exp.Pragma, exp.Cache, exp.Uncache, exp.Refresh, exp.Analyze, exp.NextValueFor, exp.QueryTransform,)-# Advancing a sequence is a write a SELECT can make. sqlglot parses only the SQL-standard `NEXT VALUE FOR` as its-# own node; Postgres and Redshift spell it as a function, Oracle and Snowflake as a `.NEXTVAL` pseudo-column, and-# Snowflake also as the table function `TABLE(GETNEXTVAL(seq))`.-_SEQUENCE_WRITE_FUNCTIONS = frozenset({"nextval", "setval", "getnextval"})+# Writes a SELECT can make through a built-in function. Advancing a sequence is one: sqlglot parses only the+# SQL-standard `NEXT VALUE FOR` as its own node; Postgres and Redshift spell it as a function, Oracle and Snowflake as+# a `.NEXTVAL` pseudo-column, and Snowflake also as the table function `TABLE(GETNEXTVAL(seq))`. Postgres large objects+# are another, and any role can create one by default.+_WRITE_FUNCTIONS = frozenset(+ {+ # sequences+ "nextval", "setval", "getnextval",+ # Postgres large objects+ "lo_creat", "lo_create", "lo_from_bytea", "lo_put", "lowrite", "lo_truncate", "lo_truncate64", "lo_unlink",+ "lo_import", "lo_export",+ }+)# Built-in functions that reach past the query that calls them: they take a lock that can outlive it, change the-# session's settings, signal or stop another session, or hold the connection waiting.+# session's settings, signal or stop another session, hold the connection waiting, or act outside the database._SESSION_EFFECT_FUNCTIONS = frozenset({# Postgres advisory locks@@ -1890,8 +1899,15 @@ _SESSION_EFFECT_FUNCTIONS = frozenset("get_lock", "release_lock", "release_all_locks",# session settings and signals"set_config", "pg_notify", "pg_terminate_backend", "pg_cancel_backend", "pg_reload_conf",+ # Postgres dblink: a named connection stays open on the session+ "dblink_connect", "dblink_connect_u",+ # Snowflake: stop another session's work+ "system$cancel_query", "system$cancel_all_queries", "system$abort_session", "system$abort_transaction",# waiting- "pg_sleep", "pg_sleep_for", "pg_sleep_until", "sleep", "benchmark",+ "pg_sleep", "pg_sleep_for", "pg_sleep_until", "sleep", "benchmark", "sleepeachrow", "system$wait",+ "master_pos_wait", "source_pos_wait", "wait_for_executed_gtid_set", "wait_until_sql_thread_after_gtids",+ # running a program, Java code or a request outside the database+ "executable", "reflect", "java_method", "http_request", "system$send_email",})@@ -1946,6 +1962,14 @@ def _sql_executing_function(stmt: exp.Expression):name = _function_name(node)if name in _SQL_EXECUTING_FUNCTIONS:return name+ # Trino passes a query through to a connector's database with `TABLE(catalog.system.query(query => '...'))`.+ # The name `query` alone is too common to refuse, so it counts only under a `system` schema.+ if (+ isinstance(node, exp.Dot)+ and node.this.name.lower() == "system"+ and _function_name(node.expression) == "query"+ ):+ return "system.query"return None@@ -1960,11 +1984,12 @@ def _writes_or_locks(node: exp.Expression) -> bool:if isinstance(node, _NOT_READ_ONLY_NODES):return Trueif isinstance(node, exp.Anonymous):- return node.name.lower() in _SEQUENCE_WRITE_FUNCTIONS+ return node.name.lower() in _WRITE_FUNCTIONS# MySQL assigns a user variable inside a SELECT with `@x := ...`, and the value outlives the query on a pooled# connection. A bare name on the left is a named argument instead, as in DuckDB and Postgres, and changes nothing.+ # The variable is searched for, not just checked for on top: a dotted name, `@x.y`, parses as a `Dot` around it.if isinstance(node, exp.PropertyEQ):- return isinstance(node.this, exp.Parameter)+ return node.this.find(exp.Parameter) is not Noneif isinstance(node, exp.Column):return bool(node.table) and node.name.lower() == "nextval"if isinstance(node, exp.WithTableHint):diff --git a/src/metabase/metabot/tools/run_query.clj b/src/metabase/metabot/tools/run_query.cljindex 913c9d948ea..e9b73fbcdfe 100644--- a/src/metabase/metabot/tools/run_query.clj+++ b/src/metabase/metabot/tools/run_query.clj@@ -328,7 +328,8 @@:multiple-statements "It holds more than one statement.":not-a-select "It is not a SELECT.":writes-or-locks "It writes, takes a lock, or advances a sequence."- :session-function (str "It calls " detail ", which takes a lock, waits, or changes the session.")+ :session-function (str "It calls " detail ", which takes a lock, waits, changes the session, or acts outside"+ " the database."):sql-executing-function (str "It calls " detail ", which runs SQL passed to it as text that this check cannot"" see into."):statement-word (str "It uses the word " detail " outside quotes, which starts a new statement on"diff --git a/test/metabase/metabot/tools/run_query_test.clj b/test/metabase/metabot/tools/run_query_test.cljindex 52258a4662e..1bd032cafdd 100644--- a/test/metabase/metabot/tools/run_query_test.clj+++ b/test/metabase/metabot/tools/run_query_test.clj@@ -565,7 +565,7 @@"It holds a U&\"...\" name, whose escapes this check cannot read. Write the name plainly.":postgres "SELECT U&\"pg_\\0073leep\"(10)"- "It calls pg_advisory_lock, which takes a lock, waits, or changes the session."+ "It calls pg_advisory_lock, which takes a lock, waits, changes the session, or acts outside the database.":postgres "SELECT pg_advisory_lock(42)""It calls query_to_xml, which runs SQL passed to it as text that this check cannot see into."diff --git a/test/metabase/sql_tools/core_test.clj b/test/metabase/sql_tools/core_test.cljindex 3ac3efabf68..5ed3cf22e1d 100644--- a/test/metabase/sql_tools/core_test.clj+++ b/test/metabase/sql_tools/core_test.clj@@ -362,22 +362,48 @@:postgres "SELECT set_config('search_path', 'x', false)":postgres "SELECT pg_terminate_backend(pid) FROM pg_stat_activity":postgres "SELECT 1 WHERE pg_sleep(10) IS NULL"+ :postgres "SELECT dblink_connect('c', 'dbname=x')":mysql "SELECT GET_LOCK('x', 10)":mysql "SELECT RELEASE_ALL_LOCKS()":mysql "SELECT SLEEP(10)"- :mysql "SELECT BENCHMARK(1000000, MD5('x'))"))+ :mysql "SELECT BENCHMARK(1000000, MD5('x'))"+ :mysql "SELECT SOURCE_POS_WAIT('binlog.000001', 4, 10)"))(testing "a SELECT that assigns a user variable changes the session, so it is not read-only"(are [driver sql] (false? (sql-tools/read-only-select? driver sql)):mysql "SELECT @x := 1"- :mysql "SELECT @total := @total + a FROM t"))+ :mysql "SELECT @total := @total + a FROM t"+ :mysql "SELECT @x.y := 1"+ :mysql "(SELECT @x.y := 1)"))(testing "reading a sequence or a user variable, hinting a plain read, or naming a function argument is read-only"(are [driver sql] (true? (sql-tools/read-only-select? driver sql)):sqlserver "SELECT * FROM t WITH (NOLOCK)":sqlserver "SELECT * FROM dbo.fn(1)":postgres "SELECT currval('seq')"+ :postgres "SELECT lo_get(16384)":mysql "SELECT a FROM t WHERE b = @x":postgres "SELECT make_interval(days := 1)")))+(deftest ^:parallel read-only-select?-side-effect-function-test+ (testing "a SELECT that writes a Postgres large object is not read-only"+ (are [driver sql] (false? (sql-tools/read-only-select? driver sql))+ :postgres "SELECT lo_from_bytea(0, 'x')"+ :postgres "SELECT lo_put(16384, 0, 'x')"+ :postgres "SELECT lo_unlink(16384)"+ :postgres "SELECT lo_export(16384, '/tmp/x')"))+ (testing "a SELECT that waits or stops another session's work is not read-only"+ (are [driver sql] (false? (sql-tools/read-only-select? driver sql))+ :clickhouse "SELECT sleepEachRow(1) FROM numbers(100)"+ :snowflake "SELECT SYSTEM$WAIT(10)"+ :snowflake "SELECT SYSTEM$CANCEL_QUERY('01a2b3c4-0000-0000-0000-000000000000')"+ :snowflake "SELECT SYSTEM$ABORT_SESSION(1234)"))+ (testing "a SELECT that runs a program, Java code or a request outside the database is not read-only"+ (are [driver sql] (false? (sql-tools/read-only-select? driver sql))+ :clickhouse "SELECT * FROM executable('script.sh', CSV, 'a Int32')"+ :sparksql "SELECT reflect('java.lang.Thread', 'sleep', 10000L)"+ :databricks "SELECT java_method('java.lang.Thread', 'sleep', 10000L)"+ :databricks "SELECT http_request(conn => 'c', method => 'POST', path => '/')"+ :snowflake "SELECT SYSTEM$SEND_EMAIL('i', '[email protected]', 's', 'b')")))+(deftest ^:parallel read-only-select?-sql-executing-function-test(testing "a SELECT that calls a function taking a SQL string to run is not read-only, whatever the string holds";; The parser reads the inner SQL as an opaque literal, so a write, a lock or a session change hidden in it@@ -394,11 +420,15 @@:sqlserver "SELECT * FROM OPENQUERY(loop, 'DELETE FROM t')":sqlserver "SELECT * FROM OPENROWSET('P', 's', 'DELETE FROM t')":bigquery "SELECT * FROM EXTERNAL_QUERY('c', 'DELETE FROM t')"- :snowflake "SELECT * FROM TABLE(TO_QUERY('SELECT seq.NEXTVAL'))"))+ :snowflake "SELECT * FROM TABLE(TO_QUERY('SELECT seq.NEXTVAL'))"+ :starburst "SELECT * FROM TABLE(system.query(query => 'SELECT pg_advisory_lock(1)'))"+ :starburst "SELECT * FROM TABLE(pg.system.query(query => 'DELETE FROM t'))"+ :athena "SELECT * FROM TABLE(system.query(query => 'DELETE FROM t'))"))(testing "an ordinary read is still read-only"(are [driver sql] (true? (sql-tools/read-only-select? driver sql))- :postgres "SELECT id, name FROM t WHERE id = 5"- :postgres "SELECT * FROM generate_series(1, 10) g")))+ :postgres "SELECT id, name FROM t WHERE id = 5"+ :postgres "SELECT * FROM generate_series(1, 10) g"+ :starburst "SELECT query FROM system.runtime.queries")))(deftest ^:parallel read-only-select?-hidden-sql-test(testing "SQL the database would run and the parser reads as something else is not read-only"@@ -441,6 +471,7 @@:postgres :session-function "pg_advisory_lock" "SELECT pg_advisory_lock(42)":mysql :session-function "get_lock" "SELECT GET_LOCK('x', 10)":postgres :sql-executing-function "query_to_xml" "SELECT query_to_xml('SELECT pg_advisory_lock(1)', true, false, '')"+ :starburst :sql-executing-function "system.query" "SELECT * FROM TABLE(system.query(query => 'SELECT 1'))":postgres :unparseable nil "SELECT (":sqlserver :statement-word "EXEC" "SELECT 1 FROM t exec('DROP TABLE x')":mysql :executable-comment nil "SELECT * FROM t /*! FOR UPDATE */"