Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Refuse IDENTIFIER calls in Metabot's read-only SQL check (#85605)

Chris Truterad0af5b6b036
diff --git a/resources/python-sources/sql_tools.py b/resources/python-sources/sql_tools.py
index 5ab4309052b..46055edc5b5 100644
--- a/resources/python-sources/sql_tools.py
+++ b/resources/python-sources/sql_tools.py
@@ -1981,6 +1981,14 @@ def _sql_executing_function(stmt: exp.Expression):
return None
+def _dynamic_function_call(stmt: exp.Expression) -> bool:
+ """Whether `stmt` calls a function named by Snowflake's `IDENTIFIER(...)`, as in `IDENTIFIER('f')(1)`."""
+ # Name checks never see this name, a string or a variable, so the call is refused whatever it names.
+ # Only the call form has an `expressions` arg, which holds the call's arguments, possibly none.
+ # A table or column name such as `IDENTIFIER('t')` has no such arg.
+ return any("expressions" in node.args for node in stmt.find_all(exp.DynamicIdentifier))
+
+
# SQL Server table hints that take update or exclusive locks, or hold shared ones to the end of the transaction.
# `NOLOCK`, `INDEX(...)` and the rest only steer how a plain read runs.
_LOCKING_TABLE_HINTS = frozenset(
@@ -2091,6 +2099,8 @@ def _not_read_only_reason(stmts, sql: str, dialect: str):
executor = _sql_executing_function(stmts[0])
if executor is not None:
return ("sql-executing-function", executor)
+ if _dynamic_function_call(stmts[0]):
+ return ("dynamic-function-call", None)
return _hidden_sql(sql, dialect)
diff --git a/src/metabase/metabot/tools/run_query.clj b/src/metabase/metabot/tools/run_query.clj
index db61af1a557..2b14cc5599f 100644
--- a/src/metabase/metabot/tools/run_query.clj
+++ b/src/metabase/metabot/tools/run_query.clj
@@ -397,6 +397,8 @@
" the database.")
:sql-executing-function (str "It calls " detail ", which runs SQL passed to it as text that this check cannot"
" see into.")
+ :dynamic-function-call (str "It calls a function through IDENTIFIER(...), whose name this check cannot read."
+ " Call the function by its name.")
:statement-word (str "It uses the word " detail " outside quotes, which starts a new statement on"
" SQL Server.")
:executable-comment "It holds a /*! or /*M! comment, which MySQL and MariaDB run as SQL."
diff --git a/src/metabase/sql_tools/core.clj b/src/metabase/sql_tools/core.clj
index 4164b526bb9..364cb551a72 100644
--- a/src/metabase/sql_tools/core.clj
+++ b/src/metabase/sql_tools/core.clj
@@ -264,7 +264,9 @@
Comments and a trailing semicolon are allowed.
SQL that does not parse in `driver`'s dialect is not read-only, and neither is SQL too long to check or holding a
literal list too large to parse whole.
- A function this does not name, a user-defined one included, passes: only the database's own permissions stop it."
+ A function this does not name, a user-defined one included, passes when called by its name.
+ Only the database's own permissions stop it.
+ A call through Snowflake's `IDENTIFIER(...)` is refused whatever it names."
[driver :- :keyword
sql :- :string]
(= :allowed (:verdict (classify-stmt driver sql "read-only"))))
@@ -272,8 +274,9 @@
(mu/defn read-only-select-problem :- [:maybe [:map [:reason :keyword] [:detail [:maybe :string]]]]
"Why `sql` is not a read-only select ([[read-only-select?]]), or nil when it is one.
The `:reason` is one of `:multiple-statements`, `:not-a-select`, `:writes-or-locks`, `:statement-word`,
- `:session-function`, `:sql-executing-function`, `:executable-comment`, `:bare-dash-comment`, `:backslash-quote`,
- `:unicode-identifier`, `:large-literal-list`, `:too-long` and `:unparseable`.
+ `:session-function`, `:sql-executing-function`, `:dynamic-function-call`, `:executable-comment`,
+ `:bare-dash-comment`, `:backslash-quote`, `:unicode-identifier`, `:large-literal-list`, `:too-long` and
+ `:unparseable`.
For `:statement-word`, `:detail` is the word, and for `:session-function` and `:sql-executing-function` it is the
function's name."
[driver :- :keyword
diff --git a/test/metabase/metabot/tools/run_query_test.clj b/test/metabase/metabot/tools/run_query_test.clj
index 5de14898927..aa5b83777d0 100644
--- a/test/metabase/metabot/tools/run_query_test.clj
+++ b/test/metabase/metabot/tools/run_query_test.clj
@@ -739,6 +739,9 @@
"It calls query_to_xml, which runs SQL passed to it as text that this check cannot see into."
:postgres "SELECT query_to_xml('SELECT pg_advisory_lock(1)', true, false, '')"
+ "It calls a function through IDENTIFIER(...), whose name this check cannot read. Call the function by its name."
+ :snowflake "SELECT IDENTIFIER('f')(1)"
+
"It is too long to check. Shorten it, for example with a subquery in place of a long list."
:postgres (str "SELECT 1 FROM t WHERE x IN (" (str/join ", " (repeat 20000 "NULL")) ")")
diff --git a/test/metabase/sql_tools/core_test.clj b/test/metabase/sql_tools/core_test.clj
index d0ec054093b..e28a30c47c8 100644
--- a/test/metabase/sql_tools/core_test.clj
+++ b/test/metabase/sql_tools/core_test.clj
@@ -493,6 +493,21 @@
:postgres "SELECT * FROM generate_series(1, 10) g"
:starburst "SELECT query FROM system.runtime.queries")))
+(deftest ^:parallel read-only-select?-dynamic-function-call-test
+ (testing "a SELECT that calls a function named through Snowflake's IDENTIFIER is not read-only, whatever it names"
+ (are [driver sql] (false? (sql-tools/read-only-select? driver sql))
+ :snowflake "SELECT IDENTIFIER('SYSTEM$CANCEL_QUERY')('01b2')"
+ :snowflake "SELECT IDENTIFIER('SYSTEM$ABORT_SESSION')()"
+ :snowflake "SELECT * FROM TABLE(IDENTIFIER('TO_QUERY')('SELECT seq.NEXTVAL'))"
+ :snowflake "SELECT * FROM TABLE(IDENTIFIER('GETNEXTVAL')(seq))"
+ :snowflake "SELECT a FROM t WHERE b IN (SELECT IDENTIFIER($fn)(1))"
+ :snowflake "SELECT IDENTIFIER('my_udf')(a) FROM t"))
+ (testing "IDENTIFIER used as a table or column name is still read-only"
+ (are [driver sql] (true? (sql-tools/read-only-select? driver sql))
+ :snowflake "SELECT * FROM IDENTIFIER('db.schema.t')"
+ :snowflake "SELECT * FROM IDENTIFIER($t)"
+ :snowflake "SELECT IDENTIFIER('a') FROM t WHERE IDENTIFIER('b') = 1")))
+
(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"
(are [driver sql] (false? (sql-tools/read-only-select? driver sql))
@@ -535,6 +550,7 @@
: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'))"
+ :snowflake :dynamic-function-call nil "SELECT IDENTIFIER('f')(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 */"