Skip to content

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

Druid validation (#83286)

Nathan Voxlandf5f8cb4e76df
diff --git a/modules/drivers/druid-jdbc/src/metabase/driver/druid_jdbc.clj b/modules/drivers/druid-jdbc/src/metabase/driver/druid_jdbc.clj
index 9e18c74b6a1..bf15939ebca 100644
--- a/modules/drivers/druid-jdbc/src/metabase/driver/druid_jdbc.clj
+++ b/modules/drivers/druid-jdbc/src/metabase/driver/druid_jdbc.clj
@@ -15,6 +15,7 @@
[metabase.driver.sql.query-processor :as sql.qp]
[metabase.driver.sql.query-processor.util :as sql.qp.u]
[metabase.util.honey-sql-2 :as h2x]
+ [metabase.util.i18n :refer [tru]]
[metabase.util.json :as json]
[metabase.util.log :as log]
[metabase.util.performance :as perf :refer [mapv]])
@@ -40,17 +41,29 @@
:native-pivot-tables true}]
(defmethod driver/database-supports? [:druid-jdbc feature] [_driver _feature _db] supported?))
+(defn- checked-connect-string-part
+ "`value` of the `detail` written into the Avatica connect string, refused when it carries the connect string's own
+ syntax: `;` starts another connection property, and quotes regroup what follows."
+ [detail value]
+ (let [s (str value)]
+ (when (re-find #"[;'\"\s]" s)
+ (throw (ex-info (tru "Invalid {0}." (name detail))
+ {:status-code 400, :errors {detail (tru "must not contain spaces, quotes, or semicolons")}})))
+ s))
+
(defmethod sql-jdbc.conn/connection-details->spec :druid-jdbc
[driver {:keys [host port auth-enabled auth-username] :as db-details}]
- (merge {:classname "org.apache.calcite.avatica.remote.Driver"
- :subprotocol "avatica:remote"
- :subname (str "url=" host ":" port "/druid/v2/sql/avatica/;transparent_reconnection=true")}
- (when auth-enabled
- {:user auth-username
- :password (driver-api/secret-value-as-string driver db-details "auth-password")})
- (when (some? (driver/report-timezone))
- {:sqlTimeZone (driver/report-timezone)
- :timeZone (driver/report-timezone)})))
+ (let [host (checked-connect-string-part :host host)
+ port (checked-connect-string-part :port port)]
+ (merge {:classname "org.apache.calcite.avatica.remote.Driver"
+ :subprotocol "avatica:remote"
+ :subname (str "url=" host ":" port "/druid/v2/sql/avatica/;transparent_reconnection=true")}
+ (when auth-enabled
+ {:user auth-username
+ :password (driver-api/secret-value-as-string driver db-details "auth-password")})
+ (when (some? (driver/report-timezone))
+ {:sqlTimeZone (driver/report-timezone)
+ :timeZone (driver/report-timezone)}))))
(defmethod driver/db-default-timezone :druid-jdbc
[_driver _database]
diff --git a/modules/drivers/druid-jdbc/test/metabase/driver/druid_jdbc_test.clj b/modules/drivers/druid-jdbc/test/metabase/driver/druid_jdbc_test.clj
index 9efb6cd0535..8a0e8e73629 100644
--- a/modules/drivers/druid-jdbc/test/metabase/driver/druid_jdbc_test.clj
+++ b/modules/drivers/druid-jdbc/test/metabase/driver/druid_jdbc_test.clj
@@ -9,6 +9,7 @@
[metabase.driver :as driver]
[metabase.driver.common.table-rows-sample :as table-rows-sample]
[metabase.driver.settings :as driver.settings]
+ [metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]
[metabase.driver.sql-jdbc.sync.interface :as sql-jdbc.sync.interface]
[metabase.driver.util :as driver.u]
[metabase.lib-be.metadata.jvm :as lib.metadata.jvm]
@@ -514,6 +515,25 @@
{:aggregation [[:count]]
:filter [:> $__time "2015-10-01T00:00:00Z"]}))))))))
+(deftest ^:parallel host-and-port-cannot-add-connection-properties-test
+ ;; both are written into the Avatica connect string, where `;` separates properties and quotes group them
+ (testing "a host or port carrying connect-string syntax is refused"
+ (doseq [details [{:host "http://localhost;truststore=/tmp/x" :port 8082}
+ {:host "http://localhost" :port "8082;truststore=/tmp/x"}
+ {:host "http://localhost'" :port 8082}
+ {:host "http://localhost\"" :port 8082}
+ {:host "http://local host" :port 8082}
+ {:host "http://localhost" :port "80 82"}]]
+ (testing (pr-str details)
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"(?i)invalid"
+ (sql-jdbc.conn/connection-details->spec :druid-jdbc details))))))
+ (testing "ordinary hosts and ports are accepted"
+ (doseq [[details subname] [[{:host "http://localhost" :port 8082}
+ "url=http://localhost:8082/druid/v2/sql/avatica/;transparent_reconnection=true"]
+ [{:host "https://druid.example.com" :port "8082"}
+ "url=https://druid.example.com:8082/druid/v2/sql/avatica/;transparent_reconnection=true"]]]
+ (is (= subname (:subname (sql-jdbc.conn/connection-details->spec :druid-jdbc details)))))))
+
(deftest ssh-tunnel-test
(mt/test-driver
:druid-jdbc