Skip to content

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

Check disallowed connection parameters in detail keys and at pool creation (#83135)

Nathan Voxlandeec2196232e2
diff --git a/docs/developers-guide/driver-changelog.md b/docs/developers-guide/driver-changelog.md
index ed4034778c4..8d0d6349332 100644
--- a/docs/developers-guide/driver-changelog.md
+++ b/docs/developers-guide/driver-changelog.md
@@ -6,6 +6,17 @@ title: Driver interface changelog
## Metabase 0.65.0
+- `metabase.driver/disallowed-connection-parameters` `[driver]` -- new multimethod returning the names of client
+ connection parameters a user may never set, whether in `:additional-options` or as a detail key. Metabase refuses
+ details that set one when a connection is tested, when a connection pool is created, and when a Database is saved.
+ Names match case-insensitively and as substrings. Extend the parent's list rather than replacing it:
+
+ ```clj
+ (defmethod driver/disallowed-connection-parameters :my-driver
+ [driver]
+ (into ((get-method driver/disallowed-connection-parameters :sql-jdbc) driver) ["someClassNameProperty"]))
+ ```
+
- `sql.qp/use-ctes-for-stages?` is a multi-method for drivers to opt-in to compiling multi-stage queries
with CTEs instead of nested subselects. Drivers should only do this if they satisfy all of the criteria
in the docstring of this method.
diff --git a/modules/drivers/databricks/src/metabase/driver/databricks.clj b/modules/drivers/databricks/src/metabase/driver/databricks.clj
index a6aa12cecb2..09bc293fdbf 100644
--- a/modules/drivers/databricks/src/metabase/driver/databricks.clj
+++ b/modules/drivers/databricks/src/metabase/driver/databricks.clj
@@ -75,13 +75,12 @@
((get-method sql-jdbc.sync/database-type->base-type :hive-like)
driver database-type)))
-(defmethod driver/validate-db-details! :databricks
- [_driver details]
- (sql-jdbc/reject-dangerous-additional-options! details)
- (when-let [opts (not-empty (:additional-options details))]
- ;; `StagingAllowedLocalPaths` is the older spelling of the same setting, and the client still honors it
- (when (re-find #"(?i)VolumeOperationAllowedLocalPaths|StagingAllowedLocalPaths" opts)
- (throw (Exception. "Potentially dangerous keys in connection details")))))
+(defmethod driver/disallowed-connection-parameters :databricks
+ [driver]
+ ;; enable Unity Catalog volume operations, which read and write arbitrary local files. `StagingAllowedLocalPaths` is
+ ;; the older spelling of the same setting, and the client still honors it
+ (into ((get-method driver/disallowed-connection-parameters :sql-jdbc) driver)
+ ["VolumeOperationAllowedLocalPaths" "StagingAllowedLocalPaths"]))
(defn- catalog-present?
[jdbc-spec catalog]
diff --git a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
index 2eb84edf71f..77aa7b349d3 100644
--- a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
+++ b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
@@ -26,6 +26,7 @@
[metabase.driver.sql.query-processor.like-escape-char-built-in :as like-escape-char-built-in]
[metabase.driver.sql.query-processor.util :as sql.qp.u]
[metabase.driver.sql.util :as sql.u]
+ [metabase.driver.util :as driver.u]
[metabase.lib.options :as lib.options]
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.sql-tools.core :as sql-tools]
@@ -289,14 +290,20 @@
(when-not (str/blank? instance) {:instanceName instance}))
(sql-jdbc.common/handle-additional-options details, :seperator-style :semicolon))))
-(def ^:private disallowed-additional-opts
- #"(?i)(?:socketFactoryClass|socketFactoryConstructorArg|trustManagerClass|trustManagerConstructorArg|accessTokenCallbackClass)")
+(def ^:private sqlserver-disallowed-parameters
+ ["socketFactoryClass" "socketFactoryConstructorArg" "trustManagerClass" "trustManagerConstructorArg"
+ "accessTokenCallbackClass"])
+
+(defmethod driver/disallowed-connection-parameters :sqlserver
+ [driver]
+ (into ((get-method driver/disallowed-connection-parameters :sql-jdbc) driver) sqlserver-disallowed-parameters))
(defmethod driver/validate-db-details! :sqlserver
- [_driver {:keys [host additional-options] :as details}]
- (when-let [match (some->> (str host ";" additional-options) (re-find disallowed-additional-opts))]
- (throw (ex-info "Potentially dangerous keys in connection details" {:disallowed-key match})))
- (sql-jdbc/reject-dangerous-additional-options! details))
+ [driver {:keys [host] :as details}]
+ ;; `host` is written into the connection string too, so a property can be smuggled in through it after a `;`
+ (when-let [match (driver.u/disallowed-connection-parameter driver host)]
+ (throw (ex-info "Potentially dangerous keys in connection details" {:status-code 400, :disallowed-key match})))
+ (driver.u/validate-connection-parameters! driver details))
(defmethod driver/can-connect? :sqlserver
[driver details]
diff --git a/modules/drivers/sqlserver/test/metabase/driver/sqlserver_test.clj b/modules/drivers/sqlserver/test/metabase/driver/sqlserver_test.clj
index 192538b1bc1..8406b0f8d01 100644
--- a/modules/drivers/sqlserver/test/metabase/driver/sqlserver_test.clj
+++ b/modules/drivers/sqlserver/test/metabase/driver/sqlserver_test.clj
@@ -304,6 +304,17 @@
(is (= "ActiveDirectoryManagedIdentity" (:authentication spec)))
(is (not (contains? spec :user))))))))
+(deftest ^:parallel reject-inherited-disallowed-parameters-in-host-test
+ (mt/test-driver :sqlserver
+ (testing "parameters every SQL-JDBC driver disallows are refused in `host` too"
+ (are [bad-option] (thrown-with-msg? clojure.lang.ExceptionInfo
+ #"Potentially dangerous keys in connection details"
+ (driver/validate-db-details! :sqlserver {:host (str "sql.example.com;" bad-option)
+ :db "db"}))
+ "hostnameverifier=bad.Verifier"
+ "sslfactory=bad.Factory"
+ "loggerFile=/tmp/x"))))
+
(deftest ^:parallel reject-details-with-dangerous-additional-options-test
(mt/test-driver :sqlserver
(let [details (:details (mt/db))]
@@ -311,7 +322,7 @@
(are [bad-option] (let [bad-opts-details (assoc details :additional-options bad-option)
bad-host-details (update details :host str ";" bad-option)]
(is (thrown-with-msg? java.lang.Exception
- #"Potentially dangerous keys in connection details"
+ #"Potentially dangerous keys in additional options"
(driver/can-connect? :sqlserver bad-opts-details)))
(is (thrown-with-msg? java.lang.Exception
#"Potentially dangerous keys in connection details"
diff --git a/src/metabase/driver.clj b/src/metabase/driver.clj
index 0d56c7e9aa5..0e6edf8dd3d 100644
--- a/src/metabase/driver.clj
+++ b/src/metabase/driver.clj
@@ -280,6 +280,28 @@
(defmethod validate-db-details! :default [_driver _details] nil)
+(defmulti disallowed-connection-parameters
+ "The names of client connection parameters a user may never set for this driver, whether written into
+ `:additional-options` or sent as a detail key -- several drivers hand detail keys they do not recognize to the client
+ 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:
+
+ (defmethod driver/disallowed-connection-parameters :mysql
+ [driver]
+ (into ((get-method driver/disallowed-connection-parameters :sql-jdbc) driver) [\"autoDeserialize\"]))"
+ {:added "0.65.0" :arglists '([driver])}
+ dispatch-on-initialized-driver
+ :hierarchy #'hierarchy)
+
+(defmethod disallowed-connection-parameters :default
+ [_driver]
+ [])
+
(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/mysql.clj b/src/metabase/driver/mysql.clj
index 00e7125ca12..8931bcf4079 100644
--- a/src/metabase/driver/mysql.clj
+++ b/src/metabase/driver/mysql.clj
@@ -228,13 +228,10 @@
"All Metabase features may not work properly when using an unsupported version."
"\n********************************************************************************\n"))))))))
-(def ^:private disallowed-additional-opts #"(?:allowLocalInfile|allowLoadLocalInfile|allowLoadLocalInfileInPath|allowUrlInLocalInfile|autoDeserialize|serverRSAPublicKeyFile)")
-
-(defmethod driver/validate-db-details! :mysql
- [_driver details]
- (sql-jdbc/reject-dangerous-additional-options! details)
- (when-let [match (some->> (:additional-options details) (re-find disallowed-additional-opts))]
- (throw (ex-info "Potentially dangerous keys in additional options" {:disallowed-key match}))))
+(defmethod driver/disallowed-connection-parameters :mysql
+ [driver]
+ (into ((get-method driver/disallowed-connection-parameters :sql-jdbc) driver)
+ ["allowLocalInfile" "allowLoadLocalInfile" "allowUrlInLocalInfile" "autoDeserialize" "serverRSAPublicKeyFile" "allowLoadLocalInfileInPath"]))
(defmethod driver/can-connect? :mysql
[driver details]
diff --git a/src/metabase/driver/sql_jdbc.clj b/src/metabase/driver/sql_jdbc.clj
index 3f30881315c..f2c544d4603 100644
--- a/src/metabase/driver/sql_jdbc.clj
+++ b/src/metabase/driver/sql_jdbc.clj
@@ -19,6 +19,7 @@
[metabase.driver.sql-jdbc.sync.describe-database :as sql-jdbc.describe-database]
[metabase.driver.sql.query-processor :as sql.qp]
[metabase.driver.sync :as driver.s]
+ [metabase.driver.util :as driver.u]
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.malli :as mu]
@@ -53,27 +54,21 @@
;;; | Default SQL JDBC metabase.driver impls |
;;; +----------------------------------------------------------------------------------------------------------------+
-(def ^:private disallowed-additional-opts
- "JDBC connection properties that are not needed to connect to a warehouse and are rejected for every
- SQL-JDBC driver. Matched case-insensitively as substrings of the raw `additional-options` string, so a token here
- also catches its longer spellings (`socketFactory` covers `socketFactoryClass`, `socketfactoryname`,
- `sslsocketfactoryname`; `hostnameverifier` covers `sslhostnameverifier`).
-
- These are all properties whose value a JDBC driver loads and instantiates as a Java class -- a no-arg constructor
- and/or static initializer runs inside the Metabase process -- which is arbitrary-class-instantiation leading to
- code execution when a gadget is on the classpath. None is needed to reach a data warehouse."
- #"(?i)(?:socketFactory|sslfactory|hostnameverifier|sslpasswordcallback|xmlFactoryFactory|loggerFile|queryInterceptors|dnsResolver)")
-
-(defn reject-dangerous-additional-options!
- "Throw if `details` name a JDBC connection property on the shared SQL-JDBC denylist. Drivers that override
- `validate-db-details!` must call this themselves."
- [details]
- (when-let [match (some->> (:additional-options details) (re-find disallowed-additional-opts))]
- (throw (ex-info "Potentially dangerous keys in additional options" {:disallowed-key match}))))
+;; JDBC connection properties that are not needed to connect to a warehouse and are rejected for every SQL-JDBC driver.
+;; Matched as substrings, so a token here also catches its longer spellings (`socketFactory` covers
+;; `socketFactoryClass`, `socketfactoryname`, `sslsocketfactoryname`; `hostnameverifier` covers `sslhostnameverifier`).
+;;
+;; These are all properties whose value a JDBC driver loads and instantiates as a Java class -- a no-arg constructor
+;; and/or static initializer runs inside the Metabase process -- which is arbitrary-class-instantiation leading to code
+;; execution when a gadget is on the classpath. None is needed to reach a data warehouse.
+(defmethod driver/disallowed-connection-parameters :sql-jdbc
+ [_driver]
+ ["socketFactory" "sslfactory" "hostnameverifier" "sslpasswordcallback" "xmlFactoryFactory" "loggerFile"
+ "queryInterceptors" "dnsResolver"])
(defmethod driver/validate-db-details! :sql-jdbc
- [_driver details]
- (reject-dangerous-additional-options! details))
+ [driver details]
+ (driver.u/validate-connection-parameters! driver details))
(defmethod driver/can-connect? :sql-jdbc
[driver details]
diff --git a/src/metabase/driver/sql_jdbc/connection.clj b/src/metabase/driver/sql_jdbc/connection.clj
index 0ba4fba8072..9fca130a22e 100644
--- a/src/metabase/driver/sql_jdbc/connection.clj
+++ b/src/metabase/driver/sql_jdbc/connection.clj
@@ -276,6 +276,8 @@
;; went through a connection test (serialization import, config files).
_ (driver.u/with-database-network-policy database
(driver.u/validate-connection-hosts! driver details))
+ ;; likewise for parameters that were disallowed after the database was saved
+ _ (driver.u/validate-connection-parameters! driver details)
details-with-tunnel (driver/incorporate-ssh-tunnel-details ;; If the tunnel is disabled this returned unchanged
driver
(update details :port #(or % (default-ssh-tunnel-target-port driver))))
@@ -283,6 +285,8 @@
driver
id
details-with-tunnel)
+ ;; the auth provider's response is merged into the details, so it can set parameters too
+ _ (driver.u/validate-connection-parameters! driver details-with-auth)
spec (connection-details->spec driver details-with-auth)
properties (data-warehouse-connection-pool-properties driver database)]
(merge
@@ -519,6 +523,7 @@
(defn do-with-connection-spec-for-testing-connection
"Impl for [[with-connection-spec-for-testing-connection]]."
[driver details f]
+ (driver.u/validate-connection-parameters! driver details)
(let [details (-> details
(update :port #(or % (default-ssh-tunnel-target-port driver)))
(ssh/resolve-known-hosts driver))]
@@ -526,7 +531,9 @@
(let [details-with-auth (driver.u/fetch-and-incorporate-auth-provider-details
driver
details-with-tunnel)
- spec (connection-details->spec driver details-with-auth)]
+ ;; the auth provider's response is merged into the details, so it can set parameters too
+ _ (driver.u/validate-connection-parameters! driver details-with-auth)
+ spec (connection-details->spec driver details-with-auth)]
(f spec)))))
(defmacro with-connection-spec-for-testing-connection
diff --git a/src/metabase/driver/util.clj b/src/metabase/driver/util.clj
index f66dc18b9d4..22bd6743f7d 100644
--- a/src/metabase/driver/util.clj
+++ b/src/metabase/driver/util.clj
@@ -234,6 +234,29 @@
(when (some #(not (u.http/host-allowed-for-network-policy? policy %)) hosts)
(throw (blocked-network-address-exception)))))))
+(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."
+ [driver s]
+ (let [s (u/lower-case-en (str s))]
+ (some #(when (str/includes? s (u/lower-case-en %)) %)
+ (driver/disallowed-connection-parameters driver))))
+
+(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.
+
+ 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
+ as well (see [[driver/incorporate-auth-provider-details]]), so check the details again once it has been."
+ [driver details]
+ (letfn [(refuse! [where match]
+ (throw (ex-info (str "Potentially dangerous keys in " where) {:status-code 400, :disallowed-key match})))]
+ (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))))
+
(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
1`. Specify optional param `throw-exceptions` if you want to handle any exceptions thrown yourself (e.g., so you
diff --git a/src/metabase/warehouses/models/database.clj b/src/metabase/warehouses/models/database.clj
index ff83a1fe0d0..047c06d9c33 100644
--- a/src/metabase/warehouses/models/database.clj
+++ b/src/metabase/warehouses/models/database.clj
@@ -464,9 +464,10 @@
"Every place a Database stores a set of connection details."
[:details :write_data_details :admin_details])
-(defn- validate-connection-hosts!
- "Refuse to store details pointing at a private/internal network address. Enforcing this on the model, and not just on
- the endpoints that test a connection, covers the routes that write a Database without ever testing it: serialization
+(defn- validate-connection-details!
+ "Refuse to store details pointing at a private/internal network address, or setting a connection parameter the driver
+ disallows (see [[driver/disallowed-connection-parameters]]). Enforcing this on the model, and not just on the
+ endpoints that test a connection, covers the routes that write a Database without ever testing it: serialization
import, config-file provisioning, and destination databases.
`keys-to-check` names which of [[details-keys]] to look at. An overlay is checked the way
@@ -480,8 +481,11 @@
(doseq [k keys-to-check
:let [details (get database k)]
:when (and (map? details) (seq details))]
- (driver.u/validate-connection-hosts! engine (cond->> details
- (not= k :details) (merge (:details database))))))))
+ (let [details (cond->> details
+ (not= k :details) (merge (:details database)))]
+ (driver.u/validate-connection-hosts! engine details)
+ (when (driver.impl/registered? engine)
+ (driver.u/validate-connection-parameters! engine details)))))))
(t2/define-before-update :model/Database
[database]
@@ -492,11 +496,11 @@
;; driver. Otherwise validate only the ones being written, so an unrelated update to a grandfathered database does
;; not start failing. Either way the candidate is the merge, since an overlay is resolved against the `:details`
;; it accompanies rather than on its own.
- (validate-connection-hosts! (or (:engine changes) (:engine original))
- (merge original changes)
- (if (contains? changes :engine)
- details-keys
- (filterv #(contains? changes %) details-keys))))
+ (validate-connection-details! (or (:engine changes) (:engine original))
+ (merge original changes)
+ (if (contains? changes :engine)
+ details-keys
+ (filterv #(contains? changes %) details-keys))))
;; Note: the "sample database may not be edited" policy is enforced at the API layer
;; ([[metabase.warehouses-rest.api]] PUT /:id), so internally-derived updates - e.g. the sample
;; database engine migration in [[metabase.sample-data.impl]] - can change the engine here.
@@ -554,7 +558,7 @@
(t2/define-before-insert :model/Database
[{:keys [details initial_sync_status engine], :as database}]
- (validate-connection-hosts! engine database details-keys)
+ (validate-connection-details! engine database details-keys)
(-> (merge {:is_full_sync true
:is_on_demand false}
database)
diff --git a/test/metabase/driver/mysql_test.clj b/test/metabase/driver/mysql_test.clj
index 9d7a99e8e12..a92e0f2a43d 100644
--- a/test/metabase/driver/mysql_test.clj
+++ b/test/metabase/driver/mysql_test.clj
@@ -494,6 +494,14 @@
(is (thrown-with-msg?
clojure.lang.ExceptionInfo #"dangerous"
(driver/validate-db-details! :mysql {:additional-options opt}))))))
+ (testing "...and so is the same property sent as a detail key, which the spec passes to the client too"
+ (doseq [details [{:host "h" :autoDeserialize true}
+ {:host "h" :allowLoadLocalInfile true}
+ {:host "h" :socketFactory "evil.SocketFactory"}]]
+ (testing (pr-str details)
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo #"dangerous"
+ (driver/validate-db-details! :mysql details))))))
(testing "benign additional options are still allowed"
(doseq [opt [nil "tinyInt1isBit=false" "useSSL=true&trustServerCertificate=true"]]
(is (nil? (driver/validate-db-details! :mysql {:additional-options opt}))))))
diff --git a/test/metabase/driver/sql_jdbc/connection_test.clj b/test/metabase/driver/sql_jdbc/connection_test.clj
index 98ad3efdca0..e159a151320 100644
--- a/test/metabase/driver/sql_jdbc/connection_test.clj
+++ b/test/metabase/driver/sql_jdbc/connection_test.clj
@@ -11,6 +11,7 @@
[clojure.test :refer :all]
[java-time.api :as t]
[metabase.app-db.core :as mdb]
+ [metabase.auth-provider.core :as auth-provider]
[metabase.config.core :as config]
[metabase.core.core :as mbc]
[metabase.driver :as driver]
@@ -62,6 +63,50 @@
(testing "Things that you can connect to, but are not DBs, should fail"
(is (not (driver.u/can-connect-with-details? :postgres {:host "google.com", :port 80}))))))
+(deftest disallowed-connection-parameters-checked-before-connecting-test
+ ;; `validate-db-details!` only runs when a connection is tested. A database that never was -- saved before a
+ ;; parameter was disallowed, imported through serialization, provisioned from a config file -- must still be refused
+ ;; when its pool is created.
+ (mt/test-drivers (mt/normal-driver-select {:+parent :sql-jdbc})
+ (doseq [details [{:host "db.example.com" :port 5432 :dbname "db" :additional-options "socketFactory=a.b.C"}
+ ;; several drivers hand detail keys they do not recognize to the client as connection properties
+ {:host "db.example.com" :port 5432 :dbname "db" :sslfactory "a.b.C"}]]
+ (testing (pr-str details)
+ (testing "when a connection pool is created"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo #"dangerous"
+ (#'sql-jdbc.conn/create-pool! (t2/instance :model/Database {:id Integer/MAX_VALUE
+ :engine driver/*driver*
+ :details details})))))
+ (testing "when a connection is tested"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo #"dangerous"
+ (sql-jdbc.conn/with-connection-spec-for-testing-connection [_spec [driver/*driver* details]]
+ ::reached))))))))
+
+(deftest disallowed-connection-parameters-from-auth-provider-test
+ ;; an auth provider's response is merged into the details (see [[driver/incorporate-auth-provider-details]]), so it
+ ;; can set a connection parameter the user never could
+ (mt/test-drivers (mt/normal-driver-select {:+parent :sql-jdbc})
+ (let [details {:host "db.example.com"
+ :port 5432
+ :dbname "db"
+ :use-auth-provider true
+ :auth-provider "http"
+ :http-auth-url "https://auth.example.com/token"}]
+ (mt/with-dynamic-fn-redefs [auth-provider/fetch-auth (constantly {:sslfactory "a.b.C"})]
+ (testing "when a connection pool is created"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo #"dangerous"
+ (#'sql-jdbc.conn/create-pool! (t2/instance :model/Database {:id Integer/MAX_VALUE
+ :engine driver/*driver*
+ :details details})))))
+ (testing "when a connection is tested"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo #"dangerous"
+ (sql-jdbc.conn/with-connection-spec-for-testing-connection [_spec [driver/*driver* details]]
+ ::reached))))))))
+
(deftest db->pooled-connection-spec-test
(mt/test-driver :h2
(testing "creating and removing specs works"
diff --git a/test/metabase/driver/sql_jdbc_test.clj b/test/metabase/driver/sql_jdbc_test.clj
index 96f1aa44b0c..ac42a7ce6d3 100644
--- a/test/metabase/driver/sql_jdbc_test.clj
+++ b/test/metabase/driver/sql_jdbc_test.clj
@@ -136,6 +136,29 @@
{:additional-options "prepareThreshold=5&tcpKeepAlive=true"}]]
(is (nil? (driver/validate-db-details! :sql-jdbc details))))))
+(deftest validate-db-details-detail-keys-test
+ ;; several drivers (postgres, mysql, redshift, vertica, snowflake, ...) hand detail keys they do not recognize to the
+ ;; client as connection properties, so a disallowed property sent as a detail key is as dangerous as one written
+ ;; into `:additional-options`
+ (testing "validate-db-details! rejects disallowed connection properties sent as detail keys"
+ (doseq [details [{:host "h" :socketFactory "a.b.C"}
+ {:host "h" :sslfactory "a.b.C"}
+ {:host "h" :queryInterceptors "a.b.C"}
+ {:host "h" "dnsResolver" "a.b.C"}
+ ;; matching is case-insensitive
+ {:host "h" :SOCKETFACTORY "a.b.C"}]]
+ (testing (pr-str details)
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo #"dangerous"
+ (driver/validate-db-details! :sql-jdbc details))))))
+ (testing "Metabase's own detail keys are allowed"
+ (is (nil? (driver/validate-db-details! :sql-jdbc {:host "h"
+ :ssl true
+ :ssl-root-cert-options "uploaded"
+ :ssl-root-cert-value "..."
+ :tunnel-enabled false
+ :additional-options "tcpKeepAlive=true"})))))
+
(defn- test-spliced-count-of [table filter-clause expected]
(let [query (mt/mbql-query nil
{:source-table (mt/id table)
diff --git a/test/metabase/warehouses/models/database_test.clj b/test/metabase/warehouses/models/database_test.clj
index 9c90db6599d..ae48ecc864c 100644
--- a/test/metabase/warehouses/models/database_test.clj
+++ b/test/metabase/warehouses/models/database_test.clj
@@ -818,6 +818,27 @@
#"private or internal network address"
(t2/update! :model/Database (:id db) {:admin_details {:host "127.0.0.1" :user "hummingbird"}})))))))
+(deftest disallowed-connection-parameters-are-refused-on-save-test
+ ;; like the network policy, enforced on the model so serialization import and config-file provisioning -- which
+ ;; write a Database without testing a connection -- cannot store one either
+ (testing "a disallowed connection parameter cannot be stored"
+ (doseq [details [{:host "db.example.com" :additional-options "socketFactory=a.b.C"}
+ {:host "db.example.com" :sslfactory "a.b.C"}]]
+ (testing (pr-str details)
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo #"dangerous"
+ (mt/with-temp [:model/Database _ {:engine :postgres :details details}]))))))
+ (testing "...including by adding it to an existing database"
+ (mt/with-temp [:model/Database db {:engine :postgres :details {:host "db.example.com"}}]
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo #"dangerous"
+ (t2/update! :model/Database (:id db) {:details {:host "db.example.com" :sslfactory "a.b.C"}})))))
+ (testing "a database whose driver isn't registered (e.g. an uninstalled plugin) can still be saved"
+ (mt/with-temp [:model/Database db {:engine "not-a-registered-driver" :details {:host "db.example.com"}}]
+ (t2/update! :model/Database (:id db) {:details {:host "db2.example.com"}})
+ (is (=? {:details {:host "db2.example.com"}}
+ (t2/select-one :model/Database (:id db)))))))
+
(deftest empty-details-are-not-subject-to-the-network-policy-test
(mt/with-temp-env-var-value! [mb-warehouse-allowed-networks "external-only"]
(testing "a database with empty details can be created"
diff --git a/test/metabase/warehouses_rest/api_test.clj b/test/metabase/warehouses_rest/api_test.clj
index bf521202d7c..7dea818480d 100644
--- a/test/metabase/warehouses_rest/api_test.clj
+++ b/test/metabase/warehouses_rest/api_test.clj
@@ -2823,15 +2823,19 @@
:ssl false,
:tunnel-enabled false,
:advanced-options false}
- new-details {:service-account-json "{\"type\": \"service_account\", \"project_id\": \"bigquery-project\"}",
- :dataset-filters-type "inclusion",
- :dataset-filters-patterns "dev",
- :project-id nil
+ new-details {:host "mysql.example.com",
+ :port 3306,
+ :dbname "shop",
+ :user "shop",
+ :password "password",
:advanced-options false}]
+ ;; saving a Database validates its details through the driver, which loads it on first use -- and a driver namespace
+ ;; cannot load while one of its multimethods is redefined below
+ (driver/the-initialized-driver :mysql)
(with-redefs [driver/can-connect? (constantly true)]
(testing "when the engine changes, the existing details are not merged into the new ones (#77480)"
(mt/with-temp [:model/Database {db-id :id} {:engine :postgres, :details existing-details}]
- (api-update-database! 200 db-id {:engine :bigquery-cloud-sdk, :details new-details})
+ (api-update-database! 200 db-id {:engine :mysql, :details new-details})
(is (= new-details
(t2/select-one-fn :details :model/Database :id db-id))))
(testing "without an engine change, existing details are still merged into partial updates"