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
Don't splice unvalidated database-type into JSON-field casts (#80482)
Alexander Polyankin13cddfb68df8
diff --git a/src/metabase/driver/mysql.clj b/src/metabase/driver/mysql.cljindex 332e1178d1d..849c211b0c6 100644--- a/src/metabase/driver/mysql.clj+++ b/src/metabase/driver/mysql.clj@@ -505,7 +505,14 @@;; equivalent; instead you can do `<string> + 0.0` =(("float" "double") [:+ json-extract+jsonpath [:inline 0.0]]- [:convert json-extract+jsonpath [:raw (u/upper-case-en field-type)]]))))+ ;; CONVERT's target type cannot be a quoted identifier, so a `database-type` that isn't a plain type name+ ;; (the same rule as [[h2x/cast]]) cannot be spliced into the SQL safely — reject it instead+ (do+ (when-not (h2x/raw-type-name? field-type)+ (throw (ex-info (format "Invalid database type for MySQL CONVERT: %s" (pr-str field-type))+ {:type driver-api/qp.error-type.invalid-query+ :database-type field-type})))+ [:convert json-extract+jsonpath [:raw (u/upper-case-en field-type)]])))))(defmethod sql.qp/->honeysql [:mysql :field][driver [_ opts id-or-name :as mbql-clause]]diff --git a/src/metabase/driver/postgres.clj b/src/metabase/driver/postgres.cljindex 0148a29c72a..fe2bba0116a 100644--- a/src/metabase/driver/postgres.clj+++ b/src/metabase/driver/postgres.clj@@ -768,8 +768,13 @@[_fn [parent-identifier field-type names]](let [names-text-array (into [::text-array] names)[parent-id-sql & parent-id-args] (sql/format-expr parent-identifier {:nested true})- [path-sql & path-args] (sql/format-expr names-text-array {:nested true})]- (into [(format "(%s#>> (%s))::%s" parent-id-sql path-sql field-type)]+ [path-sql & path-args] (sql/format-expr names-text-array {:nested true})+ ;; same rule as [[h2x/cast]]: a plain type name is spliced as-is, anything else (the field's synced+ ;; `database-type`, which we can't trust to be a plain type name) is quoted as an identifier+ [type-sql] (if (h2x/raw-type-name? field-type)+ [field-type]+ (sql/format-expr (h2x/identifier :type-name field-type) {:nested true}))]+ (into [(format "(%s#>> (%s))::%s" parent-id-sql path-sql type-sql)]cat[parent-id-args path-args])))diff --git a/src/metabase/util/honey_sql_2.clj b/src/metabase/util/honey_sql_2.cljindex 7dc0a730f9a..cb18b19e81d 100644--- a/src/metabase/util/honey_sql_2.clj+++ b/src/metabase/util/honey_sql_2.clj@@ -357,10 +357,18 @@(def ^:private raw-cast-type-name-re#"(?i)[a-z][a-z0-9_ ]*(?:\(\d+(?:, ?\d+)?\))?")+(defn raw-type-name?+ "Whether `sql-type` is a plain SQL type name — letters, digits, underscores, and spaces with an optional precision+ suffix, e.g. `varchar(10)` or `double precision` — and is therefore safe to splice into SQL unquoted. Cast targets+ that don't match (e.g. a `database-type` coming from field metadata) must be quoted as identifiers or rejected+ instead of being emitted raw."+ [sql-type]+ (boolean (re-matches raw-cast-type-name-re (name sql-type))))+(mu/defn cast :- TypedExpression"Generate a statement like `cast(expr AS sql-type)`. Returns a typed HoneySQL form."[sql-type expr]- (-> (if (re-matches raw-cast-type-name-re (name sql-type))+ (-> (if (raw-type-name? sql-type)[:cast expr ^:allow-raw-sql [:raw (name sql-type)]][:cast expr (identifier :type-name (name sql-type))])(with-database-type-info sql-type)))diff --git a/test/metabase/driver/mysql_test.clj b/test/metabase/driver/mysql_test.cljindex db4bf2cb2d2..d5c860581df 100644--- a/test/metabase/driver/mysql_test.clj+++ b/test/metabase/driver/mysql_test.clj@@ -548,7 +548,12 @@(testing "Doesn't complain when field is boolean"(let [boolean-boop-field {:database-type "boolean" :nfc-path [:bleh "boop" :foobar 1234]}](is (= ["JSON_UNQUOTE(JSON_EXTRACT(`boop`.`bleh`, ?))" "$.\"boop\".\"foobar\".\"1234\""]- (sql.qp/format-honeysql :mysql (sql.qp/json-query :mysql boop-identifier boolean-boop-field))))))))+ (sql.qp/format-honeysql :mysql (sql.qp/json-query :mysql boop-identifier boolean-boop-field))))))+ (testing "a database-type that isn't a plain type name is rejected instead of spliced raw into CONVERT"+ (let [evil-field {:database-type "signed); select 1 --" :nfc-path [:bleh :meh]}]+ (is (thrown-with-msg? clojure.lang.ExceptionInfo+ #"Invalid database type for MySQL CONVERT"+ (sql.qp/json-query :mysql boop-identifier evil-field)))))))(tx/defdataset json-unquote-test[["json_test"diff --git a/test/metabase/driver/postgres_test.clj b/test/metabase/driver/postgres_test.cljindex fba98310d43..4568e4e2323 100644--- a/test/metabase/driver/postgres_test.clj+++ b/test/metabase/driver/postgres_test.clj@@ -430,7 +430,11 @@(testing "Give us a bigint cast when the field is bigint (#22732)"(let [boolean-boop-field {:database-type "bigint" :nfc-path [:bleh "boop" :foobar 1234]}](is (= ["(boop.bleh#>> (array[?, ?, 1234]::text[]))::bigint" "boop" "foobar"]- (sql/format-expr (#'sql.qp/json-query :postgres boop-identifier boolean-boop-field))))))))+ (sql/format-expr (#'sql.qp/json-query :postgres boop-identifier boolean-boop-field))))))+ (testing "a database-type that isn't a plain type name is quoted as an identifier instead of spliced raw"+ (let [evil-field {:database-type "integer); select 1 --" :nfc-path [:bleh :meh]}]+ (is (= ["(\"boop\".\"bleh\"#>> (array[?]::text[]))::\"integer); select 1 --\"" "meh"]+ (sql.qp/format-honeysql :postgres (#'sql.qp/json-query :postgres boop-identifier evil-field))))))))(deftest ^:parallel json-query-survives-impersonation-validation-test(testing "JSON-extracted field SQL still extracts the same value after `validate-impersonated-query*` re-emits it (#73776)"diff --git a/test/metabase/util/honey_sql_2_test.clj b/test/metabase/util/honey_sql_2_test.cljindex a0147c2bfab..633ffa087f2 100644--- a/test/metabase/util/honey_sql_2_test.clj+++ b/test/metabase/util/honey_sql_2_test.clj@@ -157,6 +157,18 @@(sql/format {:select [[(h2x/cast "date) UNION SELECT 1 --" "toucan")]]}{:quoted true, :dialect :ansi})))))+(deftest ^:parallel raw-type-name?-test+ (are [expected sql-type] (= expected (h2x/raw-type-name? sql-type))+ true "timestamp"+ true :varchar+ true "double precision"+ true "decimal(10, 2)"+ true "VARCHAR(10)"+ false "integer); select 1 --"+ false "\"quoted\""+ false "schema.type"+ false "decimal(10, 2); --"))+(defn- ->sql [expr](sql/format {:select [[expr]]} {:quoted false}))