Skip to content

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

Validate URL-building connection details (#85741)

John Swanson0c85bae63475
diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.edn
index b65df3abda0..8a3e6b28065 100644
--- a/.clj-kondo/config.edn
+++ b/.clj-kondo/config.edn
@@ -223,6 +223,7 @@
metabase.driver.sql-jdbc.execute/execute-prepared-statement!
;; only inspects a details map and throws; the `!` is for the throw, not for a mutation
metabase.driver/validate-db-details!
+ metabase.driver.util/validate-connection-parameters!
metabase.mcp.usage/record-mcp-session!
metabase.mcp.usage/record-mcp-session-end!
metabase.mcp.usage/record-mcp-tool-call!
diff --git a/.clj-kondo/config/test-helpers/config.edn b/.clj-kondo/config/test-helpers/config.edn
index 95a44ec8957..ab4f581e9e8 100644
--- a/.clj-kondo/config/test-helpers/config.edn
+++ b/.clj-kondo/config/test-helpers/config.edn
@@ -72,6 +72,7 @@
metabase.driver.postgres-test/create-enums-db!
metabase.driver.postgres-test/drop-if-exists-and-create-db!
metabase.driver.sql-jdbc.execute/execute-statement!
+ metabase.driver.util/validate-connection-parameters!
metabase.indexed-entities.models.model-index/add-values!
metabase.driver.sql-jdbc.connection.ssh-tunnel-test/start-mock-servers!
metabase.driver.sql-jdbc.connection.ssh-tunnel-test/stop-mock-servers!
diff --git a/docs/developers-guide/driver-changelog.md b/docs/developers-guide/driver-changelog.md
index 454eb931fa6..bbf6504102e 100644
--- a/docs/developers-guide/driver-changelog.md
+++ b/docs/developers-guide/driver-changelog.md
@@ -4,6 +4,16 @@ title: Driver interface changelog
# Driver Interface Changelog
+## Metabase 0.66.0
+
+- `metabase.driver/url-building-detail-keys` `[driver]` -- new multimethod returning the connection-detail keys whose
+ values the driver splices into its connection URL. Metabase refuses a value of one of these that contains `?`, `&`,
+ `;` or `#`, even percent-encoded. Defaults to `[:host :port :db :dbname :catalog :account]`; override it if one of
+ those keys holds something else, such as a whole connection string (H2) or a file path (SQLite).
+
+- `metabase.driver/disallowed-connection-parameters` names now match ignoring percent-encoding, `_ . - +` and
+ whitespace as well as case.
+
## Metabase 0.65.3
- `metabase.driver.sql-jdbc.execute/impl-error-in-query?` `[driver exception]` identifies query errors raised while
diff --git a/modules/drivers/athena/src/metabase/driver/athena.clj b/modules/drivers/athena/src/metabase/driver/athena.clj
index fb26de355a2..d69fca2fced 100644
--- a/modules/drivers/athena/src/metabase/driver/athena.clj
+++ b/modules/drivers/athena/src/metabase/driver/athena.clj
@@ -75,6 +75,11 @@
[region]
(str "//athena." region ".amazonaws.com" (when (str/starts-with? region "cn-") ".cn") ":443"))
+;; the URL is built from the hostname, or from the region when there is none; everything else is a property
+(defmethod driver/url-building-detail-keys :athena
+ [_driver]
+ [:hostname :region])
+
(defmethod driver/connection-hosts :athena
[_driver {:keys [hostname region]}]
(driver/hosts-from-details
diff --git a/modules/drivers/athena/test/metabase/driver/athena_test.clj b/modules/drivers/athena/test/metabase/driver/athena_test.clj
index 74884051a73..5adb566e565 100644
--- a/modules/drivers/athena/test/metabase/driver/athena_test.clj
+++ b/modules/drivers/athena/test/metabase/driver/athena_test.clj
@@ -681,3 +681,13 @@
;; should escape single quotes
"'); OR 1 = 1 --"
"SELECT * FROM categories WHERE regexp_like(name, '''); OR 1 = 1 --')")))))
+
+(deftest ^:parallel url-building-details-reject-separators-test
+ (doseq [details [{:hostname "a;b" :region "us-east-1" :s3_staging_dir "s3://b/"}
+ {:region "us-east-1;b" :s3_staging_dir "s3://b/"}]]
+ (testing (pr-str details)
+ (is (=? {:status-code 400}
+ (try (driver.u/validate-connection-parameters! :athena details)
+ nil
+ (catch clojure.lang.ExceptionInfo e
+ (ex-data e))))))))
diff --git a/modules/drivers/clickhouse/test/metabase/driver/clickhouse_test.clj b/modules/drivers/clickhouse/test/metabase/driver/clickhouse_test.clj
index fe84daef1e4..8a2ab1044c4 100644
--- a/modules/drivers/clickhouse/test/metabase/driver/clickhouse_test.clj
+++ b/modules/drivers/clickhouse/test/metabase/driver/clickhouse_test.clj
@@ -911,3 +911,16 @@
(let [e (SQLException. ^String message "22000" (int code))]
(is (false? (#'sql-jdbc.execute/error-in-query? :clickhouse e)))
(is (true? (#'sql-jdbc.execute/error-in-query? :h2 e)))))))
+
+(deftest ^:parallel url-building-details-reject-separators-test
+ (doseq [details [{:host "https://a/b;c" :port 8443 :dbname "d"}
+ {:host "https://a/b?c" :port 8443 :dbname "d"}]]
+ (testing (pr-str details)
+ (is (=? {:status-code 400}
+ (try (driver.u/validate-connection-parameters! :clickhouse details)
+ nil
+ (catch clojure.lang.ExceptionInfo e
+ (ex-data e)))))))
+ (testing "a host written as a URL with a scheme and path is still allowed"
+ (is (nil? (driver.u/validate-connection-parameters!
+ :clickhouse {:host "https://ch.example.com/x" :port 8443 :dbname "d"})))))
diff --git a/modules/drivers/databricks/test/metabase/driver/databricks_test.clj b/modules/drivers/databricks/test/metabase/driver/databricks_test.clj
index f7e12714792..f6af46dfca8 100644
--- a/modules/drivers/databricks/test/metabase/driver/databricks_test.clj
+++ b/modules/drivers/databricks/test/metabase/driver/databricks_test.clj
@@ -589,3 +589,19 @@
nil
(catch clojure.lang.ExceptionInfo e (ex-data e))))
(str "should be refused: " opts))))))
+
+(deftest ^:parallel url-building-details-reject-separators-test
+ (doseq [details [{:host "abc.cloud.databricks.com:443/;b"
+ :http-path "/sql/1.0/warehouses/x"
+ :catalog "main"
+ :token "t"}
+ {:host "abc.cloud.databricks.com"
+ :http-path "/sql/1.0/warehouses/x"
+ :catalog "main;b"
+ :token "t"}]]
+ (testing (pr-str details)
+ (is (=? {:status-code 400}
+ (try (driver.u/validate-connection-parameters! :databricks details)
+ nil
+ (catch clojure.lang.ExceptionInfo e
+ (ex-data e))))))))
diff --git a/modules/drivers/presto-jdbc/src/metabase/driver/presto_jdbc.clj b/modules/drivers/presto-jdbc/src/metabase/driver/presto_jdbc.clj
index 244f5551079..a3112875061 100644
--- a/modules/drivers/presto-jdbc/src/metabase/driver/presto_jdbc.clj
+++ b/modules/drivers/presto-jdbc/src/metabase/driver/presto_jdbc.clj
@@ -542,6 +542,11 @@
:else
(str c "/" s)))
+;; `jdbc-spec` splices the schema into the URL path after the catalog
+(defmethod driver/url-building-detail-keys :presto-jdbc
+ [driver]
+ (conj ((get-method driver/url-building-detail-keys :default) driver) :schema))
+
(defn- jdbc-spec
"Creates a spec for `clojure.java.jdbc` to use for connecting to Presto via JDBC, from the given `opts`."
[{:keys [host port catalog schema]
diff --git a/modules/drivers/presto-jdbc/test/metabase/driver/presto_jdbc_test.clj b/modules/drivers/presto-jdbc/test/metabase/driver/presto_jdbc_test.clj
index ba9fef0d041..c9c6d9fcd91 100644
--- a/modules/drivers/presto-jdbc/test/metabase/driver/presto_jdbc_test.clj
+++ b/modules/drivers/presto-jdbc/test/metabase/driver/presto_jdbc_test.clj
@@ -11,6 +11,7 @@
[metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]
[metabase.driver.sql-jdbc.execute :as sql-jdbc.execute]
[metabase.driver.sql.query-processor :as sql.qp]
+ [metabase.driver.util :as driver.u]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
[metabase.query-processor.compile :as qp.compile]
@@ -388,3 +389,13 @@
(mt/with-db db
(is (=? {:status :failed, :error_type :qp}
(qp/process-query (qp/userland-query (lib/native-query (mt/metadata-provider) "SELECT 1 AS x")))))))))))
+
+(deftest ^:parallel url-building-details-reject-separators-test
+ (doseq [details [{:host "localhost" :port 8080 :catalog "c" :schema "a?b"}
+ {:host "localhost" :port 8080 :catalog "c" :schema "a%26b"}]]
+ (testing (pr-str details)
+ (is (=? {:status-code 400}
+ (try (driver.u/validate-connection-parameters! :presto-jdbc details)
+ nil
+ (catch clojure.lang.ExceptionInfo e
+ (ex-data e))))))))
diff --git a/modules/drivers/redshift/test/metabase/driver/redshift_test.clj b/modules/drivers/redshift/test/metabase/driver/redshift_test.clj
index 28d479290f1..1aa195657f2 100644
--- a/modules/drivers/redshift/test/metabase/driver/redshift_test.clj
+++ b/modules/drivers/redshift/test/metabase/driver/redshift_test.clj
@@ -851,3 +851,12 @@
{:name nm, :schema "s", :type "table", :description nil, :selectable selectable}))]
(is (= [{:name "readable", :schema "s", :description nil}]
(into [] (#'redshift/describe-database-tables (driver.u/lib-database (:id db))))))))))
+
+(deftest ^:parallel url-building-details-reject-separators-test
+ (doseq [details [{:host "r.example.com" :port 5439 :user "u" :db "a?b"}]]
+ (testing (pr-str details)
+ (is (=? {:status-code 400}
+ (try (driver.u/validate-connection-parameters! :redshift details)
+ nil
+ (catch clojure.lang.ExceptionInfo e
+ (ex-data e))))))))
diff --git a/modules/drivers/sparksql/test/metabase/driver/sparksql_test.clj b/modules/drivers/sparksql/test/metabase/driver/sparksql_test.clj
index 2747d5662d9..b49b49e10df 100644
--- a/modules/drivers/sparksql/test/metabase/driver/sparksql_test.clj
+++ b/modules/drivers/sparksql/test/metabase/driver/sparksql_test.clj
@@ -7,6 +7,7 @@
[metabase.driver.sparksql :as sparksql]
[metabase.driver.sql-jdbc.execute :as sql-jdbc.execute]
[metabase.driver.sql.query-processor :as sql.qp]
+ [metabase.driver.util :as driver.u]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.test-util.notebook-helpers :as lib.tu.notebook]
@@ -179,3 +180,13 @@
(testing "The query should run successfully"
(is (= [[1 4] [2 11] [3 11]]
(mt/rows (qp/process-query query)))))))))
+
+(deftest ^:parallel url-building-details-reject-separators-test
+ (doseq [details [{:host "localhost" :port 10000 :dbname "a;b"}
+ {:host "localhost" :port 10000 :dbname "a%3Bb"}]]
+ (testing (pr-str details)
+ (is (=? {:status-code 400}
+ (try (driver.u/validate-connection-parameters! :sparksql details)
+ nil
+ (catch clojure.lang.ExceptionInfo e
+ (ex-data e))))))))
diff --git a/modules/drivers/starburst/src/metabase/driver/starburst.clj b/modules/drivers/starburst/src/metabase/driver/starburst.clj
index 5005d544944..ef27083e7e7 100644
--- a/modules/drivers/starburst/src/metabase/driver/starburst.clj
+++ b/modules/drivers/starburst/src/metabase/driver/starburst.clj
@@ -949,6 +949,11 @@
;; in any case, remove the standalone Kerberos properties from details map
(apply dissoc (cons det (keys kerb-props->url-param-names)))))
+;; `jdbc-spec` splices the schema into the URL path after the catalog
+(defmethod driver/url-building-detail-keys :starburst
+ [driver]
+ (conj ((get-method driver/url-building-detail-keys :default) driver) :schema))
+
(defn- jdbc-spec
"Creates a spec for `clojure.java.jdbc` to use for connecting to starburst via JDBC, from the given `opts`."
[{:keys [host port catalog schema]
diff --git a/modules/drivers/starburst/test/metabase/driver/starburst_test.clj b/modules/drivers/starburst/test/metabase/driver/starburst_test.clj
index 586a9377fb4..85e794228c8 100644
--- a/modules/drivers/starburst/test/metabase/driver/starburst_test.clj
+++ b/modules/drivers/starburst/test/metabase/driver/starburst_test.clj
@@ -12,6 +12,7 @@
[metabase.driver.sql-jdbc.sync.interface :as sql-jdbc.sync.interface]
[metabase.driver.sql.query-processor :as sql.qp]
[metabase.driver.starburst :as starburst]
+ [metabase.driver.util :as driver.u]
[metabase.lib.metadata :as lib.metadata]
[metabase.query-processor.compile :as qp.compile]
[metabase.query-processor.test :as qp]
@@ -411,3 +412,13 @@
(fn [^Connection conn]
(is (false?
(sql-jdbc.sync.interface/have-select-privilege? driver/*driver* conn "default" "fake_table"))))))))
+
+(deftest ^:parallel url-building-details-reject-separators-test
+ (doseq [details [{:host "localhost" :port 8080 :catalog "c" :schema "a?b"}
+ {:host "localhost" :port 8080 :catalog "c" :schema "a%26b"}]]
+ (testing (pr-str details)
+ (is (=? {:status-code 400}
+ (try (driver.u/validate-connection-parameters! :starburst details)
+ nil
+ (catch clojure.lang.ExceptionInfo e
+ (ex-data e))))))))
diff --git a/modules/drivers/vertica/test/metabase/driver/vertica_test.clj b/modules/drivers/vertica/test/metabase/driver/vertica_test.clj
index cec4f3f0ac9..ff3eae71320 100644
--- a/modules/drivers/vertica/test/metabase/driver/vertica_test.clj
+++ b/modules/drivers/vertica/test/metabase/driver/vertica_test.clj
@@ -5,6 +5,7 @@
[clojure.test :refer :all]
[metabase.driver :as driver]
[metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]
+ [metabase.driver.util :as driver.u]
[metabase.lib.metadata :as lib.metadata]
[metabase.query-processor.compile :as qp.compile]
[metabase.test :as mt]
@@ -98,3 +99,12 @@
(->> (mt/native-query {:query (tx/native-array-query :vertica)})
mt/process-query
mt/rows)))))
+
+(deftest ^:parallel url-building-details-reject-separators-test
+ (doseq [details [{:host "localhost" :port 5433 :user "u" :db "a?b"}]]
+ (testing (pr-str details)
+ (is (=? {:status-code 400}
+ (try (driver.u/validate-connection-parameters! :vertica details)
+ nil
+ (catch clojure.lang.ExceptionInfo e
+ (ex-data e))))))))
diff --git a/src/metabase/driver.clj b/src/metabase/driver.clj
index 80f6fc1fec4..c3cb14de144 100644
--- a/src/metabase/driver.clj
+++ b/src/metabase/driver.clj
@@ -286,10 +286,11 @@
as connection properties. Typically parameters whose value the client loads and instantiates as a Java class. Defaults
to none.
- Matched case-insensitively, and as substrings, so a name here also catches its longer spellings (`socketFactory`
- covers `socketFactoryClass`). Enforced by [[metabase.driver.util/validate-connection-parameters!]], which runs when a
- connection is tested, a connection pool is created, and a Database is saved. Extend a parent's list rather than
- replacing it:
+ Matched as substrings of `:additional-options` and of each detail key, after normalization: case folded, percent
+ escapes decoded once, and `_ . - +` and whitespace ignored. So a name here also catches its longer and disguised
+ spellings (`socketFactory` covers `socketFactoryClass` and `SOCKET_FACTORY`). Enforced by
+ [[metabase.driver.util/validate-connection-parameters!]], which runs when a connection is tested, a connection pool
+ is created, and a Database is saved. Extend a parent's list rather than replacing it:
(defmethod driver/disallowed-connection-parameters :mysql
[driver]
@@ -302,6 +303,21 @@
[_driver]
[])
+(defmulti url-building-detail-keys
+ "The connection-detail keys whose values this driver splices into its connection URL.
+ [[metabase.driver.util/validate-connection-parameters!]] refuses a value of one of these that contains a URL
+ separator, even percent-encoded, so it cannot start a client parameter of its own.
+
+ Defaults to `[:host :port :db :dbname :catalog :account]`. Override it when one of those keys is not spliced into a
+ URL, such as one holding a whole connection string or a file path."
+ {:added "0.66.0" :arglists '([driver])}
+ dispatch-on-initialized-driver
+ :hierarchy #'hierarchy)
+
+(defmethod url-building-detail-keys :default
+ [_driver]
+ [:host :port :db :dbname :catalog :account])
+
(def default-host-detail-keys
"Detail keys that hold a warehouse host across the drivers we ship. `:host` is the near-universal one;
`:hostname` is Athena's."
diff --git a/src/metabase/driver/h2.clj b/src/metabase/driver/h2.clj
index dffcb75e784..49cd2979ec3 100644
--- a/src/metabase/driver/h2.clj
+++ b/src/metabase/driver/h2.clj
@@ -64,6 +64,12 @@
(driver/hosts-from-details {:db db} [:db])
[]))
+;; H2's only connection detail, `:db`, is the whole connection string (`file:/path/to/db;OPTION=...`), passed through
+;; as-is, so it is not a value spliced into a URL Metabase builds
+(defmethod driver/url-building-detail-keys :h2
+ [_driver]
+ [])
+
;;; this will prevent the H2 driver from showing up in the list of options when adding a new Database.
(defmethod driver/superseded-by :h2 [_driver] :deprecated)
diff --git a/src/metabase/driver/sqlite.clj b/src/metabase/driver/sqlite.clj
index 1af1f00a6e6..f61edd6a0c7 100644
--- a/src/metabase/driver/sqlite.clj
+++ b/src/metabase/driver/sqlite.clj
@@ -177,6 +177,11 @@
(check-db-file! details))
nil)
+;; `:db` is a file path, not part of a URL
+(defmethod driver/url-building-detail-keys :sqlite
+ [driver]
+ (remove #{:db} ((get-method driver/url-building-detail-keys :default) driver)))
+
(defmethod driver/can-connect? :sqlite
[driver details]
(driver/validate-db-details! driver details)
diff --git a/src/metabase/driver/util.clj b/src/metabase/driver/util.clj
index f27fff6350c..f69a86c111d 100644
--- a/src/metabase/driver/util.clj
+++ b/src/metabase/driver/util.clj
@@ -26,7 +26,8 @@
[metabase.util.log :as log]
[metabase.util.malli :as mu]
[metabase.util.performance :refer [mapv empty? some]]
- [metabase.warehouses.schema :as warehouses.schema])
+ [metabase.warehouses.schema :as warehouses.schema]
+ [ring.util.codec :as codec])
(:import
(java.io ByteArrayInputStream)
(java.security KeyFactory KeyStore PrivateKey)
@@ -236,17 +237,50 @@
(when (some #(not (u.http/host-allowed-for-network-policy? policy %)) hosts)
(throw (blocked-network-address-exception)))))))
+(defn- fold-case
+ "`s` with each code point folded the way `String/equalsIgnoreCase` compares it: upper case, then lower case."
+ ^String [^String s]
+ (-> (String/.codePoints s)
+ (.map ^[int] Character/toUpperCase)
+ (.map ^[int] Character/toLowerCase)
+ (.collect ^[] StringBuilder/new
+ ^[int] StringBuilder/.appendCodePoint
+ ^[StringBuffer] StringBuilder/.append)
+ (str)))
+
+(defn- normalize-connection-parameter
+ "Normalize `s` for matching against [[driver/disallowed-connection-parameters]]: percent escapes decoded once, case
+ folded the way `equalsIgnoreCase` does, and `_ . - +` and whitespace removed."
+ [s]
+ ;; (?U) makes \s match Unicode whitespace too
+ (-> (codec/percent-decode (str s)) fold-case (str/replace #"(?U)[_.\-+\s]" "")))
+
(defn disallowed-connection-parameter
- "The first name in [[driver/disallowed-connection-parameters]] that the string `s` contains, matched
- case-insensitively, or nil when it contains none."
+ "The first name in [[driver/disallowed-connection-parameters]] that the string `s` contains, after both are
+ normalized by [[normalize-connection-parameter]], or nil when it contains none."
[driver s]
- (let [s (u/lower-case-en (str s))]
- (some #(when (str/includes? s (u/lower-case-en %)) %)
+ (let [s (normalize-connection-parameter s)]
+ (some #(when (str/includes? s (normalize-connection-parameter %)) %)
(driver/disallowed-connection-parameters driver))))
+(def ^:private url-separators
+ "Characters that end a URL-building detail's part of a connection URL and start a client parameter."
+ [\? \& \; \#])
+
+(defn- validate-url-building-details!
+ [driver details]
+ (doseq [k (driver/url-building-detail-keys driver)
+ :let [v (get details k)]
+ ;; some clients percent-decode the URL once before splitting off its parameters, so an encoded separator is
+ ;; refused too
+ :when (and (some? v) (some (set url-separators) (codec/percent-decode (str v))))]
+ (throw (ex-info (str "Connection detail " (name k) " may not contain " (str/join " " url-separators))
+ {:status-code 400, :detail k}))))
+
(defn validate-connection-parameters!
"Throw a 400 if `details` set a connection parameter named in [[driver/disallowed-connection-parameters]], either in
- `:additional-options` or as a detail key. Returns nil when the details are acceptable.
+ `:additional-options` or as a detail key, or if a value of one of the [[driver/url-building-detail-keys]] contains a
+ URL separator. Returns nil when the details are acceptable.
Reads the details a user supplied rather than the connection spec built from them: the spec also carries properties
Metabase sets itself, which are not the user's to have chosen. An auth provider's response is merged into the details
@@ -257,7 +291,8 @@
(when-let [match (some->> (:additional-options details) (disallowed-connection-parameter driver))]
(refuse! "additional options" match))
(when-let [match (some #(disallowed-connection-parameter driver (if (keyword? %) (name %) %)) (keys details))]
- (refuse! "connection details" match))))
+ (refuse! "connection details" match)))
+ (validate-url-building-details! driver details))
(defn can-connect-with-details?
"Check whether we can connect to a database with `driver` and `details-map` and perform a basic query such as `SELECT
diff --git a/test/metabase/driver/mysql_test.clj b/test/metabase/driver/mysql_test.clj
index bba0501627b..0de81804772 100644
--- a/test/metabase/driver/mysql_test.clj
+++ b/test/metabase/driver/mysql_test.clj
@@ -1467,3 +1467,13 @@
(doseq [[^String state code expected] [["HY000" 4176 true] ["HY000" 1205 false] [nil 4176 false]]]
(is (= expected (sql-jdbc.execute/impl-error-in-query?
:mysql (SQLTransientConnectionException. "Row read failed" state (int code))))))))
+
+(deftest ^:parallel url-building-details-reject-separators-test
+ (doseq [details [{:host "localhost" :port 3306 :user "u" :db "a?b"}
+ {:host "localhost" :port 3306 :user "u" :db "a%3Fb"}]]
+ (testing (pr-str details)
+ (is (=? {:status-code 400}
+ (try (driver.u/validate-connection-parameters! :mysql details)
+ nil
+ (catch clojure.lang.ExceptionInfo e
+ (ex-data e))))))))
diff --git a/test/metabase/driver/postgres_test.clj b/test/metabase/driver/postgres_test.clj
index d76fc3185da..8b3c69e7a79 100644
--- a/test/metabase/driver/postgres_test.clj
+++ b/test/metabase/driver/postgres_test.clj
@@ -2708,3 +2708,13 @@
(lib.metadata/database (mt/metadata-provider)) [(format "SELECT generate_series(1, %d) AS i" n)])]
(testing "only the first rows are pulled, not all ~2 billion"
(is (= [1 2 3] (into [] (comp (take 3) (map :i)) result))))))))
+
+(deftest ^:parallel url-building-details-reject-separators-test
+ (doseq [details [{:host "localhost" :port 5432 :user "u" :db "a?b"}
+ {:host "localhost:5432/d?b" :port 5432 :user "u" :db "d"}]]
+ (testing (pr-str details)
+ (is (=? {:status-code 400}
+ (try (driver.u/validate-connection-parameters! :postgres details)
+ nil
+ (catch clojure.lang.ExceptionInfo e
+ (ex-data e))))))))
diff --git a/test/metabase/driver/util_test.clj b/test/metabase/driver/util_test.clj
index e84c3bc1a93..0af4bd9285c 100644
--- a/test/metabase/driver/util_test.clj
+++ b/test/metabase/driver/util_test.clj
@@ -986,3 +986,75 @@
(mt/with-temp-env-var-value! [mb-warehouse-allowed-networks "allow-private"]
(is (= :allow-private
(startup/def-startup-validation! ::driver.settings/warehouse-allowed-networks))))))
+
+(deftest ^:parallel normalize-connection-parameter-test
+ (are [s expected] (= expected (#'driver.u/normalize-connection-parameter s))
+ "testSetting" "testsetting"
+ "TEST_SETTING" "testsetting"
+ "test.setting-name+x y\tz" "testsettingnamexyz"
+ "%74est%53etting" "testsetting"
+ "%2574estSetting" "%74estsetting"
+ "te%C5%BFtSetting" "testsetting"
+ "teſtSetting" "testsetting"
+ "testSettİng" "testsetting"
+ "%" "%"
+ "%zz%4" "%zz%4"
+ "%C5" "�"
+ nil ""))
+
+(driver/register! ::denylist-driver, :abstract? true)
+
+(defmethod driver/disallowed-connection-parameters ::denylist-driver
+ [_driver]
+ ["testSetting" "otherSetting"])
+
+(deftest ^:parallel disallowed-connection-parameter-normalizes-both-sides-test
+ (is (= "testSetting" (driver.u/disallowed-connection-parameter ::denylist-driver "x=te%C5%BFT_setting")))
+ (is (= "otherSetting" (driver.u/disallowed-connection-parameter ::denylist-driver "OTHER-setting.x")))
+ (is (= "testSetting" (driver.u/disallowed-connection-parameter ::denylist-driver "testSettİng=1")))
+ (is (nil? (driver.u/disallowed-connection-parameter ::denylist-driver "harmless=1"))))
+
+(defn- validation-error [driver details]
+ (try
+ (driver.u/validate-connection-parameters! driver details)
+ nil
+ (catch clojure.lang.ExceptionInfo e
+ (assoc (ex-data e) :message (ex-message e)))))
+
+(deftest ^:parallel validate-connection-parameters!-normalizes-names-test
+ (doseq [opts ["testSetting=x" "TEST_SETTING=x" "test.setting=x" "%74est%53etting=x" "teſtSetting=x"]]
+ (testing (pr-str opts)
+ (is (= {:status-code 400 :disallowed-key "testSetting" :message "Potentially dangerous keys in additional options"}
+ (validation-error ::denylist-driver {:additional-options opts})))))
+ (doseq [k [:TEST_SETTING :test-setting "test%53etting" :teſtSetting]]
+ (testing (pr-str k)
+ (is (=? {:status-code 400 :message "Potentially dangerous keys in connection details"}
+ (validation-error ::denylist-driver {k "x"})))))
+ (testing "a disallowed name in a URL-building value is refused by the separator check"
+ (is (=? {:status-code 400 :detail :db}
+ (validation-error ::denylist-driver {:db "a?testSetting=x"}))))
+ (is (nil? (validation-error ::denylist-driver {:host "db.example.com" :port 5432 :db "d" :user "u" :password "p&q;r"
+ :additional-options "harmless=1&other=2"}))))
+
+(deftest ^:parallel validate-connection-parameters!-url-building-details-test
+ (doseq [k [:host :port :db :dbname :catalog :account]
+ sep ["?" "&" ";" "#"]]
+ (testing (pr-str k sep)
+ (is (= {:status-code 400
+ :detail k
+ :message (str "Connection detail " (name k) " may not contain ? & ; #")}
+ (validation-error :postgres {k (str "a" sep "b")})))))
+ (doseq [k [:host :db]
+ encoded ["%3B" "%3b" "%3F" "%26" "%23"]]
+ (testing (pr-str k encoded)
+ (is (= 400 (:status-code (validation-error :postgres {k (str "a" encoded "b")}))))))
+ (doseq [k [:host :db]]
+ (testing (pr-str k "%253B")
+ ;; clients decode once, so a double-encoded separator reaches them as the literal text %3B
+ (is (nil? (validation-error :postgres {k "a%253Bb"})))))
+ (is (nil? (validation-error :postgres {:host "https://db.example.com:5432/x" :port 5432 :db "my db%2Fb%41%20"
+ :password "a?b&c;d#e f"})))
+ (is (= [] (driver/url-building-detail-keys :h2)))
+ (is (nil? (validation-error :h2 {:db "file:/tmp/x;USER=GUEST;PASSWORD=guest"})))
+ (is (not-any? #{:db} (driver/url-building-detail-keys :sqlite)))
+ (is (nil? (validation-error :sqlite {:db "/tmp/my data#1.db"}))))