Skip to content

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

Allow configuring readable/writable paths (#81794)

Ethan McCued8fa856fddba
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 31ada1aaede..158d539e120 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -2787,7 +2787,7 @@
{:team "UX West"
:api #{metabase.system.core
metabase.system.init}
- :uses #{appearance settings util}}
+ :uses #{appearance premium-features settings util}}
task
{:team "UX West"
@@ -3390,6 +3390,7 @@
lib.schema
models
premium-features
+ system
util}}
xrays
diff --git a/src/metabase/secrets/models/secret.clj b/src/metabase/secrets/models/secret.clj
index 5f08ba28187..b44cb453013 100644
--- a/src/metabase/secrets/models/secret.clj
+++ b/src/metabase/secrets/models/secret.clj
@@ -11,6 +11,7 @@
[metabase.models.interface :as mi]
[metabase.premium-features.core :as premium-features]
[metabase.secrets.db :as secrets.db]
+ [metabase.system.core :as system]
[metabase.util :as u]
[metabase.util.i18n :refer [tru]]
[metabase.util.log :as log]
@@ -233,14 +234,14 @@
(when-let [{source :source secret-value :value} (resolve-secret-map driver details secret-property)]
(let [s (unresolved-value-string secret-value)]
(if (= :file-path source)
- (slurp s)
+ (slurp (system/ensure-readable-path! s))
s))))
(defn- value-as-file*
[driver details secret-property & [ext]]
(when-let [{source :source secret-value :value secret-id :id} (resolve-secret-map driver details secret-property)]
(if (= :file-path source)
- (let [secret-value (unresolved-value-string secret-value)
+ (let [secret-value (system/ensure-readable-path! (unresolved-value-string secret-value))
^File existing-file (File. ^String secret-value)]
(if (.exists existing-file)
existing-file
diff --git a/src/metabase/system/core.clj b/src/metabase/system/core.clj
index c514d038034..cd6b038703c 100644
--- a/src/metabase/system/core.clj
+++ b/src/metabase/system/core.clj
@@ -1,11 +1,18 @@
(ns metabase.system.core
(:require
+ [metabase.system.paths]
[metabase.system.settings]
[potemkin :as p]))
-(comment metabase.system.settings/keep-me)
+(comment metabase.system.paths/keep-me
+ metabase.system.settings/keep-me)
(p/import-vars
+ [metabase.system.paths
+ ensure-readable-path!
+ ensure-writable-path!
+ readable-path?
+ writable-path?]
[metabase.system.settings
admin-email
admin-email!
diff --git a/src/metabase/system/paths.clj b/src/metabase/system/paths.clj
new file mode 100644
index 00000000000..2b8e77aaee4
--- /dev/null
+++ b/src/metabase/system/paths.clj
@@ -0,0 +1,52 @@
+(ns metabase.system.paths
+ "Checks for local file paths against the [[metabase.system.settings/readable-paths]] and
+ [[metabase.system.settings/writable-paths]] allowlists."
+ (:require
+ [metabase.system.settings :as system.settings]
+ [metabase.util :as u]
+ [metabase.util.i18n :refer [tru]])
+ (:import
+ (java.nio.file FileSystem FileSystems Path)))
+
+(set! *warn-on-reflection* true)
+
+(defn- ->normalized-path ^Path [^FileSystem fs ^String path]
+ ;; absolute first: normalizing a relative path cannot collapse a leading `..`
+ (-> (.getPath fs path (u/varargs String)) .toAbsolutePath .normalize))
+
+(defn- allowed-path?
+ "Whether `path` is one of the `allowlist` directories or under one. Compares normalized paths, so `..` cannot
+ escape an allowed directory, and whole path segments, so `/abc` does not allow `/abcd`. A `/` entry allows any
+ path; on Windows it would otherwise mean only the root of the current drive."
+ ([allowlist path]
+ (allowed-path? (FileSystems/getDefault) allowlist path))
+ ([^FileSystem fs allowlist path]
+ (or (boolean (some #{"/"} allowlist))
+ (let [path (->normalized-path fs path)]
+ (boolean (some #(.startsWith path (->normalized-path fs %)) allowlist))))))
+
+(defn readable-path?
+ "Whether Metabase may read the local file at `path`."
+ [path]
+ (allowed-path? (system.settings/readable-paths) path))
+
+(defn writable-path?
+ "Whether Metabase may write the local file at `path`."
+ [path]
+ (allowed-path? (system.settings/writable-paths) path))
+
+(defn ensure-readable-path!
+ "Returns `path` if Metabase may read it, otherwise throws a 400."
+ [path]
+ (when-not (readable-path? path)
+ (throw (ex-info (tru "Reading from path is disallowed: {0}" path)
+ {:file-path path, :access :read, :status-code 400})))
+ path)
+
+(defn ensure-writable-path!
+ "Returns `path` if Metabase may write it, otherwise throws a 400."
+ [path]
+ (when-not (writable-path? path)
+ (throw (ex-info (tru "Writing to path is disallowed: {0}" path)
+ {:file-path path, :access :write, :status-code 400})))
+ path)
diff --git a/src/metabase/system/settings.clj b/src/metabase/system/settings.clj
index e81ed6e721f..245720094e4 100644
--- a/src/metabase/system/settings.clj
+++ b/src/metabase/system/settings.clj
@@ -3,6 +3,7 @@
[clojure.string :as str]
[java-time.api :as t]
[metabase.appearance.core :as appearance]
+ [metabase.premium-features.core :as premium-features]
[metabase.settings.core :as setting :refer [defsetting]]
[metabase.util :as u]
[metabase.util.encryption :as encryption]
@@ -143,3 +144,35 @@
:setter :none
:getter encryption/default-encryption-enabled?
:doc false)
+
+(defn- env-path-allowlist
+ "The allowlist of paths for `setting-name`, read from its env var only. `NONE` allows no path. With nothing set,
+ hosted instances allow only `/tmp` and self-hosted ones allow every path."
+ [setting-name]
+ (if-let [raw (setting/env-var-value setting-name)]
+ (let [paths (into [] (comp (map str/trim) (remove str/blank?)) (str/split raw #","))]
+ (if (= paths ["NONE"]) [] paths))
+ (if (premium-features/is-hosted?) ["/tmp"] ["/"])))
+
+(defsetting readable-paths
+ (deferred-tru (str "Comma-separated allowlist of directories Metabase may read files from, e.g. for database "
+ "secrets given as a local file path. Use NONE to allow no paths. Defaults to /tmp on Metabase "
+ "Cloud and / (every path) when self-hosted."))
+ :type :csv
+ :encryption :no
+ :visibility :internal
+ :export? false
+ :setter :none
+ :getter #(env-path-allowlist :readable-paths)
+ :doc false)
+
+(defsetting writable-paths
+ (deferred-tru (str "Comma-separated allowlist of directories Metabase may write files to. Use NONE to allow no "
+ "paths. Defaults to /tmp on Metabase Cloud and / (every path) when self-hosted."))
+ :type :csv
+ :encryption :no
+ :visibility :internal
+ :export? false
+ :setter :none
+ :getter #(env-path-allowlist :writable-paths)
+ :doc false)
diff --git a/test/metabase/secrets/models/secret_test.clj b/test/metabase/secrets/models/secret_test.clj
index cf4db33b3ad..04284e91b8c 100644
--- a/test/metabase/secrets/models/secret_test.clj
+++ b/test/metabase/secrets/models/secret_test.clj
@@ -274,3 +274,47 @@
(is (= (keyword latest-source) (:source secret)))))
(testing "get-secret-string"
(is (= latest-value (secret/value-as-string :secret-test-driver details secret-property)))))))))
+
+(deftest secret-loading-from-path-test
+ (testing "Loading secrets from a path only works if it is under an allowed path"
+ (mt/with-temp-file [file-db "-1-key.pem"]
+ (spit file-db "apple")
+ (let [read-secret #(secret/value-as-string
+ :secret-test-driver
+ {:keystore-path file-db
+ :keystore-options "local"}
+ "keystore")]
+ (testing "Works when readable paths is set to root"
+ (mt/with-temp-env-var-value! [mb-readable-paths "/"]
+ (is (= "apple" (read-secret)))))
+ (testing "Cannot read when readable paths has no allowed values"
+ (mt/with-temp-env-var-value! [mb-readable-paths "NONE"]
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Reading from path is disallowed"
+ (read-secret)))))
+ (testing "Can read when the readable paths includes the file we want"
+ (mt/with-temp-env-var-value! [mb-readable-paths (str "/abc," file-db)]
+ (is (= "apple" (read-secret)))))
+ (testing "Cannot read when the readable paths doesn't include the file we want"
+ (mt/with-temp-env-var-value! [mb-readable-paths "/abc"]
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Reading from path is disallowed"
+ (read-secret)))))))))
+
+(deftest secret-file-from-path-test
+ (testing "value-as-file! only hands out a local file path under an allowed path"
+ (mt/with-temp-file [file-db "-1-key.pem"]
+ (spit file-db "apple")
+ (let [file-for #(secret/value-as-file!
+ :secret-test-driver
+ {:keystore-path %
+ :keystore-options "local"}
+ "keystore")]
+ (mt/with-temp-env-var-value! [mb-readable-paths "/abc"]
+ (testing "an existing file outside the allowlist"
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Reading from path is disallowed"
+ (file-for file-db))))
+ (testing "a missing file outside the allowlist does not reveal that it is missing"
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Reading from path is disallowed"
+ (file-for (str file-db ".missing"))))))
+ (mt/with-temp-env-var-value! [mb-readable-paths file-db]
+ (testing "a file under the allowlist"
+ (is (= "apple" (slurp (file-for file-db))))))))))
diff --git a/test/metabase/system/paths_test.clj b/test/metabase/system/paths_test.clj
new file mode 100644
index 00000000000..094c9ae2cce
--- /dev/null
+++ b/test/metabase/system/paths_test.clj
@@ -0,0 +1,82 @@
+(ns metabase.system.paths-test
+ (:require
+ [clojure.test :refer :all]
+ [metabase.system.core :as system]
+ [metabase.system.paths :as paths]
+ [metabase.test :as mt]
+ [metabase.test.util :as tu])
+ (:import
+ (com.google.common.jimfs Configuration Jimfs)))
+
+(set! *warn-on-reflection* true)
+
+(def ^:private checks
+ [[:read system/readable-path? system/ensure-readable-path! :mb-readable-paths #"Reading from path is disallowed"]
+ [:write system/writable-path? system/ensure-writable-path! :mb-writable-paths #"Writing to path is disallowed"]])
+
+(deftest allowed-path-test
+ (doseq [[access allowed? ensure! env-var disallowed-msg] checks]
+ (testing access
+ (tu/do-with-temp-env-var-value!
+ env-var "/allowed/dir,/other"
+ (fn []
+ (testing "paths under an allowed directory are allowed"
+ (is (true? (allowed? "/allowed/dir")))
+ (is (true? (allowed? "/allowed/dir/file.pem")))
+ (is (true? (allowed? "/other/deep/nested/file")))
+ (is (= "/allowed/dir/file.pem" (ensure! "/allowed/dir/file.pem"))))
+ (testing "paths outside every allowed directory are not"
+ (doseq [path ["/etc/passwd"
+ "/allowed"
+ ;; a sibling sharing a string prefix is not a descendant
+ "/allowed/directory/file"
+ ;; traversal is normalized before comparing
+ "/allowed/dir/../../etc/passwd"]]
+ (testing path
+ (is (false? (allowed? path)))
+ (is (=? {:file-path path, :access access, :status-code 400}
+ (try (ensure! path) nil (catch clojure.lang.ExceptionInfo e (ex-data e)))))
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo disallowed-msg (ensure! path))))))))
+ (testing "NONE allows nothing"
+ (tu/do-with-temp-env-var-value! env-var "NONE"
+ #(is (false? (allowed? "/allowed/dir/file.pem")))))
+ (testing "the root allows everything"
+ (tu/do-with-temp-env-var-value! env-var "/"
+ #(is (true? (allowed? "/etc/passwd"))))))))
+
+(deftest read-and-write-allowlists-are-independent-test
+ (mt/with-temp-env-var-value! [mb-readable-paths "/read"
+ mb-writable-paths "/write"]
+ (is (true? (system/readable-path? "/read/f")))
+ (is (false? (system/writable-path? "/read/f")))
+ (is (true? (system/writable-path? "/write/f")))
+ (is (false? (system/readable-path? "/write/f")))))
+
+(deftest relative-path-traversal-test
+ (testing "relative paths resolve against the working directory before `..` is collapsed"
+ (let [cwd (System/getProperty "user.dir")]
+ (doseq [[access allowed? _ensure! env-var] checks]
+ (testing access
+ (tu/do-with-temp-env-var-value!
+ env-var cwd
+ (fn []
+ (is (true? (allowed? "sub/file.pem")))
+ (is (false? (allowed? "../outside.pem")))
+ (is (false? (allowed? "sub/../../outside.pem"))))))))))
+
+(deftest windows-paths-test
+ (with-open [fs (Jimfs/newFileSystem (Configuration/windows))]
+ (let [allowed? (fn [allowlist path] (#'paths/allowed-path? fs allowlist path))]
+ (testing "/ allows any path, not only the current drive"
+ (doseq [path ["C:\\certs\\key.pem" "D:\\certs\\key.pem"]]
+ (is (true? (allowed? ["/"] path)) path)))
+ (testing "drive-letter allowlist entries"
+ (is (true? (allowed? ["C:\\certs"] "C:\\certs\\key.pem")))
+ (testing "either separator, any case"
+ (is (true? (allowed? ["C:/certs"] "c:\\CERTS\\key.pem"))))
+ (testing "another drive is not under it"
+ (is (false? (allowed? ["C:\\certs"] "D:\\certs\\key.pem")))
+ ;; which is why `/` cannot just be compared as a path: on Windows it is only the current drive's root
+ (is (false? (allowed? ["C:\\"] "D:\\certs\\key.pem"))))
+ (testing "traversal is normalized before comparing"
+ (is (false? (allowed? ["C:\\certs"] "C:\\certs\\..\\secrets\\key.pem"))))))))
diff --git a/test/metabase/system/settings_test.clj b/test/metabase/system/settings_test.clj
index 024610ef7ed..9399ee25233 100644
--- a/test/metabase/system/settings_test.clj
+++ b/test/metabase/system/settings_test.clj
@@ -5,6 +5,7 @@
[metabase.system.settings :as system.settings]
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
+ [metabase.test.util :as tu]
[metabase.util.i18n :as i18n :refer [tru]]))
(use-fixtures :once (fixtures/initialize :db))
@@ -126,3 +127,47 @@
(deftest site-locale-only-return-valid-locales-test
(mt/with-temporary-raw-setting-values [site-locale "wow_this_in_not_a_locale"]
(is (nil? (system.settings/site-locale)))))
+
+(def ^:private allowlist-settings
+ [[:readable-paths system.settings/readable-paths :mb-readable-paths]
+ [:writable-paths system.settings/writable-paths :mb-writable-paths]])
+
+(deftest allowed-paths-default-test
+ (doseq [[setting-name getter env-var] allowlist-settings]
+ (testing setting-name
+ (tu/do-with-temp-env-var-value!
+ env-var nil
+ (fn []
+ (testing "self-hosted, with nothing configured, every path is allowed"
+ (mt/with-premium-features #{}
+ (is (= ["/"] (getter)))))
+ (testing "hosted, with nothing configured, only /tmp is allowed"
+ (mt/with-premium-features #{:hosting}
+ (is (= ["/tmp"] (getter))))))))))
+
+(deftest allowed-paths-from-env-test
+ (doseq [[setting-name getter env-var] allowlist-settings
+ hosting [#{} #{:hosting}]]
+ (testing (str setting-name " with features " hosting)
+ (mt/with-premium-features hosting
+ (testing "the environment wins over the default, even when hosted"
+ (tu/do-with-temp-env-var-value! env-var "/a, /b/c ,,"
+ #(is (= ["/a" "/b/c"] (getter)))))
+ (testing "NONE allows no path"
+ (tu/do-with-temp-env-var-value! env-var "NONE"
+ #(is (= [] (getter)))))))))
+
+(deftest allowed-paths-are-environment-only-test
+ (testing "the allowlists are read from the environment only: a value that reached the setting table is ignored"
+ (mt/with-premium-features #{:hosting}
+ (mt/with-temp-env-var-value! [mb-readable-paths nil
+ mb-writable-paths nil]
+ (mt/with-temporary-raw-setting-values [readable-paths "/"
+ writable-paths "/"]
+ (is (= ["/tmp"] (system.settings/readable-paths)))
+ (is (= ["/tmp"] (system.settings/writable-paths)))))))
+ (testing "nothing can write them: they are read-only Settings"
+ (is (thrown-with-msg? UnsupportedOperationException #"read-only setting"
+ (setting/set! :readable-paths "/")))
+ (is (thrown-with-msg? UnsupportedOperationException #"read-only setting"
+ (setting/set! :writable-paths "/")))))