Skip to content

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

Apply the configured path settings to Kerberos and MySQL certificate file fields (#83498)

Nathan Voxland266eee8275ae
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 732eb196e41..d031c7cb7f7 100644
--- a/modules/drivers/presto-jdbc/src/metabase/driver/presto_jdbc.clj
+++ b/modules/drivers/presto-jdbc/src/metabase/driver/presto_jdbc.clj
@@ -477,7 +477,23 @@
:kerberos-service-principal-pattern "KerberosServicePrincipalPattern"
:kerberos-config-path "KerberosConfigPath"})
+(defn- check-kerberos-file-paths!
+ "The keytab, config and credential-cache paths are files on the Metabase host that an admin types in, so they have
+ to be somewhere `readable-paths` allows. A credential cache may also be named by type: `FILE:`/`DIR:` name a path,
+ the others (`KEYRING:`, `MEMORY:`, `KCM:`, ...) name no file."
+ [details]
+ (doseq [k [:kerberos-keytab-path :kerberos-config-path :kerberos-credential-cache-path]
+ :let [v (some-> (get details k) str str/trim)
+ path (cond
+ (str/blank? v) nil
+ (re-find #"(?i)^(FILE|DIR):" v) (str/replace v #"(?i)^(FILE|DIR):" "")
+ (re-find #"^[A-Za-z]{2,}:" v) nil
+ :else v)]
+ :when path]
+ (driver-api/ensure-readable-path! path)))
+
(defn- details->kerberos-url-params [details]
+ (check-kerberos-file-paths! details)
(let [remove-blank-vals (fn [m] (into {} (remove (comp str/blank? val) m)))
ks (keys kerb-props->url-param-names)]
(-> (select-keys details ks)
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 4d002c27ca3..a8351661e47 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
@@ -234,22 +234,43 @@
(let [db-details (assoc (:details (mt/db)) :let-user-control-scheduling false)]
(is (nil? (warehouses/test-database-connection :presto-jdbc db-details)))))))
-(deftest ^:parallel kerberos-properties-test
+(deftest kerberos-properties-test
(testing "Kerberos related properties are set correctly"
- (let [details {:host "presto-server"
- :port 7778
- :catalog "my-catalog"
- :kerberos true
- :ssl true
- :kerberos-config-path "/path/to/krb5.conf"
- :kerberos-principal "[email protected]"
- :kerberos-remote-service-name "HTTP"
- :kerberos-keytab-path "/path/to/client.keytab"}
- jdbc-spec (sql-jdbc.conn/connection-details->spec :presto-jdbc details)]
- (is (= (str "//presto-server:7778/[email protected]"
- "&KerberosRemoteServiceName=HTTP&KerberosKeytabPath=/path/to/client.keytab"
- "&KerberosConfigPath=/path/to/krb5.conf")
- (:subname jdbc-spec))))))
+ (mt/with-temp-env-var-value! [mb-readable-paths "/path/to"]
+ (let [details {:host "presto-server"
+ :port 7778
+ :catalog "my-catalog"
+ :kerberos true
+ :ssl true
+ :kerberos-config-path "/path/to/krb5.conf"
+ :kerberos-principal "[email protected]"
+ :kerberos-remote-service-name "HTTP"
+ :kerberos-keytab-path "/path/to/client.keytab"}
+ jdbc-spec (sql-jdbc.conn/connection-details->spec :presto-jdbc details)]
+ (is (= (str "//presto-server:7778/[email protected]"
+ "&KerberosRemoteServiceName=HTTP&KerberosKeytabPath=/path/to/client.keytab"
+ "&KerberosConfigPath=/path/to/krb5.conf")
+ (:subname jdbc-spec)))))))
+
+(deftest kerberos-file-paths-must-be-readable-test
+ (testing "the Kerberos keytab, config and credential-cache paths are files on the Metabase host an admin types in, so
+ they have to be somewhere `readable-paths` allows"
+ (let [details {:host "presto-server" :port 7778 :catalog "my-catalog" :kerberos true :ssl true
+ :kerberos-principal "[email protected]"}
+ spec #(sql-jdbc.conn/connection-details->spec :presto-jdbc (merge details %))]
+ (mt/with-temp-env-var-value! [mb-readable-paths "/allowed-dir"]
+ (doseq [extra [{:kerberos-keytab-path "/etc/krb5.keytab"}
+ {:kerberos-config-path "/etc/krb5.conf"}
+ {:kerberos-credential-cache-path "/tmp/../etc/krb5cc"}
+ {:kerberos-credential-cache-path "FILE:/etc/krb5cc"}]]
+ (testing (pr-str extra)
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Reading from path is disallowed" (spec extra)))))
+ (testing "paths inside an allowed directory, and credential caches that are not files, are accepted"
+ (doseq [extra [{:kerberos-keytab-path "/allowed-dir/client.keytab"
+ :kerberos-config-path "/allowed-dir/krb5.conf"}
+ {:kerberos-credential-cache-path "FILE:/allowed-dir/krb5cc"}
+ {:kerberos-credential-cache-path "KEYRING:persistent:1000"}]]
+ (is (map? (spec extra)) (pr-str extra))))))))
(defn- create-dummy-keystore
"Creates and empty file for simulating a JKS."
diff --git a/modules/drivers/starburst/src/metabase/driver/starburst.clj b/modules/drivers/starburst/src/metabase/driver/starburst.clj
index 464b0ad4fe8..76af24925d7 100644
--- a/modules/drivers/starburst/src/metabase/driver/starburst.clj
+++ b/modules/drivers/starburst/src/metabase/driver/starburst.clj
@@ -918,7 +918,23 @@
:kerberos-config-path "KerberosConfigPath"
:kerberos-delegation "KerberosDelegation"})
+(defn- check-kerberos-file-paths!
+ "The keytab, config and credential-cache paths are files on the Metabase host that an admin types in, so they have
+ to be somewhere `readable-paths` allows. A credential cache may also be named by type: `FILE:`/`DIR:` name a path,
+ the others (`KEYRING:`, `MEMORY:`, `KCM:`, ...) name no file."
+ [details]
+ (doseq [k [:kerberos-keytab-path :kerberos-config-path :kerberos-credential-cache-path]
+ :let [v (some-> (get details k) str str/trim)
+ path (cond
+ (str/blank? v) nil
+ (re-find #"(?i)^(FILE|DIR):" v) (str/replace v #"(?i)^(FILE|DIR):" "")
+ (re-find #"^[A-Za-z]{2,}:" v) nil
+ :else v)]
+ :when path]
+ (driver-api/ensure-readable-path! path)))
+
(defn- details->kerberos-url-params [details]
+ (check-kerberos-file-paths! details)
(let [remove-blank-vals (fn [m] (into {} (remove (comp str/blank? val) m)))
ks (keys kerb-props->url-param-names)]
(-> (select-keys details ks)
diff --git a/modules/drivers/starburst/test/metabase/driver/starburst_test.clj b/modules/drivers/starburst/test/metabase/driver/starburst_test.clj
index 1d57c4b20b1..1e8f33cdd7c 100644
--- a/modules/drivers/starburst/test/metabase/driver/starburst_test.clj
+++ b/modules/drivers/starburst/test/metabase/driver/starburst_test.clj
@@ -250,21 +250,42 @@
(deftest kerberos-properties-test
(testing "Kerberos related properties are set correctly"
- (let [details {:host "starburst-server"
- :port 7778
- :catalog "my-catalog"
- :kerberos true
- :ssl true
- :kerberos-config-path "/path/to/krb5.conf"
- :kerberos-principal "[email protected]"
- :kerberos-remote-service-name "HTTP"
- :kerberos-keytab-path "/path/to/client.keytab"
- :kerberos-delegation true}
- jdbc-spec (sql-jdbc.conn/connection-details->spec :starburst details)]
- (is (= (str "//starburst-server:7778/[email protected]"
- "&KerberosRemoteServiceName=HTTP&KerberosKeytabPath=/path/to/client.keytab"
- "&KerberosConfigPath=/path/to/krb5.conf&KerberosDelegation=true")
- (:subname jdbc-spec))))))
+ (mt/with-temp-env-var-value! [mb-readable-paths "/path/to"]
+ (let [details {:host "starburst-server"
+ :port 7778
+ :catalog "my-catalog"
+ :kerberos true
+ :ssl true
+ :kerberos-config-path "/path/to/krb5.conf"
+ :kerberos-principal "[email protected]"
+ :kerberos-remote-service-name "HTTP"
+ :kerberos-keytab-path "/path/to/client.keytab"
+ :kerberos-delegation true}
+ jdbc-spec (sql-jdbc.conn/connection-details->spec :starburst details)]
+ (is (= (str "//starburst-server:7778/[email protected]"
+ "&KerberosRemoteServiceName=HTTP&KerberosKeytabPath=/path/to/client.keytab"
+ "&KerberosConfigPath=/path/to/krb5.conf&KerberosDelegation=true")
+ (:subname jdbc-spec)))))))
+
+(deftest kerberos-file-paths-must-be-readable-test
+ (testing "the Kerberos keytab, config and credential-cache paths are files on the Metabase host an admin types in, so
+ they have to be somewhere `readable-paths` allows"
+ (let [details {:host "starburst-server" :port 7778 :catalog "my-catalog" :kerberos true :ssl true
+ :kerberos-principal "[email protected]"}
+ spec #(sql-jdbc.conn/connection-details->spec :starburst (merge details %))]
+ (mt/with-temp-env-var-value! [mb-readable-paths "/allowed-dir"]
+ (doseq [extra [{:kerberos-keytab-path "/etc/krb5.keytab"}
+ {:kerberos-config-path "/etc/krb5.conf"}
+ {:kerberos-credential-cache-path "/tmp/../etc/krb5cc"}
+ {:kerberos-credential-cache-path "FILE:/etc/krb5cc"}]]
+ (testing (pr-str extra)
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Reading from path is disallowed" (spec extra)))))
+ (testing "paths inside an allowed directory, and credential caches that are not files, are accepted"
+ (doseq [extra [{:kerberos-keytab-path "/allowed-dir/client.keytab"
+ :kerberos-config-path "/allowed-dir/krb5.conf"}
+ {:kerberos-credential-cache-path "FILE:/allowed-dir/krb5cc"}
+ {:kerberos-credential-cache-path "KEYRING:persistent:1000"}]]
+ (is (map? (spec extra)) (pr-str extra))))))))
(deftest source-property-test
(testing "source property is set correctly"
diff --git a/src/metabase/driver/mysql.clj b/src/metabase/driver/mysql.clj
index d0871e92901..00e7125ca12 100644
--- a/src/metabase/driver/mysql.clj
+++ b/src/metabase/driver/mysql.clj
@@ -31,7 +31,7 @@
[metabase.util :as u]
[metabase.util.date-2 :as u.date]
[metabase.util.honey-sql-2 :as h2x]
- [metabase.util.i18n :refer [deferred-tru]]
+ [metabase.util.i18n :refer [deferred-tru tru]]
[metabase.util.log :as log]
[metabase.util.malli :as mu]
[metabase.util.memoize :as memoize]
@@ -40,6 +40,7 @@
[next.jdbc :as next.jdbc])
(:import
(java.io File)
+ (java.net MalformedURLException URL URLDecoder)
(java.sql Connection DatabaseMetaData ResultSet ResultSetMetaData SQLException Types)
(java.time LocalDateTime OffsetDateTime OffsetTime ZonedDateTime ZoneOffset)
(java.time.format DateTimeFormatter)))
@@ -53,11 +54,15 @@
(driver/register! :mysql, :parent #{:sql-jdbc ::like-escape-char-built-in/like-escape-char-built-in})
-(defmethod driver/host-carrying-parameters :mysql [_driver] [])
+(defmethod driver/host-carrying-parameters :mysql
+ [_driver]
+ ;; the client fetches `serverSslCert` when it is a URL, and a `file:` URL naming a host other than localhost is
+ ;; fetched over FTP
+ ["serverSslCert"])
(defmethod driver/non-host-parameters :mysql
[_driver]
- ["disableSslHostnameVerification" "localSocketAddress" "serverRsaPublicKeyFile" "serverSslCert" "serverTimezone"
+ ["disableSslHostnameVerification" "localSocketAddress" "serverRsaPublicKeyFile" "serverTimezone"
"tcpNoDelay" "trustServerCertificate" "useServerPrepStmts"])
(def ^:private ^:const min-supported-mysql-version 5.7)
@@ -751,6 +756,29 @@
[spec allowed?]
(sql-jdbc.common/handle-additional-options spec {:additional-options (str "allowLocalInfile=" allowed?)}))
+(defn- ssl-cert-url-local-path
+ "The local path a `file:` URL reads, or nil if `url` reads from somewhere else. A `file:` URL naming any host but
+ `localhost` is fetched over FTP."
+ [^URL url]
+ (when (and (= "file" (.getProtocol url))
+ (#{"" "localhost"} (u/lower-case-en (.getHost url))))
+ ;; the URL path is percent-encoded, but a `+` in it is a literal `+`
+ (URLDecoder/decode (str/replace (.getPath url) "+" "%2B") "UTF-8")))
+
+(defn- ensure-ssl-cert-readable!
+ "Throw unless `readable-paths` allows what the client reads for `ssl-cert`. Mirrors the client's own reading of
+ `serverSslCert`: inline PEM only when it starts with exactly `-----BEGIN CERTIFICATE-----`, a classpath resource
+ with `classpath:`, then a URL if it parses as one, and only otherwise a file on the Metabase host. A `file:` URL
+ reads its local path; any other URL reads from outside the allowlist, so is allowed only if every path is."
+ [^String ssl-cert]
+ (when-not (or (str/starts-with? ssl-cert "-----BEGIN CERTIFICATE-----")
+ (str/starts-with? ssl-cert "classpath:"))
+ (if-let [url (try (URL. ssl-cert) (catch MalformedURLException _ nil))]
+ (when-not (driver-api/readable-path? (or (ssl-cert-url-local-path url) "/"))
+ (throw (ex-info (tru "Reading from path is disallowed: {0}" ssl-cert)
+ {:file-path ssl-cert, :access :read, :status-code 400})))
+ (driver-api/ensure-readable-path! ssl-cert))))
+
(defmethod sql-jdbc.conn/connection-details->spec :mysql
[_ {ssl? :ssl, :keys [additional-options ssl-cert auth-provider], :as details}]
;; In versions older than 0.32.0 the MySQL driver did not correctly save `ssl?` connection status. Users worked
@@ -765,6 +793,8 @@
(log/info "You may need to add 'trustServerCertificate=true' to the additional connection options to connect with SSL."))
(when (and use-iam? (not ssl?))
(throw (ex-info "You must enable SSL in order to use AWS IAM authentication" {})))
+ (when (and ssl-cert? (string? ssl-cert))
+ (ensure-ssl-cert-readable! ssl-cert))
(when (and use-iam?
(contains? addl-opts-map "sslMode")
(not= (get addl-opts-map "sslMode") "VERIFY_CA"))
diff --git a/src/metabase/driver_api/core.clj b/src/metabase/driver_api/core.clj
index 7625e8f7026..d57781c4f71 100644
--- a/src/metabase/driver_api/core.clj
+++ b/src/metabase/driver_api/core.clj
@@ -179,6 +179,7 @@
sync-util/reducible-sync-tables
system/ensure-readable-path!
system/ensure-writable-path!
+ system/readable-path?
system/site-uuid
upload.db/current-database)
diff --git a/test/metabase/driver/mysql_test.clj b/test/metabase/driver/mysql_test.clj
index fcfd91f00d0..c69e45b14ac 100644
--- a/test/metabase/driver/mysql_test.clj
+++ b/test/metabase/driver/mysql_test.clj
@@ -54,6 +54,66 @@
(set! *warn-on-reflection* true)
+(deftest ssl-cert-path-must-be-readable-test
+ ;; the "Server SSL certificate chain" field takes inline PEM, a `classpath:` resource, or a path to a file on the
+ ;; Metabase host -- and a path has to be somewhere `readable-paths` allows
+ (mt/with-temp-env-var-value! [mb-readable-paths "/allowed-dir"]
+ (let [details {:host "h" :port 3306 :dbname "db" :user "u" :ssl true}
+ spec #(sql-jdbc.conn/connection-details->spec :mysql (merge details %))]
+ (testing "a certificate path outside the allowed directories is refused"
+ (doseq [cert ["/etc/server-ca.pem" "/allowed-dir/../etc/server-ca.pem"]]
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Reading from path is disallowed"
+ (spec {:ssl-cert cert}))
+ cert)))
+ (testing "inline PEM, a classpath resource, an allowed path, or a certificate SSL does not use, are accepted"
+ (doseq [extra [{:ssl-cert "-----BEGIN CERTIFICATE-----\nMIIB\n-----END CERTIFICATE-----"}
+ {:ssl-cert "classpath:server-ca.pem"}
+ {:ssl-cert "/allowed-dir/server-ca.pem"}
+ {:ssl false :ssl-cert "/etc/server-ca.pem"}]]
+ (is (map? (spec extra)) (pr-str extra)))))))
+
+(deftest ssl-cert-url-must-be-readable-test
+ ;; the client opens the value as a URL before falling back to a file path, so `file:/etc/x` reads `/etc/x` -- not a
+ ;; file named `file:/etc/x` under the working directory, which is allowed here
+ (mt/with-temp-env-var-value! [mb-readable-paths (str "/allowed-dir," (System/getProperty "user.dir"))]
+ (let [details {:host "h" :port 3306 :dbname "db" :user "u" :ssl true}
+ spec #(sql-jdbc.conn/connection-details->spec :mysql (merge details {:ssl-cert %}))]
+ (testing "a URL the client would read from somewhere other than an allowed local file is refused"
+ (doseq [cert ["file:/etc/server-ca.pem"
+ "file:///etc/server-ca.pem"
+ "file:/allowed-dir/%2E%2E/etc/server-ca.pem"
+ "file://other-host/allowed-dir/server-ca.pem"
+ "jar:file:/allowed-dir/certs.jar!/server-ca.pem"
+ "http://example.com/server-ca.pem"]]
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Reading from path is disallowed"
+ (spec cert))
+ cert)))
+ (testing "a file URL for an allowed path is accepted"
+ (doseq [cert ["file:/allowed-dir/server-ca.pem"
+ "file:///allowed-dir/server-ca.pem"
+ "file://localhost/allowed-dir/server-ca.pem"]]
+ (is (map? (spec cert)) cert)))))
+ (testing "any URL is accepted when every path is readable"
+ (mt/with-temp-env-var-value! [mb-readable-paths "/"]
+ (is (map? (sql-jdbc.conn/connection-details->spec
+ :mysql
+ {:host "h" :port 3306 :dbname "db" :user "u" :ssl true :ssl-cert "http://example.com/server-ca.pem"}))))))
+
+(deftest ssl-cert-url-host-is-a-connection-parameter-host-test
+ ;; a URL in the certificate field is fetched by the client, so the host it names is one the connection reaches and
+ ;; has to pass the network policy like any other. Every path is readable here, which is the self-hosted default,
+ ;; so the readable-paths check does not refuse the URL first.
+ (mt/with-temp-env-var-value! [mb-readable-paths "/"]
+ (let [details {:host "h" :port 3306 :dbname "db" :user "u" :ssl true}
+ hosts #(set (driver/connection-parameter-hosts :mysql (merge details %)))]
+ (testing "the certificate field"
+ (is (contains? (hosts {:ssl-cert "http://10.0.0.1/server-ca.pem"}) "10.0.0.1"))
+ (is (contains? (hosts {:ssl-cert "file://10.0.0.2/server-ca.pem"}) "10.0.0.2")))
+ (testing "serverSslCert in the additional options"
+ ;; a name rather than an address: an undeclared parameter of the connection string is only read for an address
+ (is (contains? (hosts {:additional-options "serverSslCert=http://certs.example/server-ca.pem"})
+ "certs.example"))))))
+
(deftest default-schema-test
(mt/test-driver :mysql
(is (nil? (driver.sql/default-schema :mysql (mt/db))))))