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
Validate local infile (#81967)
Nathan Voxlandd3c7576ccb3c
diff --git a/src/metabase/driver/mysql.clj b/src/metabase/driver/mysql.cljindex f5be6ccc7e3..5aef1ed1a3b 100644--- a/src/metabase/driver/mysql.clj+++ b/src/metabase/driver/mysql.clj@@ -221,7 +221,7 @@"All Metabase features may not work properly when using an unsupported version.""\n********************************************************************************\n"))))))))-(def ^:private disallowed-additional-opts #"(?:allowLoadLocalInfile|allowLoadLocalInfileInPath|allowUrlInLocalInfile|autoDeserialize|serverRSAPublicKeyFile)")+(def ^:private disallowed-additional-opts #"(?:allowLocalInfile|allowLoadLocalInfile|allowLoadLocalInfileInPath|allowUrlInLocalInfile|autoDeserialize|serverRSAPublicKeyFile)")(defmethod driver/validate-db-details! :mysql[_driver details]@@ -740,6 +740,14 @@(set-prog-nm-fn))(set-prog-nm-fn)))) ; additional-options did not contain connectionAttributes at all; set it+(defn- set-local-infile+ "Pin `allowLocalInfile` to `allowed?` on `spec`'s connection string.++ Appending is what makes this stick: the driver lets URL parameters override connection `Properties`, and lets a later+ duplicate parameter override an earlier one, so this only wins if it comes after `:additional-options`."+ [spec allowed?]+ (sql-jdbc.common/handle-additional-options spec {:additional-options (str "allowLocalInfile=" allowed?)}))+(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@@ -779,7 +787,8 @@(dissoc :ssl)))](-> (driver-api/spec :mysql details)(maybe-add-program-name-option addl-opts-map)- (sql-jdbc.common/handle-additional-options details))))))+ (sql-jdbc.common/handle-additional-options details)+ (set-local-infile false))))))(defmethod sql-jdbc.sync/active-tables :mysql[& args]@@ -1072,12 +1081,14 @@(with-open [^java.io.Writer writer (jio/writer file-path)](doseq [value (interpose \newline tsvs)](.write writer (str value))))- (sql-jdbc.execute/do-with-connection-with-options- driver- db-id- nil- (fn [conn]- (jdbc/execute! {:connection conn} sql))))+ ;; Bulk-loading needs a connection that will service `LOAD DATA LOCAL INFILE`, which the pooled connections+ ;; refuse (see [[set-local-infile]]), so open a dedicated unpooled one for this statement. That is safe here+ ;; where it is not for user-supplied SQL: the driver only sends the server the file named in the statement,+ ;; and this statement names the temp file we just wrote.+ (driver-api/with-metadata-provider db-id+ (let [details (driver.conn/effective-details (driver-api/database (driver-api/metadata-provider)))]+ (sql-jdbc.conn/with-connection-spec-for-testing-connection [spec [driver details]]+ (jdbc/execute! (set-local-infile spec true) sql)))))(finally(.delete temp-file))))))diff --git a/test/metabase/driver/mysql_test.clj b/test/metabase/driver/mysql_test.cljindex 36c78cc18ec..7c1734c5f4f 100644--- a/test/metabase/driver/mysql_test.clj+++ b/test/metabase/driver/mysql_test.clj@@ -45,7 +45,11 @@[metabase.util :as u][metabase.util.honey-sql-2 :as h2x][metabase.util.log :as log]- [toucan2.core :as t2]))+ [toucan2.core :as t2])+ (:import+ (java.io File)+ (java.util Properties)+ (org.mariadb.jdbc UrlParser)))(set! *warn-on-reflection* true)@@ -373,7 +377,7 @@(deftest ^:parallel connection-spec-test-3(testing "Connections that are `:ssl false` but with `useSSL` in the additional options should be treated as SSL (see #9629)"- (is (=? {:useSSL true, :subname "//localhost:3306/my_db?useSSL=true&trustServerCertificate=true"}+ (is (=? {:useSSL true, :subname "//localhost:3306/my_db?useSSL=true&trustServerCertificate=true&allowLocalInfile=false"}(sql-jdbc.conn/connection-details->spec :mysql(assoc sample-connection-details:ssl false@@ -382,7 +386,7 @@(deftest ^:parallel connection-spec-test-4(testing "A program_name specified in additional-options is not overwritten by us"(let [conn-attrs "connectionAttributes=program_name:my_custom_value"]- (is (=? {:subname (str "//localhost:3306/my_db?" conn-attrs)+ (is (=? {:subname (str "//localhost:3306/my_db?" conn-attrs "&allowLocalInfile=false"):useSSL false;; because program_name was in additional-options, we shouldn't use emit :connectionAttributes:connectionAttributes (symbol "nil #_\"key is not present.\"")}@@ -390,6 +394,63 @@:mysql(assoc sample-connection-details :additional-options conn-attrs)))))))+(defn- spec->allow-local-infile?+ "Whether the MariaDB JDBC driver would let a server ask `spec`'s connection to read a file off the Metabase host.+ Asks the driver's own URL parser rather than eyeballing the connection string, because URL parameters and connection+ `Properties` do not carry equal weight and the last duplicate URL parameter wins."+ [{:keys [subprotocol subname] :as spec}]+ (let [props (Properties.)]+ (doseq [[k v] (dissoc spec :classname :subprotocol :subname)]+ (.setProperty props (name k) (str v)))+ (.-allowLocalInfile (.getOptions (UrlParser/parse (str "jdbc:" subprotocol ":" subname) props)))))++(deftest ^:parallel local-infile-always-disabled-test+ (testing "connections never honor `LOAD DATA LOCAL INFILE`"+ (are [additional-options] (false? (spec->allow-local-infile?+ (sql-jdbc.conn/connection-details->spec+ :mysql+ (cond-> sample-connection-details+ additional-options (assoc :additional-options additional-options)))))+ nil+ "allowLocalInfile=true"+ "useSSL=true&allowLocalInfile=true"+ ;; the driver lets a later duplicate win, so ours has to be appended after whatever the user wrote+ "allowLocalInfile=false&allowLocalInfile=true")))++(deftest ^:synchronized local-infile-blocked-for-write-queries-test+ (mt/test-driver :mysql+ (testing "a write query cannot make the driver read a file off the Metabase host"+ ;; Write queries — query actions, transforms — reach the database without the `-- Metabase::` remark that native+ ;; queries carry, and that remark was the only thing keeping the driver from treating user SQL as a local-infile+ ;; request. The connection now refuses local infile outright, so the remark no longer matters.+ (let [spec (sql-jdbc.conn/connection-details->spec driver/*driver* (:details (mt/db)))+ temp-file (File/createTempFile "local-infile" ".txt")]+ (try+ (spit temp-file "secret-from-the-metabase-host\n")+ (jdbc/execute! spec "DROP TABLE IF EXISTS local_infile_loot")+ (jdbc/execute! spec "CREATE TABLE local_infile_loot (line TEXT)")+ (qp.store/with-metadata-provider (mt/id)+ (is (thrown? clojure.lang.ExceptionInfo+ (driver/execute-write-query!+ driver/*driver*+ {:type :native+ :native {:query (format "LOAD DATA LOCAL INFILE '%s' INTO TABLE local_infile_loot"+ (.getAbsolutePath temp-file))}}))))+ (is (= [] (jdbc/query spec "SELECT * FROM local_infile_loot"))+ "nothing from the Metabase host made it into the warehouse")+ (finally+ (jdbc/execute! spec "DROP TABLE IF EXISTS local_infile_loot")+ (.delete temp-file)))))))++(deftest ^:parallel local-infile-enabled-for-uploads-test+ (testing "the dedicated connection uploads bulk-load through is the one exception"+ (is (true? (spec->allow-local-infile?+ (#'mysql/set-local-infile+ (sql-jdbc.conn/connection-details->spec+ :mysql+ (assoc sample-connection-details :additional-options "allowLocalInfile=false"))+ true))))))+(deftest read-timediffs-test(mt/test-driver :mysql(testing "Make sure negative result of *diff() functions don't cause Exceptions (#10983)"@@ -1091,6 +1152,7 @@(driver/can-connect? :mysql details))))"allowLoadLocalInfile=true""allowLoadLocalInfileInPath=1"+ "allowLocalInfile=true""allowUrlInLocalInfile=1""autoDeserialize=1""serverRSAPublicKeyFile=/path/to/file"))