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
Improve *-allowed-network validation (#82519)
Nathan Voxlandb485c0f7fa44
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 6792268c3bc..1c47ea4e99a 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -551,6 +551,7 @@query-processor.pivotrequestsettings+ startupsystemtasktiles@@ -934,6 +935,7 @@query-processor.driver-apisettingssql-tools+ startupsystemtracingutil@@ -1568,6 +1570,7 @@settingssetupsql-tools+ startupsyncutilutil.config@@ -2637,6 +2640,7 @@serversettingssso.auth-identity+ startupsystemutilutil.config}@@ -2812,6 +2816,7 @@premium-featuresquery-processorsettings+ startuputil}:model-imports #{:model/Card :model/Dashboard :model/DashboardCard}}diff --git a/enterprise/backend/test/metabase_enterprise/advanced_permissions/api/channel_test.clj b/enterprise/backend/test/metabase_enterprise/advanced_permissions/api/channel_test.cljindex a0b60889236..a7ba579eb15 100644--- a/enterprise/backend/test/metabase_enterprise/advanced_permissions/api/channel_test.clj+++ b/enterprise/backend/test/metabase_enterprise/advanced_permissions/api/channel_test.clj@@ -30,7 +30,7 @@(test-channel [user status](testing (format "test channel with %s user" (mt/user-descriptor user))(channel.http-test/with-server [url [channel.http-test/post-200 channel.http-test/post-400]]- (mt/with-temporary-setting-values [http-channel-allowed-networks :allow-all]+ (mt/with-temp-env-var-value! [mb-http-channel-allowed-networks "allow-all"](mt/user-http-request user :post status "channel/test" {:type "channel/http":details {:url (str url (:path channel.http-test/post-200)):auth-method "none"diff --git a/src/metabase/channel/settings.clj b/src/metabase/channel/settings.cljindex a9d4052a65f..7fee088266d 100644--- a/src/metabase/channel/settings.clj+++ b/src/metabase/channel/settings.clj@@ -4,6 +4,8 @@[java-time.api :as t][metabase.config.core :as config][metabase.settings.core :as setting :refer [defsetting]]+ [metabase.startup.core :as startup]+ [metabase.util.http :as u.http][metabase.util.i18n :refer [deferred-tru tru]][metabase.util.malli.registry :as mr][metabase.util.malli.schema :as ms]@@ -326,11 +328,18 @@:default :external-only:export? false:deprecated-name :http-channel-host-strategy- :setter (fn [new-value]- (when (some? new-value)- (assert (#{:external-only :allow-private :allow-all} (keyword new-value))- (tru "Invalid http-channel-allowed-networks! Only values of external-only, allow-private, and allow-all are allowed.")))- (setting/set-value-of-type! :keyword :http-channel-allowed-networks new-value)))+ :setter :none+ :doc (str "Set this when a notification webhook must reach a host on your private network "+ "(`allow-private`) or on this machine (`allow-all`). Default is `external-only`")+ :getter (fn []+ (let [[env-var-name raw-value] (setting/env-var-source :http-channel-allowed-networks)]+ (or (u.http/env-network-policy env-var-name raw-value)+ :external-only))))++;; Reading it throws when the environment names a policy that does not exist: a typo stops the boot rather than+;; surfacing at the first webhook.+(defmethod startup/def-startup-validation! ::http-channel-allowed-networks [_]+ (http-channel-allowed-networks))(defsetting slack-configured?"Is Slack integration configured?"diff --git a/src/metabase/driver/settings.clj b/src/metabase/driver/settings.cljindex c727c87aed3..ed8be139378 100644--- a/src/metabase/driver/settings.clj+++ b/src/metabase/driver/settings.clj@@ -5,8 +5,10 @@[metabase.events.core :as events][metabase.premium-features.core :as premium-features][metabase.settings.core :as setting :refer [defsetting]]+ [metabase.startup.core :as startup][metabase.util :as u]- [metabase.util.i18n :refer [deferred-tru tru]]))+ [metabase.util.http :as u.http]+ [metabase.util.i18n :refer [deferred-tru]]))(set! *warn-on-reflection* true)@@ -21,21 +23,30 @@:type :keyword:visibility :internal:export? false+ ;; Environment only. This policy defends the host Metabase runs on against the people who administer Metabase --+ ;; on Cloud, an admin loosening it would be reaching for our own infrastructure -- so it is never settable through+ ;; the API or a config file, and a value that reached the application database some other way is ignored.+ :setter :none+ :doc (str "Set this when Metabase must reach a warehouse on a private network (allow-private) or on this "+ "machine (allow-all). There is no admin UI for it, and a value stored in the application "+ "database is ignored. Defaults to external-only on Metabase Cloud and allow-all when "+ "self-hosted. Metabase refuses to start if this is set to anything but one of the three "+ "policies, rather than run on a policy nobody chose.");; No `:default`, because it depends on where we are running. On Cloud a warehouse is always reached across the;; public internet, so an internal address is somebody reaching for our own infrastructure rather than their;; database. Self-hosted, a warehouse on a private network is the ordinary case, and defaulting to anything;; stricter would break working instances on upgrade.:getter (fn []- (or (setting/get-value-of-type :keyword :warehouse-allowed-networks)- (if (premium-features/is-hosted?)- :external-only- :allow-all)))- :setter (fn [new-value]- (when (some? new-value)- (assert (#{:external-only :allow-private :allow-all} (keyword new-value))- (tru (str "Invalid warehouse-allowed-networks! Only values of `external-only`, "- "`allow-private`,` and `allow-all` are allowed."))))- (setting/set-value-of-type! :keyword :warehouse-allowed-networks new-value)))+ (let [[env-var-name raw-value] (setting/env-var-source :warehouse-allowed-networks)]+ (or (u.http/env-network-policy env-var-name raw-value)+ (if (premium-features/is-hosted?)+ :external-only+ :allow-all)))))++;; Reading it throws when the environment names a policy that does not exist: a typo stops the boot rather than+;; surfacing at the first query against a warehouse.+(defmethod startup/def-startup-validation! ::warehouse-allowed-networks [_]+ (warehouse-allowed-networks))(defsetting ssh-heartbeat-interval-sec(deferred-tru "Controls how often the heartbeats are sent when an SSH tunnel is established (in seconds).")diff --git a/src/metabase/llm/provider/settings.clj b/src/metabase/llm/provider/settings.cljindex b0eaa936e42..842a2989979 100644--- a/src/metabase/llm/provider/settings.clj+++ b/src/metabase/llm/provider/settings.clj@@ -10,6 +10,7 @@[metabase.premium-features.core :as premium-features][metabase.request.current :as request.current][metabase.settings.core :as setting :refer [defsetting]]+ [metabase.startup.core :as startup][metabase.util :as u][metabase.util.http :as u.http][metabase.util.i18n :refer [deferred-tru tru]]@@ -64,15 +65,8 @@;;; ------------------------------------------------- Network policy ----------------------------------------------(def ^:private network-policies- "The `llm-allowed-networks` policies, loosest last."- [:external-only :allow-private :allow-all])-(def ^:private network-policy-rank- (zipmap network-policies (range)))--(defonce ^:private warned-network-policy-values- (atom #{}))+ (zipmap u.http/configurable-network-policies (range)))(defsetting llm-allowed-networks(deferred-tru (str "Controls which networks Metabase may connect to for LLM provider base URLs. "@@ -98,17 +92,14 @@"its outbound connections. Proxy-only DNS is supported. Metabase enforces destination addresses ""at connection time for direct requests."):getter (fn []- (let [value (some-> (setting/env-var-value :llm-allowed-networks) keyword)]- (cond- (nil? value) :external-only- (contains? network-policy-rank value) value- ;; fail closed on a typo, and say so once rather than on every request- :else- (do (when-not (contains? @warned-network-policy-values value)- (swap! warned-network-policy-values conj value)- (log/warnf "Ignoring MB_LLM_ALLOWED_NETWORKS=%s: expected one of %s; using external-only"- (name value) (str/join ", " (map name network-policies))))- :external-only)))))+ (let [[env-var-name raw-value] (setting/env-var-source :llm-allowed-networks)]+ (or (u.http/env-network-policy env-var-name raw-value)+ :external-only))))++;; Reading it throws when the environment names a policy that does not exist: a typo stops the boot rather than+;; surfacing at the first LLM request.+(defmethod startup/def-startup-validation! ::llm-allowed-networks [_]+ (llm-allowed-networks))(defn network-policy"The network policy for an LLM request.diff --git a/src/metabase/settings/core.clj b/src/metabase/settings/core.cljindex 1a2233c0936..4ce7fc5eec7 100644--- a/src/metabase/settings/core.clj+++ b/src/metabase/settings/core.clj@@ -96,6 +96,7 @@defsettingdisabled-for-db-reasonsenv-var-name+ env-var-sourceenv-var-valueexport?getdiff --git a/src/metabase/settings/models/setting.clj b/src/metabase/settings/models/setting.cljindex 123e2324c3e..044587191f5 100644--- a/src/metabase/settings/models/setting.clj+++ b/src/metabase/settings/models/setting.clj@@ -474,26 +474,39 @@(or (@env-var-translation-cache sname)((swap! env-var-translation-cache assoc sname (keyword (str "mb-" (munge-setting-name sname)))) sname))))-(defn env-var-value- "Get the value of `setting-definition-or-name` from the corresponding env var, if any.- The name of the Setting is converted to uppercase and dashes to underscores; for example, a setting named+(defn env-var-source+ "Which env var supplies `setting-definition-or-name`'s value and what it holds, as `[env-var-name value]`, or nil+ when no env var supplies one.++ The name of the Setting is converted to uppercase and dashes to underscores; for example, a setting named`default-domain` can be set with the env var `MB_DEFAULT_DOMAIN`. Note that this strips out characters that are notlegal for shells. Setting `foo-bar?` will expect to find the key `:mb-foo-bar` which will be sourced from theenvironment variable `MB_FOO_BAR`.When the primary env var is truly absent (nil from environ) and the setting has a `:deprecated-name`, the env var- derived from that name is checked as a fallback. An empty string for the primary env var means \"explicitly unset\"- and blocks the fallback."- ^String [setting-definition-or-name]+ derived from that name is checked as a fallback, and is the one named -- so a message about the value points at+ the variable the operator actually set. An empty string for the primary env var means \"explicitly unset\" and+ blocks the fallback.++ Prefer [[env-var-value]] unless the name is needed too."+ [setting-definition-or-name](let [setting (resolve-setting setting-definition-or-name)](when (and (allows-site-wide-values? setting)(allows-setting-via-env? setting))(if-let [v (env/env (setting-env-map-name setting))]- ;; primary env var is set — return it only if non-empty- (not-empty v)+ ;; primary env var is set — use it only if non-empty+ (when-let [v (not-empty v)]+ [(env-var-name setting) v]);; primary env var is absent — try deprecated name(when-let [deprecated-name (:deprecated-name setting)]- (not-empty (env/env (setting-env-map-name deprecated-name))))))))+ (when-let [v (not-empty (env/env (setting-env-map-name deprecated-name)))]+ [(env-var-name deprecated-name) v]))))))++(defn env-var-value+ "Get the value of `setting-definition-or-name` from the corresponding env var, if any.+ See [[env-var-source]], which this reads the value half of."+ ^String [setting-definition-or-name]+ (second (env-var-source setting-definition-or-name)))(defn log-deprecated-env-var-usage!"Log warnings for any settings currently using a deprecated env var name.diff --git a/src/metabase/sso/settings.clj b/src/metabase/sso/settings.cljindex e033485bb6c..195963fa405 100644--- a/src/metabase/sso/settings.clj+++ b/src/metabase/sso/settings.clj@@ -4,7 +4,9 @@[metabase.config.core :as config][metabase.premium-features.core :as premium-features][metabase.settings.core :as setting :refer [defsetting define-multi-setting define-multi-setting-impl]]+ [metabase.startup.core :as startup][metabase.util :as u]+ [metabase.util.http :as u.http][metabase.util.i18n :refer [deferred-tru tru]][metabase.util.json :as json][metabase.util.string :as u.str])@@ -268,12 +270,21 @@(defsetting oidc-allowed-networks(deferred-tru "What networks are OIDC requests allowed to? Possible values: ''allow-all'' (default), ''allow-private'', or ''external-only''."):type :keyword+ :visibility :internal:default :allow-all:export? false- :setter (fn [new-value]- (when (some? new-value)- (assert (#{:allow-all :allow-private :external-only} (keyword new-value))))- (setting/set-value-of-type! :keyword :oidc-allowed-networks new-value)))+ :setter :none+ :doc (str "Set this to tighten which networks OIDC discovery and token requests may reach; it defaults to "+ "allow-all. Other values: external-only and allow-private")+ :getter (fn []+ (let [[env-var-name raw-value] (setting/env-var-source :oidc-allowed-networks)]+ (or (u.http/env-network-policy env-var-name raw-value)+ :allow-all))))++;; Reading it throws when the environment names a policy that does not exist: a typo stops the boot rather than+;; surfacing at the first login.+(defmethod startup/def-startup-validation! ::oidc-allowed-networks [_]+ (oidc-allowed-networks))(defn- ee-sso-configured? [](when config/ee-available?diff --git a/src/metabase/tiles/settings.clj b/src/metabase/tiles/settings.cljindex b8339cb5671..066b06a9987 100644--- a/src/metabase/tiles/settings.clj+++ b/src/metabase/tiles/settings.clj@@ -4,6 +4,7 @@[clojure.string :as str][metabase.premium-features.core :as premium-features][metabase.settings.core :as setting :refer [defsetting]]+ [metabase.startup.core :as startup][metabase.util.http :as u.http][metabase.util.i18n :as i18n :refer [tru]])(:import@@ -36,16 +37,19 @@:type :keyword:visibility :internal:export? false+ :setter :none+ :doc (str "Allowed network for the map tile server."):getter (fn []- (or (setting/get-value-of-type :keyword :map-tile-server-allowed-networks)- (if (premium-features/is-hosted?)- :external-only- :allow-private)))- :setter (fn [new-value]- (when (some? new-value)- (assert (#{:external-only :allow-private :allow-all} (keyword new-value))- (tru "Invalid map-tile-server-allowed-networks! Only values of external-only, allow-private, and allow-all are allowed.")))- (setting/set-value-of-type! :keyword :map-tile-server-allowed-networks new-value)))+ (let [[env-var-name raw-value] (setting/env-var-source :map-tile-server-allowed-networks)]+ (or (u.http/env-network-policy env-var-name raw-value)+ (if (premium-features/is-hosted?)+ :external-only+ :allow-private)))))++;; Reading it throws when the environment names a policy that does not exist: a typo stops the boot rather than+;; surfacing at the first map.+(defmethod startup/def-startup-validation! ::map-tile-server-allowed-networks [_]+ (map-tile-server-allowed-networks))(defn- valid-map-tile-server-url?"Whether `template` is safe to store. It must be http(s) and its host must be alloweddiff --git a/src/metabase/util/http.clj b/src/metabase/util/http.cljindex cf40a87f2fd..3ddd0b4a0e5 100644--- a/src/metabase/util/http.clj+++ b/src/metabase/util/http.clj@@ -197,6 +197,25 @@:allow-all true(throw (ex-info (str "Unknown network policy: " (pr-str policy)) {:policy policy}))))+(def configurable-network-policies+ "The policies a deployment may ask for through an `*-allowed-networks` environment variable, strictest first."+ [:external-only :allow-private :allow-all])++(defn env-network-policy+ "The network policy named by `raw-value`, the contents of an `*-allowed-networks` environment variable, or nil when+ that variable is unset. Throws if raw-value is not a valid value."+ [env-var-name raw-value]+ (when-let [value (some-> raw-value str str/trim not-empty lower-case-en keyword)]+ (if (some #{value} configurable-network-policies)+ value+ (throw (ex-info (format "Invalid %s: %s. Expected one of %s."+ env-var-name+ (pr-str raw-value)+ (str/join ", " (map name configurable-network-policies)))+ {:env-var env-var-name+ :value raw-value+ :expected (vec configurable-network-policies)})))))+;; one or more scheme segments, so nested schemes (`jdbc:postgresql://...`) are stripped too(def ^:private scheme-prefix-regex #"(?i)^(?:[a-z][a-z0-9+.-]*:)+//")diff --git a/test/metabase/channel/api/channel_test.clj b/test/metabase/channel/api/channel_test.cljindex 1d9594388e4..7edd4820ab1 100644--- a/test/metabase/channel/api/channel_test.clj+++ b/test/metabase/channel/api/channel_test.clj@@ -156,7 +156,7 @@:return-value {:errors {:email "Invalid email"}}}))))))(deftest test-channel-http-test- (mt/with-temporary-setting-values [http-channel-allowed-networks :allow-all]+ (mt/with-temp-env-var-value! [mb-http-channel-allowed-networks "allow-all"](channel.http-test/with-server [url [channel.http-test/post-200 channel.http-test/post-400]](testing "status-code=200 endpoint"(is (= {:ok true}@@ -205,7 +205,7 @@(:message(channel-test "http://169.254.1.100/api/health" 400))))))(testing "allow-private strategy"- (mt/with-temporary-setting-values [http-channel-allowed-networks :allow-private]+ (mt/with-temp-env-var-value! [mb-http-channel-allowed-networks "allow-private"](testing "still blocks localhost addresses"(is (= "URLs referring to hosts that supply internal hosting metadata are prohibited."(:message@@ -214,7 +214,7 @@(is (= "URLs referring to hosts that supply internal hosting metadata are prohibited."(:message(channel-test "http://169.254.1.100/api/health" 400)))))))- (mt/with-temporary-setting-values [http-channel-allowed-networks :allow-all]+ (mt/with-temp-env-var-value! [mb-http-channel-allowed-networks "allow-all"](channel.http-test/with-server [url [channel.http-test/post-200 channel.http-test/post-400]](testing "allow-all strategy allows localhost"(channel-test (str url (:path channel.http-test/post-200)) 200))))))diff --git a/test/metabase/channel/impl/http_test.clj b/test/metabase/channel/impl/http_test.cljindex 844e0cab52c..a68e0b57590 100644--- a/test/metabase/channel/impl/http_test.clj+++ b/test/metabase/channel/impl/http_test.clj@@ -155,7 +155,7 @@(ex-data e#))))(deftest can-connect-no-auth-test- (mt/with-temporary-setting-values [http-channel-allowed-networks :allow-all]+ (mt/with-temp-env-var-value! [mb-http-channel-allowed-networks "allow-all"](with-server [url [get-favicon get-200 get-302-redirect-200 get-400 get-302-redirect-400 get-500]](let [can-connect?* (fn [route](can-connect? {:url (str url (:path route))@@ -178,7 +178,7 @@(exception-data (can-connect?* get-500))))))))(deftest can-connect-header-auth-test- (mt/with-temporary-setting-values [http-channel-allowed-networks :allow-all]+ (mt/with-temp-env-var-value! [mb-http-channel-allowed-networks "allow-all"](with-server [url [(make-route :get "/user"(fn [x](if (= "SECRET" (get-in x [:headers "x-api-key"]))@@ -200,7 +200,7 @@:auth-info {"x-api-key" "WRONG"}}))))))))(deftest can-connect-query-param-auth-test- (mt/with-temporary-setting-values [http-channel-allowed-networks :allow-all]+ (mt/with-temp-env-var-value! [mb-http-channel-allowed-networks "allow-all"](with-server [url [(make-route :get "/user"(fn [x](if (= ["qnkhuat" "secretpassword"]@@ -225,7 +225,7 @@"password" "wrongpassword"}}))))))))(deftest can-connect-request-body-auth-test- (mt/with-temporary-setting-values [http-channel-allowed-networks :allow-all]+ (mt/with-temp-env-var-value! [mb-http-channel-allowed-networks "allow-all"](with-server [url [(make-route :post "/user"(fn [x](if (= "SECRET_TOKEN" (get-in x [:body :token]))@@ -258,7 +258,7 @@(testing "include undefined key"(is (=? {:errors {:xyz ["disallowed key"]}}(exception-data (can-connect? {:xyz "hello world"})))))- (mt/with-temporary-setting-values [http-channel-allowed-networks :allow-all]+ (mt/with-temp-env-var-value! [mb-http-channel-allowed-networks "allow-all"](with-server [url [get-400]](is (= {:request-body "Bad request":request-status 400}@@ -277,7 +277,7 @@:auth-method "none"})))))))))(deftest send!-test- (mt/with-temporary-setting-values [http-channel-allowed-networks :allow-all]+ (mt/with-temp-env-var-value! [mb-http-channel-allowed-networks "allow-all"](testing "basic send"(with-captured-http-requests [requests](channel/send! {:type :channel/http@@ -338,7 +338,7 @@#"No URL is configured for this webhook"(channel/send! channel nil)))))(testing "an unparseable webhook URL throws a human-readable error (#76802)"- (mt/with-temporary-setting-values [http-channel-allowed-networks :external-only]+ (mt/with-temp-env-var-value! [mb-http-channel-allowed-networks "external-only"](is (thrown-with-msg? clojure.lang.ExceptionInfo#"Invalid webhook URL"(channel/send! {:type :channel/http@@ -348,7 +348,7 @@(deftest send!-rejects-any-local-ula-cgnat-test(testing "under :external-only, hosts the old valid-host? let through -- any-local (0.0.0.0 / [::]),IPv6 ULA, IPv4 CGNAT -- are rejected up front"- (mt/with-temporary-setting-values [http-channel-allowed-networks :external-only]+ (mt/with-temp-env-var-value! [mb-http-channel-allowed-networks "external-only"](doseq [host ["0.0.0.0" "[::]" "[fc00::1]" "100.64.0.1"]](testing host(is (thrown-with-msg?@@ -364,7 +364,7 @@[:allow-private true][:external-only true]]](with-captured-http-requests [requests]- (mt/with-temporary-setting-values [http-channel-allowed-networks strategy]+ (mt/with-temp-env-var-value! [mb-http-channel-allowed-networks (name strategy)](channel/send! {:type :channel/http:details {:url "https://8.8.8.8":auth-method "none"@@ -381,7 +381,7 @@"the rendered request cannot override the policy DNS resolver"))))))(deftest alert-http-channel-e2e-test- (mt/with-temporary-setting-values [http-channel-allowed-networks :allow-all]+ (mt/with-temp-env-var-value! [mb-http-channel-allowed-networks "allow-all"](let [received-message (atom nil)receive-route (make-route :post "/test_http_channel"(fn [res]diff --git a/test/metabase/channel/render/maps_test.clj b/test/metabase/channel/render/maps_test.cljindex 9819e850bcf..aeec999a15e 100644--- a/test/metabase/channel/render/maps_test.clj+++ b/test/metabase/channel/render/maps_test.clj@@ -79,7 +79,7 @@(testing "under external-only (what Metabase Cloud gets) an internal tile URL is never requested —map-tile-server-url is admin-settable, so a raw GET here would be a blind SSRF sink reachablefrom any subscription render"- (mt/with-temporary-setting-values [map-tile-server-allowed-networks :external-only]+ (mt/with-temp-env-var-value! [mb-map-tile-server-allowed-networks "external-only"](let [requested (atom [])](fake/with-fake-routes {#".*" (fn [req](swap! requested conj (:url req))@@ -99,7 +99,7 @@(deftest fetch-tile-honors-allow-private-test(testing "under allow-private (the self-hosted default) a tile server on the deployment's own network isfetched, so a URL an admin is allowed to save is one subscription renders can actually reach"- (mt/with-temporary-setting-values [map-tile-server-allowed-networks :allow-private]+ (mt/with-temp-env-var-value! [mb-map-tile-server-allowed-networks "allow-private"](fake/with-fake-routes (render.tu/fake-tile-routes #"http://tiles\.internal/.*")(is (some? (#'maps/fetch-tile "http://tiles.internal/{z}/{x}/{y}.png" 3 4 2))))))(testing "loopback and cloud metadata are still refused — by the policy's DNS resolver at connect time,diff --git a/test/metabase/channel/settings_test.clj b/test/metabase/channel/settings_test.cljindex 3bf3265b43b..65b599833bb 100644--- a/test/metabase/channel/settings_test.clj+++ b/test/metabase/channel/settings_test.clj@@ -3,6 +3,8 @@(:require[clojure.test :refer :all][metabase.channel.settings :as channel.settings]+ [metabase.settings.core :as setting]+ [metabase.startup.core :as startup][metabase.test :as mt])(:import (clojure.lang ExceptionInfo)))@@ -100,3 +102,43 @@(channel.settings/find-cached-slack-channel-or-username "U001"))))(testing "returns nil when not found"(is (nil? (channel.settings/find-cached-slack-channel-or-username "no-such-channel")))))))++(deftest http-channel-allowed-networks-is-environment-only-test+ (testing "the policy is read from the environment only: a value that reached the setting table -- an older+ version's admin API, a serialization import, a direct write -- is ignored, not trusted"+ (mt/with-temp-env-var-value! [mb-http-channel-allowed-networks nil]+ (mt/with-temporary-raw-setting-values [http-channel-allowed-networks "allow-all"]+ (is (= :external-only (channel.settings/http-channel-allowed-networks))))))+ (testing "and the environment still wins over a stored value"+ (mt/with-temp-env-var-value! [mb-http-channel-allowed-networks "allow-private"]+ (mt/with-temporary-raw-setting-values [http-channel-allowed-networks "allow-all"]+ (is (= :allow-private (channel.settings/http-channel-allowed-networks))))))+ (testing "the deprecated env var name still configures it"+ (mt/with-temp-env-var-value! [mb-http-channel-host-strategy "allow-all"]+ (is (= :allow-all (channel.settings/http-channel-allowed-networks)))))+ (testing "an unrecognized policy is refused outright"+ (mt/with-temp-env-var-value! [mb-http-channel-allowed-networks "allow-everything"]+ (is (thrown-with-msg? ExceptionInfo+ #"Invalid MB_HTTP_CHANNEL_ALLOWED_NETWORKS: \"allow-everything\"\. Expected one of external-only, allow-private, allow-all\."+ (channel.settings/http-channel-allowed-networks)))))+ (testing "a bad value in the deprecated variable names that variable, not the one the operator did not set"+ (mt/with-temp-env-var-value! [mb-http-channel-host-strategy "allow-everything"]+ (is (thrown-with-msg? ExceptionInfo+ #"Invalid MB_HTTP_CHANNEL_HOST_STRATEGY"+ (channel.settings/http-channel-allowed-networks)))))+ (testing "nothing can write it: it is a read-only Setting"+ (is (thrown-with-msg? UnsupportedOperationException+ #"read-only setting"+ (setting/set! :http-channel-allowed-networks :allow-all)))))++(deftest http-channel-allowed-networks-startup-validation-test+ (testing "a policy the environment names but Metabase does not recognize stops the boot, rather than waiting+ for the first webhook to discover it"+ (mt/with-temp-env-var-value! [mb-http-channel-allowed-networks "allow-everything"]+ (is (thrown-with-msg? ExceptionInfo+ #"Invalid MB_HTTP_CHANNEL_ALLOWED_NETWORKS"+ (startup/def-startup-validation! ::channel.settings/http-channel-allowed-networks)))))+ (testing "a policy it does recognize lets the boot continue"+ (mt/with-temp-env-var-value! [mb-http-channel-allowed-networks "allow-private"]+ (is (= :allow-private+ (startup/def-startup-validation! ::channel.settings/http-channel-allowed-networks))))))diff --git a/test/metabase/driver/util_test.clj b/test/metabase/driver/util_test.cljindex 40428906be0..092c8eecda2 100644--- a/test/metabase/driver/util_test.clj+++ b/test/metabase/driver/util_test.clj@@ -14,6 +14,8 @@[metabase.lib.test-util :as lib.tu];; binds mock metadata providers via the ambient store, which the code under test reads^{:clj-kondo/ignore [:deprecated-namespace]} [metabase.query-processor.store :as qp.store]+ [metabase.settings.core :as setting]+ [metabase.startup.core :as startup][metabase.test :as mt][metabase.test.fixtures :as fixtures][metabase.util :as u]@@ -925,8 +927,39 @@(is (= :external-only (driver.settings/warehouse-allowed-networks)))(is (=? {:status-code 400}(ssrf-error #(driver.u/validate-connection-hosts! :postgres {:host "127.0.0.1"}))))))- (testing "an unrecognized policy fails closed at the point of use rather than quietly allowing everything"+ (testing "an unrecognized policy is refused outright rather than silently leaving the instance on some other+ policy -- startup reads this Setting, so the instance does not come up at all"(mt/with-temp-env-var-value! [mb-warehouse-allowed-networks "unknown-policy"](is (thrown-with-msg? clojure.lang.ExceptionInfo- #"Unknown network policy"- (driver.u/validate-connection-hosts! :postgres {:host "127.0.0.1"}))))))+ #"Invalid MB_WAREHOUSE_ALLOWED_NETWORKS"+ (driver.settings/warehouse-allowed-networks))))))++(deftest warehouse-allowed-networks-is-environment-only-test+ (testing "the policy is read from the environment only: a value that reached the setting table -- an older+ version's admin API, a serialization import, a direct write -- is ignored, not trusted"+ (mt/with-temp-env-var-value! [mb-warehouse-allowed-networks nil]+ (mt/with-premium-features #{:hosting}+ (mt/with-temporary-raw-setting-values [warehouse-allowed-networks "allow-all"]+ (is (= :external-only (driver.settings/warehouse-allowed-networks)))+ (is (=? {:status-code 400}+ (ssrf-error #(driver.u/validate-connection-hosts! :postgres {:host "127.0.0.1"}))))))))+ (testing "and the environment still wins over a stored value"+ (mt/with-temp-env-var-value! [mb-warehouse-allowed-networks "allow-private"]+ (mt/with-temporary-raw-setting-values [warehouse-allowed-networks "external-only"]+ (is (= :allow-private (driver.settings/warehouse-allowed-networks))))))+ (testing "nothing can write it: it is a read-only Setting"+ (is (thrown-with-msg? UnsupportedOperationException+ #"read-only setting"+ (setting/set! :warehouse-allowed-networks :allow-all)))))++(deftest warehouse-allowed-networks-startup-validation-test+ (testing "a policy the environment names but Metabase does not recognize stops the boot, rather than waiting+ for the first query to discover it"+ (mt/with-temp-env-var-value! [mb-warehouse-allowed-networks "allow-everything"]+ (is (thrown-with-msg? clojure.lang.ExceptionInfo+ #"Invalid MB_WAREHOUSE_ALLOWED_NETWORKS"+ (startup/def-startup-validation! ::driver.settings/warehouse-allowed-networks)))))+ (testing "a policy it does recognize lets the boot continue"+ (mt/with-temp-env-var-value! [mb-warehouse-allowed-networks "allow-private"]+ (is (= :allow-private+ (startup/def-startup-validation! ::driver.settings/warehouse-allowed-networks))))))diff --git a/test/metabase/llm/settings_test.clj b/test/metabase/llm/settings_test.cljindex 1f05af6039e..e7da5d609ba 100644--- a/test/metabase/llm/settings_test.clj+++ b/test/metabase/llm/settings_test.clj@@ -4,6 +4,7 @@[metabase.config.core :as config][metabase.llm.settings :as llm.settings][metabase.settings.core :as setting]+ [metabase.startup.core :as startup][metabase.test :as mt][metabase.util.http :as u.http])(:import@@ -215,9 +216,12 @@(is (= :allow-all (llm.settings/llm-allowed-networks))))(mt/with-temp-env-var-value! [mb-llm-allowed-networks "allow-private"](is (= :allow-private (llm.settings/llm-allowed-networks)))))- (testing "a value that is not one of the policies fails closed"+ (testing "a value that is not one of the policies is refused outright: startup reads this Setting, so the+ instance does not come up on a policy nobody chose"(mt/with-temp-env-var-value! [mb-llm-allowed-networks "allow_all"]- (is (= :external-only (llm.settings/llm-allowed-networks)))))+ (is (thrown-with-msg? clojure.lang.ExceptionInfo+ #"Invalid MB_LLM_ALLOWED_NETWORKS"+ (llm.settings/llm-allowed-networks)))))(testing "it is not settable: nobody loosens it through the API"(is (thrown? Exception (setting/set! :llm-allowed-networks :allow-all)))))(testing "a value that reached the application database some other way is ignored"@@ -442,3 +446,15 @@(testing "can be overridden"(mt/with-temporary-setting-values [llm-rate-limit-per-ip 200](is (= 200 (llm.settings/llm-rate-limit-per-ip))))))++(deftest llm-allowed-networks-startup-validation-test+ (testing "a policy the environment names but Metabase does not recognize stops the boot, rather than waiting+ for the first LLM request to discover it"+ (mt/with-temp-env-var-value! [mb-llm-allowed-networks "allow-everything"]+ (is (thrown-with-msg? clojure.lang.ExceptionInfo+ #"Invalid MB_LLM_ALLOWED_NETWORKS"+ (startup/def-startup-validation! :metabase.llm.provider.settings/llm-allowed-networks)))))+ (testing "a policy it does recognize lets the boot continue"+ (mt/with-temp-env-var-value! [mb-llm-allowed-networks "allow-private"]+ (is (= :allow-private+ (startup/def-startup-validation! :metabase.llm.provider.settings/llm-allowed-networks))))))diff --git a/test/metabase/pulse/api/pulse_test.clj b/test/metabase/pulse/api/pulse_test.cljindex 97ac40c07bf..46114660a59 100644--- a/test/metabase/pulse/api/pulse_test.clj+++ b/test/metabase/pulse/api/pulse_test.clj@@ -1080,7 +1080,7 @@(deftest send-test-alert-with-http-channel-test(testing "POST /api/pulse/test send test alert to a http channel"(notification.tu/with-send-notification-sync- (mt/with-temporary-setting-values [http-channel-allowed-networks :allow-all]+ (mt/with-temp-env-var-value! [mb-http-channel-allowed-networks "allow-all"](let [requests (atom [])endpoint (channel.http-test/make-route:post "/test"diff --git a/test/metabase/pulse/send_test.clj b/test/metabase/pulse/send_test.cljindex cbdf3e008c5..2de37832d4a 100644--- a/test/metabase/pulse/send_test.clj+++ b/test/metabase/pulse/send_test.clj@@ -655,7 +655,7 @@(swap! requests conj req){:status 200:body "ok"}))]- (mt/with-temporary-setting-values [http-channel-allowed-networks :allow-all]+ (mt/with-temp-env-var-value! [mb-http-channel-allowed-networks "allow-all"](notification.tu/with-notification-testing-setup!(channel.http-test/with-server [url [endpoint]](mt/with-tempdiff --git a/test/metabase/settings/models/setting_test.clj b/test/metabase/settings/models/setting_test.cljindex f8e40d1c1cb..4653754c0ad 100644--- a/test/metabase/settings/models/setting_test.clj+++ b/test/metabase/settings/models/setting_test.clj@@ -1943,6 +1943,27 @@(is (nil? (setting/env-var-value :test-setting-with-deprecated-name)))(is (= [] (deprecated-env-var-warnings))))+(deftest env-var-source-names-the-variable-actually-set-test+ (testing "the value comes back with the name of the variable that supplied it, so a message about the value can+ point at the variable the operator set rather than at one they did not"+ (mt/with-temp-env-var-value! [mb-test-setting-with-deprecated-name "PRIMARY"]+ (is (= ["MB_TEST_SETTING_WITH_DEPRECATED_NAME" "PRIMARY"]+ (setting/env-var-source :test-setting-with-deprecated-name))))+ (mt/with-temp-env-var-value! [mb-old-test-setting-name "LEGACY"]+ (is (= ["MB_OLD_TEST_SETTING_NAME" "LEGACY"]+ (setting/env-var-source :test-setting-with-deprecated-name))))+ (testing "the primary wins when both are set"+ (mt/with-temp-env-var-value! [mb-test-setting-with-deprecated-name "PRIMARY"+ mb-old-test-setting-name "LEGACY"]+ (is (= ["MB_TEST_SETTING_WITH_DEPRECATED_NAME" "PRIMARY"]+ (setting/env-var-source :test-setting-with-deprecated-name)))))+ (testing "an empty primary means explicitly unset, and blocks the deprecated fallback"+ (mt/with-temp-env-var-value! [mb-test-setting-with-deprecated-name ""+ mb-old-test-setting-name "LEGACY"]+ (is (nil? (setting/env-var-source :test-setting-with-deprecated-name)))))+ (testing "nil when neither is set"+ (is (nil? (setting/env-var-source :test-setting-with-deprecated-name))))))+(deftest deprecated-name-empty-primary-blocks-fallback-test(mt/with-temp-env-var-value! [mb-test-setting-with-deprecated-name ""mb-old-test-setting-name "LEGACY"]diff --git a/test/metabase/sso/oidc/discovery_test.clj b/test/metabase/sso/oidc/discovery_test.cljindex 8a90207b755..d089b8f4b41 100644--- a/test/metabase/sso/oidc/discovery_test.clj+++ b/test/metabase/sso/oidc/discovery_test.clj@@ -193,7 +193,7 @@(deftest discover-oidc-configuration-ssrf-protection-test(testing "Respects oidc-allowed-networks if set — blocked requests return nil (no HTTP request made)"(oidc.discovery/clear-cache!)- (mt/with-temporary-setting-values [oidc-allowed-networks :external-only]+ (mt/with-temp-env-var-value! [mb-oidc-allowed-networks "external-only"](testing "Rejects internal addresses (localhost)"(oidc.discovery/clear-cache!)(is (nil? (oidc.discovery/discover-oidc-configuration "http://localhost/oidc"))))@@ -214,20 +214,20 @@(deftest get-token-endpoint-blocks-internal-hosts-test(testing "get-token-endpoint rejects internal hosts when oidc-allowed-networks is :external-only"- (mt/with-temporary-setting-values [oidc-allowed-networks :external-only]+ (mt/with-temp-env-var-value! [mb-oidc-allowed-networks "external-only"](let [config {:discovery-document {:token_endpoint "http://169.254.169.254/latest/meta-data/"}}](is (thrown-with-msg? clojure.lang.ExceptionInfo#"address not allowed by network restrictions"(oidc.discovery/get-token-endpoint config))))))(testing "get-token-endpoint allows all hosts when oidc-allowed-networks is :allow-all"- (mt/with-temporary-setting-values [oidc-allowed-networks :allow-all]+ (mt/with-temp-env-var-value! [mb-oidc-allowed-networks "allow-all"](let [config {:discovery-document {:token_endpoint "https://provider.example.com/token"}}](is (= "https://provider.example.com/token"(oidc.discovery/get-token-endpoint config)))))))(deftest get-authorization-endpoint-blocks-internal-hosts-test(testing "get-authorization-endpoint rejects internal hosts"- (mt/with-temporary-setting-values [oidc-allowed-networks :external-only]+ (mt/with-temp-env-var-value! [mb-oidc-allowed-networks "external-only"](let [config {:discovery-document {:authorization_endpoint "http://192.168.1.1/authorize"}}](is (thrown-with-msg? clojure.lang.ExceptionInfo#"address not allowed by network restrictions"@@ -235,7 +235,7 @@(deftest get-jwks-uri-blocks-internal-hosts-test(testing "get-jwks-uri rejects internal hosts"- (mt/with-temporary-setting-values [oidc-allowed-networks :external-only]+ (mt/with-temp-env-var-value! [mb-oidc-allowed-networks "external-only"](let [config {:discovery-document {:jwks_uri "http://10.0.0.1/jwks"}}](is (thrown-with-msg? clojure.lang.ExceptionInfo#"address not allowed by network restrictions"diff --git a/test/metabase/sso/oidc/http_test.clj b/test/metabase/sso/oidc/http_test.cljindex e4a196bd196..5646c69d489 100644--- a/test/metabase/sso/oidc/http_test.clj+++ b/test/metabase/sso/oidc/http_test.clj@@ -7,7 +7,7 @@(deftest oidc-get-ssrf-protection-test(testing "oidc-get blocks internal hosts when oidc-allowed-networks is :external-only"- (mt/with-temporary-setting-values [oidc-allowed-networks :external-only]+ (mt/with-temp-env-var-value! [mb-oidc-allowed-networks "external-only"](testing "Rejects localhost"(is (thrown-with-msg? clojure.lang.ExceptionInfo#"address not allowed by network restrictions"@@ -21,14 +21,14 @@#"address not allowed by network restrictions"(oidc.http/oidc-get "http://192.168.1.1/path"))))))(testing "oidc-get allows requests when oidc-allowed-networks is :allow-all"- (mt/with-temporary-setting-values [oidc-allowed-networks :allow-all]+ (mt/with-temp-env-var-value! [mb-oidc-allowed-networks "allow-all"](mt/with-dynamic-fn-redefs [http/get (fn [_url _opts] {:status 200 :body {:ok true}})](let [response (oidc.http/oidc-get "https://provider.example.com/discovery")](is (= 200 (:status response))))))))(deftest oidc-post-ssrf-protection-test(testing "oidc-post blocks internal hosts when oidc-allowed-networks is :external-only"- (mt/with-temporary-setting-values [oidc-allowed-networks :external-only]+ (mt/with-temp-env-var-value! [mb-oidc-allowed-networks "external-only"](testing "Rejects cloud metadata endpoint"(is (thrown-with-msg? clojure.lang.ExceptionInfo#"address not allowed by network restrictions"@@ -40,7 +40,7 @@(oidc.http/oidc-post "http://127.0.0.1/token"{:form-params {:grant_type "client_credentials"}}))))))(testing "oidc-post allows requests when oidc-allowed-networks is :allow-all"- (mt/with-temporary-setting-values [oidc-allowed-networks :allow-all]+ (mt/with-temp-env-var-value! [mb-oidc-allowed-networks "allow-all"](mt/with-dynamic-fn-redefs [http/post (fn [_url _opts] {:status 200 :body {:access_token "tok"}})](let [response (oidc.http/oidc-post "https://provider.example.com/token"{:form-params {:grant_type "client_credentials"}})]@@ -48,13 +48,13 @@(deftest oidc-get-allow-all-test(testing "oidc-get allows all hosts when oidc-allowed-networks is :allow-all"- (mt/with-temporary-setting-values [oidc-allowed-networks :allow-all]+ (mt/with-temp-env-var-value! [mb-oidc-allowed-networks "allow-all"](mt/with-dynamic-fn-redefs [http/get (fn [_url _opts] {:status 200 :body {}})](is (= 200 (:status (oidc.http/oidc-get "http://192.168.1.1/path"))))))))(deftest oidc-get-merges-custom-opts-test(testing "Custom options are merged with defaults"- (mt/with-temporary-setting-values [oidc-allowed-networks :allow-all]+ (mt/with-temp-env-var-value! [mb-oidc-allowed-networks "allow-all"](mt/with-dynamic-fn-redefs [http/get (fn [_url opts];; Verify custom opts are present alongside defaults(is (= :json (:as opts)))diff --git a/test/metabase/sso/oidc/tokens_test.clj b/test/metabase/sso/oidc/tokens_test.cljindex bf1fa884ab9..1684db7c25b 100644--- a/test/metabase/sso/oidc/tokens_test.clj+++ b/test/metabase/sso/oidc/tokens_test.clj@@ -340,7 +340,7 @@ h0ccjghRm1/Az8L/HL+gdQmtY0NdB4Ml2mZHCVsPYf5WzIirTpjY0EzKDA==(deftest get-jwks-ssrf-protection-test(testing "Respects oidc-allowed-networks if set — blocked requests return nil (no HTTP request made)"- (mt/with-temporary-setting-values [oidc-allowed-networks :external-only]+ (mt/with-temp-env-var-value! [mb-oidc-allowed-networks "external-only"](testing "Rejects internal addresses (localhost)"(oidc.tokens/clear-jwks-cache!)(is (nil? (oidc.tokens/get-jwks "http://localhost/jwks"))))diff --git a/test/metabase/sso/settings_test.clj b/test/metabase/sso/settings_test.cljindex d4e41d7e933..c60f7f81cdb 100644--- a/test/metabase/sso/settings_test.clj+++ b/test/metabase/sso/settings_test.clj@@ -6,6 +6,7 @@[metabase.sso.ldap-test-util :as ldap.test][metabase.sso.ldap.settings :as sso.ldap.settings][metabase.sso.settings :as sso.settings]+ [metabase.startup.core :as startup][metabase.test :as mt]))(deftest ldap-enabled-test@@ -45,3 +46,35 @@(doseq [k [:ldap-attribute-email :ldap-attribute-firstname :ldap-attribute-lastname]](testing k(is (nil? (setting/user-facing-value k))))))))++(deftest oidc-allowed-networks-is-environment-only-test+ (testing "the policy is read from the environment only: a value that reached the setting table -- an older+ version's admin API, a serialization import, a direct write -- is ignored, not trusted"+ (mt/with-temp-env-var-value! [mb-oidc-allowed-networks nil]+ (mt/with-temporary-raw-setting-values [oidc-allowed-networks "external-only"]+ (is (= :allow-all (sso.settings/oidc-allowed-networks))))))+ (testing "and the environment still wins over a stored value"+ (mt/with-temp-env-var-value! [mb-oidc-allowed-networks "external-only"]+ (mt/with-temporary-raw-setting-values [oidc-allowed-networks "allow-all"]+ (is (= :external-only (sso.settings/oidc-allowed-networks))))))+ (testing "an unrecognized policy is refused outright"+ (mt/with-temp-env-var-value! [mb-oidc-allowed-networks "allow-everything"]+ (is (thrown-with-msg? clojure.lang.ExceptionInfo+ #"Invalid MB_OIDC_ALLOWED_NETWORKS"+ (sso.settings/oidc-allowed-networks)))))+ (testing "nothing can write it: it is a read-only Setting"+ (is (thrown-with-msg? UnsupportedOperationException+ #"read-only setting"+ (setting/set! :oidc-allowed-networks :allow-all)))))++(deftest oidc-allowed-networks-startup-validation-test+ (testing "a policy the environment names but Metabase does not recognize stops the boot, rather than waiting+ for the first login to discover it"+ (mt/with-temp-env-var-value! [mb-oidc-allowed-networks "allow-everything"]+ (is (thrown-with-msg? clojure.lang.ExceptionInfo+ #"Invalid MB_OIDC_ALLOWED_NETWORKS"+ (startup/def-startup-validation! ::sso.settings/oidc-allowed-networks)))))+ (testing "a policy it does recognize lets the boot continue"+ (mt/with-temp-env-var-value! [mb-oidc-allowed-networks "external-only"]+ (is (= :external-only+ (startup/def-startup-validation! ::sso.settings/oidc-allowed-networks))))))diff --git a/test/metabase/tiles/settings_test.clj b/test/metabase/tiles/settings_test.cljindex f1c0d078161..943aa46f5fa 100644--- a/test/metabase/tiles/settings_test.clj+++ b/test/metabase/tiles/settings_test.clj@@ -2,6 +2,8 @@(:require[clojure.test :refer :all][metabase.premium-features.test-util :as premium-features.tu]+ [metabase.settings.core :as setting]+ [metabase.startup.core :as startup][metabase.test :as mt][metabase.tiles.settings :as tiles.settings]))@@ -36,13 +38,13 @@(testing "the default policy depends on where we are running: on Cloud a tile server on an internaladdress is somebody reaching for our infrastructure, but self-hosted it is ordinary";; nil = nothing stored, so the getter falls through to the where-are-we-running default- (mt/with-temporary-setting-values [map-tile-server-allowed-networks nil]+ (mt/with-temp-env-var-value! [mb-map-tile-server-allowed-networks nil](premium-features.tu/with-premium-features #{:hosting}(is (= :external-only (tiles.settings/map-tile-server-allowed-networks))))(premium-features.tu/with-premium-features #{}(is (= :allow-private (tiles.settings/map-tile-server-allowed-networks)))))(testing "an explicit value wins either way"- (mt/with-temporary-setting-values [map-tile-server-allowed-networks :allow-all]+ (mt/with-temp-env-var-value! [mb-map-tile-server-allowed-networks "allow-all"](premium-features.tu/with-premium-features #{:hosting}(is (= :allow-all (tiles.settings/map-tile-server-allowed-networks))))))))@@ -51,19 +53,19 @@configuration: this setting is consumed by the browser, so an on-prem instance with no internetegress can only show maps at all by pointing at an internal host"(premium-features.tu/with-premium-features #{}- (mt/with-temporary-setting-values [map-tile-server-url osm-template- map-tile-server-allowed-networks nil]- (doseq [template ["http://10.0.0.1/{z}/{x}/{y}.png"- "http://192.168.0.1:8080/{z}/{x}/{y}.png"- "http://172.16.4.20/{z}/{x}/{y}.png"]]- (testing template- (is (= template (set-tile-url! template))))))))+ (mt/with-temp-env-var-value! [mb-map-tile-server-allowed-networks nil]+ (mt/with-temporary-setting-values [map-tile-server-url osm-template]+ (doseq [template ["http://10.0.0.1/{z}/{x}/{y}.png"+ "http://192.168.0.1:8080/{z}/{x}/{y}.png"+ "http://172.16.4.20/{z}/{x}/{y}.png"]]+ (testing template+ (is (= template (set-tile-url! template)))))))))(testing "on Cloud the same URLs are refused"(premium-features.tu/with-premium-features #{:hosting}- (mt/with-temporary-setting-values [map-tile-server-url osm-template- map-tile-server-allowed-networks nil]- (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Invalid map tile server URL"- (tiles.settings/map-tile-server-url! "http://10.0.0.1/{z}/{x}/{y}.png")))))))+ (mt/with-temp-env-var-value! [mb-map-tile-server-allowed-networks nil]+ (mt/with-temporary-setting-values [map-tile-server-url osm-template]+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Invalid map tile server URL"+ (tiles.settings/map-tile-server-url! "http://10.0.0.1/{z}/{x}/{y}.png"))))))))(deftest map-tile-server-url-accepts-valid-templates-test(testing "the setter still accepts the templates admins legitimately configure"@@ -81,12 +83,12 @@(deftest map-tile-server-allowed-networks-test(mt/with-temporary-setting-values [map-tile-server-url osm-template](testing "external-only tightens the default, rejecting private networks"- (mt/with-temporary-setting-values [map-tile-server-allowed-networks :external-only]+ (mt/with-temp-env-var-value! [mb-map-tile-server-allowed-networks "external-only"](is (thrown-with-msg? clojure.lang.ExceptionInfo #"Invalid map tile server URL"(tiles.settings/map-tile-server-url! "http://10.0.0.1/{z}/{x}/{y}.png")))(is (= osm-template (set-tile-url! osm-template)))))(testing "allow-all imposes no host restriction, for a tile server Metabase itself cannot resolve"- (mt/with-temporary-setting-values [map-tile-server-allowed-networks :allow-all]+ (mt/with-temp-env-var-value! [mb-map-tile-server-allowed-networks "allow-all"](doseq [template ["http://127.0.0.1:8899/{z}/{x}/{y}.png""https://tiles.invalid/{z}/{x}/{y}.png"]](testing template@@ -94,6 +96,36 @@(testing "the scheme is still checked"(is (thrown-with-msg? clojure.lang.ExceptionInfo #"Invalid map tile server URL"(tiles.settings/map-tile-server-url! "file:///etc/passwd"))))))- (testing "only the three known values are accepted"- (is (thrown-with-msg? java.lang.AssertionError #"Invalid map-tile-server-allowed-networks"- (tiles.settings/map-tile-server-allowed-networks! :allow-everything))))))+ (testing "an unrecognized policy is refused outright"+ (mt/with-temp-env-var-value! [mb-map-tile-server-allowed-networks "allow-everything"]+ (is (thrown-with-msg? clojure.lang.ExceptionInfo+ #"Invalid MB_MAP_TILE_SERVER_ALLOWED_NETWORKS"+ (tiles.settings/map-tile-server-allowed-networks)))))))++(deftest map-tile-server-allowed-networks-is-environment-only-test+ (testing "the policy is read from the environment only: a value that reached the setting table -- an older+ version's admin API, a serialization import, a direct write -- is ignored, not trusted"+ (mt/with-temp-env-var-value! [mb-map-tile-server-allowed-networks nil]+ (premium-features.tu/with-premium-features #{:hosting}+ (mt/with-temporary-raw-setting-values [map-tile-server-allowed-networks "allow-all"]+ (is (= :external-only (tiles.settings/map-tile-server-allowed-networks)))))))+ (testing "and the environment still wins over a stored value"+ (mt/with-temp-env-var-value! [mb-map-tile-server-allowed-networks "allow-all"]+ (mt/with-temporary-raw-setting-values [map-tile-server-allowed-networks "external-only"]+ (is (= :allow-all (tiles.settings/map-tile-server-allowed-networks))))))+ (testing "nothing can write it: it is a read-only Setting"+ (is (thrown-with-msg? UnsupportedOperationException+ #"read-only setting"+ (setting/set! :map-tile-server-allowed-networks :allow-all)))))++(deftest map-tile-server-allowed-networks-startup-validation-test+ (testing "a policy the environment names but Metabase does not recognize stops the boot, rather than waiting+ for the first map to discover it"+ (mt/with-temp-env-var-value! [mb-map-tile-server-allowed-networks "allow-everything"]+ (is (thrown-with-msg? clojure.lang.ExceptionInfo+ #"Invalid MB_MAP_TILE_SERVER_ALLOWED_NETWORKS"+ (startup/def-startup-validation! ::tiles.settings/map-tile-server-allowed-networks)))))+ (testing "a policy it does recognize lets the boot continue"+ (mt/with-temp-env-var-value! [mb-map-tile-server-allowed-networks "allow-all"]+ (is (= :allow-all+ (startup/def-startup-validation! ::tiles.settings/map-tile-server-allowed-networks))))))diff --git a/test/metabase/util/http_test.clj b/test/metabase/util/http_test.cljindex c82cd01e3af..40b7e56d30d 100644--- a/test/metabase/util/http_test.clj+++ b/test/metabase/util/http_test.clj@@ -386,3 +386,30 @@(is (= 5 (count (#'http/read-bounded (ByteArrayInputStream. (.getBytes "12345")) 5)))))(testing "returns nil when the stream exceeds the cap"(is (nil? (#'http/read-bounded (ByteArrayInputStream. (.getBytes "0123456789")) 5)))))++(deftest ^:parallel env-network-policy-test+ (testing "an unset environment variable names no policy, so the caller supplies its own default"+ (doseq [raw [nil "" " "]]+ (is (nil? (http/env-network-policy "MB_TEST_ALLOWED_NETWORKS" raw)) (pr-str raw))))+ (testing "a known policy is read as written"+ (doseq [[raw expected] {"external-only" :external-only+ "allow-private" :allow-private+ "allow-all" :allow-all+ "ALLOW-PRIVATE" :allow-private+ " allow-all " :allow-all}]+ (is (= expected (http/env-network-policy "MB_TEST_ALLOWED_NETWORKS" raw)) raw)))+ (testing "a value that is not a policy throws, naming the variable and what it accepts: nothing else validates+ what is in the environment, and running on a policy nobody chose is not an option"+ (doseq [raw ["allow-everything" "externl-only" "true" ":allow-all" "allow_all"+ ;; a policy of the address checker, but not one a deployment may ask for+ "loopback-and-private"]]+ (is (thrown-with-msg? ExceptionInfo+ #"Invalid MB_TEST_ALLOWED_NETWORKS: .* Expected one of external-only, allow-private, allow-all\."+ (http/env-network-policy "MB_TEST_ALLOWED_NETWORKS" raw))+ raw)))+ (testing "the thrown data names the variable, the value and what was expected"+ (is (= {:env-var "MB_TEST_ALLOWED_NETWORKS"+ :value "allow-everything"+ :expected [:external-only :allow-private :allow-all]}+ (try (http/env-network-policy "MB_TEST_ALLOWED_NETWORKS" "allow-everything")+ (catch ExceptionInfo e (ex-data e)))))))