Skip to content

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

Added map-tile-server-allowed-networks setting (#81243)

Nathan Voxland52ba78451de6
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index cc5d8cccb71..0eb85257b0e 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -2485,6 +2485,7 @@
lib
lib-be
parameters
+ premium-features
query-processor
settings
util}
diff --git a/deps.edn b/deps.edn
index 839bbc3718b..684b56105a9 100644
--- a/deps.edn
+++ b/deps.edn
@@ -421,7 +421,7 @@
"-Dmb.experimental.search.block.on.queue=true"
"-Dmb.search.engine=appdb"
;; We use a local webhook service for testing
- "-Dmb.http.channel.host.strategy=allow-all"]}
+ "-Dmb.http.channel.allowed.networks=allow-all"]}
;; run the prod server when generating docs
;; as some default values differ between dev and prod
diff --git a/docs/configuring-metabase/config-template.md b/docs/configuring-metabase/config-template.md
index f7fe921bfc1..11c34d31767 100644
--- a/docs/configuring-metabase/config-template.md
+++ b/docs/configuring-metabase/config-template.md
@@ -165,7 +165,7 @@ config:
help-link: metabase
help-link-custom-destination: https://www.metabase.com/help/premium
hide-stacktraces: false
- http-channel-host-strategy: external-only
+ http-channel-allowed-networks: external-only
humanization-strategy: simple
index-update-thread-count: 2
jdbc-data-warehouse-connection-pool-checkout-timeout-ms: 0
@@ -252,6 +252,7 @@ config:
loading-message: doing-science
login-page-illustration: default
login-page-illustration-custom: null
+ map-tile-server-allowed-networks: null
map-tile-server-url: https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png
mcp-apps-cors-custom-origins: ''
mcp-apps-cors-enabled-clients: []
diff --git a/docs/configuring-metabase/environment-variables.md b/docs/configuring-metabase/environment-variables.md
index 4eafb7e3ed4..2a3ca93748e 100644
--- a/docs/configuring-metabase/environment-variables.md
+++ b/docs/configuring-metabase/environment-variables.md
@@ -870,7 +870,7 @@ Custom URL for the help link.
Prevent the exception middleware from including stacktraces in responses.
-### `MB_HTTP_CHANNEL_HOST_STRATEGY`
+### `MB_HTTP_CHANNEL_ALLOWED_NETWORKS`
- Type: keyword
- Default: `external-only`
@@ -1731,6 +1731,18 @@ Options for displaying the illustration on the login page.
The custom illustration for the login page.
+### `MB_MAP_TILE_SERVER_ALLOWED_NETWORKS`
+
+- Type: keyword
+- Default: `null`
+
+Controls which networks Metabase may connect to for map tile servers.
+Options:
+- allow-private (external + private networks but NOT loopback or link-local)
+- external-only (only globally routable public addresses)
+- allow-all (no restrictions).
+Defaults to allow-private when self-hosted.
+
### `MB_MAP_TILE_SERVER_URL`
- Type: string
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.clj
index 5d68e820b01..a0b60889236 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-host-strategy :allow-all]
+ (mt/with-temporary-setting-values [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/impl/http.clj b/src/metabase/channel/impl/http.clj
index c2a940b587c..10148a3d744 100644
--- a/src/metabase/channel/impl/http.clj
+++ b/src/metabase/channel/impl/http.clj
@@ -55,7 +55,7 @@
(mu/defmethod channel/send! :channel/http
[{{:keys [url method auth-method auth-info]} :details} :- HTTPChannel
request]
- (let [strategy (channel.settings/http-channel-host-strategy)
+ (let [strategy (channel.settings/http-channel-allowed-networks)
resolver (u.http/network-policy-dns-resolver strategy)]
(check-url! strategy url)
(let [req (-> (merge
diff --git a/src/metabase/channel/render/maps.clj b/src/metabase/channel/render/maps.clj
index 6ae043a72cb..97fbd39fc31 100644
--- a/src/metabase/channel/render/maps.clj
+++ b/src/metabase/channel/render/maps.clj
@@ -5,11 +5,12 @@
draws pin tiles for the live app); the new part is fetching + stitching the OSM basemap under the
overlay."
(:require
- [clj-http.client :as http]
[clojure.core.memoize :as memoize]
[clojure.java.io :as io]
[clojure.string :as str]
+ [metabase.tiles.settings :as tiles.settings]
[metabase.util :as u]
+ [metabase.util.http :as u.http]
[metabase.util.log :as log]
[metabase.util.web-mercator :as mercator])
(:import
@@ -84,22 +85,25 @@
from hammering the tile server — important for OSM's usage policy."
(u/hours->ms 1))
+(def ^:private tile-max-bytes
+ "Download cap per tile. A 256×256 basemap tile is a few tens of KB; this is a backstop against a
+ hostile or misconfigured tile server streaming forever."
+ (* 4 1024 1024))
+
(def ^:private fetch-tile*
- ;; Throws on any failure (non-200, undecodable image) so that only successful fetches are cached —
- ;; otherwise a transient failure would leave a blank strip in every render until the TTL expires.
+ ;; Throws on any failure (refused, non-200, undecodable image) so that only successful fetches are cached
+ ;; — otherwise a transient failure would leave a blank strip in every render until the TTL expires.
(memoize/ttl
(fn [template ^long z ^long x ^long y]
- (let [url (expand-tile-url template z x y)
- resp (http/get url {:as :byte-array
- :socket-timeout 5000
- :connection-timeout 5000
- :throw-exceptions false
- ;; OSM rejects requests without a valid User-Agent.
- :headers {"User-Agent" "Metabase static map renderer"}})]
- (when-not (= 200 (:status resp))
- (throw (ex-info (format "Tile fetch failed with status %d" (:status resp))
- {:url url :status (:status resp)})))
- (or (ImageIO/read (ByteArrayInputStream. ^bytes (:body resp)))
+ (let [url (expand-tile-url template z x y)
+ {:keys [^bytes bytes]} (u.http/fetch-bytes url {:max-bytes tile-max-bytes
+ :timeout-ms 5000
+ :network-policy (tiles.settings/map-tile-server-allowed-networks)
+ ;; OSM rejects requests without a valid User-Agent.
+ :user-agent "Metabase static map renderer"})]
+ (when-not bytes
+ (throw (ex-info "Tile fetch failed" {:url url})))
+ (or (ImageIO/read (ByteArrayInputStream. bytes))
(throw (ex-info "Tile response is not a decodable image" {:url url})))))
:ttl/threshold tile-cache-ttl-ms))
diff --git a/src/metabase/channel/settings.clj b/src/metabase/channel/settings.clj
index ec0581aa14b..a9d4052a65f 100644
--- a/src/metabase/channel/settings.clj
+++ b/src/metabase/channel/settings.clj
@@ -315,7 +315,7 @@
:visibility :internal
:audit :getter)
-(defsetting http-channel-host-strategy
+(defsetting http-channel-allowed-networks
(deferred-tru (str "Controls which types of hosts are allowed as HTTP channel destinations.\n"
"Options:\n"
"- external-only (default - only external hosts)\n"
@@ -325,11 +325,12 @@
:visibility :internal
: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-host-strategy! Only values of external-only, allow-private, and allow-all are allowed.")))
- (setting/set-value-of-type! :keyword :http-channel-host-strategy 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)))
(defsetting slack-configured?
"Is Slack integration configured?"
diff --git a/src/metabase/tiles/settings.clj b/src/metabase/tiles/settings.clj
index 350184c4951..e6d44d56ac9 100644
--- a/src/metabase/tiles/settings.clj
+++ b/src/metabase/tiles/settings.clj
@@ -1,11 +1,86 @@
(ns metabase.tiles.settings
(:require
- [metabase.settings.core :refer [defsetting]]
- [metabase.util.i18n :as i18n]))
+ [clojure.java.io :as io]
+ [clojure.string :as str]
+ [metabase.premium-features.core :as premium-features]
+ [metabase.settings.core :as setting :refer [defsetting]]
+ [metabase.util.http :as u.http]
+ [metabase.util.i18n :as i18n :refer [tru]])
+ (:import
+ (java.net URL)))
+
+(set! *warn-on-reflection* true)
+
+(defn- concrete-tile-url
+ "`template` with its `{...}` placeholders filled in, so it parses as — and is host-checked as — the URL that
+ will actually be requested. `{s}` (the subdomain shard) becomes `a`, matching the static-map renderer."
+ [template]
+ (-> (str template)
+ (str/replace "{s}" "a")
+ (str/replace #"\{[^{}]*\}" "0")))
+
+(defn- relative-template?
+ "Whether `url` is a same-origin path like `/tiles/{z}/{x}/{y}.png` — resolved by the browser against the
+ Metabase host, so it names no outside host to validate. `//host/...` is protocol-relative, not relative."
+ [^String url]
+ (and (str/starts-with? url "/")
+ (not (str/starts-with? url "//"))))
+
+(defsetting map-tile-server-allowed-networks
+ "Controls which networks Metabase may connect to for map tile servers.
+ Options:
+ - allow-private (external + private networks but NOT loopback or link-local)
+ - external-only (only globally routable public addresses)
+ - allow-all (no restrictions).
+ Defaults to external-only on Metabase Cloud and allow-private when self-hosted."
+ :type :keyword
+ :visibility :internal
+ :export? false
+ :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)))
+
+(defn- valid-map-tile-server-url?
+ "Whether `template` is safe to store. It must be http(s) and its host must be allowed
+ by [[map-tile-server-allowed-networks]]. A host that cannot be resolved at all is rejected rather than
+ trusted. Self-hosted that policy defaults to `:allow-private`, not `:external-only`, because the browser
+ is what fetches these tiles: an on-prem instance with no internet egress can only show maps via a tile
+ server on its own network. The server-side fetch in [[metabase.channel.render.maps]] applies the same
+ policy, so a tile server that is legal to configure is one subscription renders can actually reach."
+ [template]
+ (let [url (concrete-tile-url template)]
+ (or (relative-template? url)
+ (try
+ (let [^URL parsed (io/as-url url)
+ policy (map-tile-server-allowed-networks)
+ host (.getHost parsed)]
+ (and (contains? #{"http" "https"} (.getProtocol parsed))
+ (or (= policy :allow-all)
+ ;; [[u.http/host-allowed-for-network-policy?]] deliberately allows a host it cannot
+ ;; resolve, so a DNS outage on a real warehouse surfaces as a connection error rather
+ ;; than an accusation. Here the value is only being stored, so we can afford to be
+ ;; stricter -- and need to be: an obfuscated literal like `0xa9fea9fe`
+ ;; (169.254.169.254) presents as an unresolvable host on a modern JDK.
+ (and (seq (u.http/host->inet-addresses host))
+ (u.http/host-allowed-for-network-policy? policy host)))))
+ (catch Throwable _ false)))))
(defsetting map-tile-server-url
(i18n/deferred-tru "The map tile server URL template used in map visualizations, for example from OpenStreetMaps or MapBox.")
:encryption :when-encryption-key-set
:default "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
:visibility :public
- :audit :getter)
+ :audit :getter
+ :setter (fn [new-value]
+ (when-not (or (str/blank? new-value)
+ (valid-map-tile-server-url? new-value))
+ (throw (ex-info (tru "Invalid map tile server URL: must be a relative path, or an http:// or https:// URL whose host is resolvable and permitted by map-tile-server-allowed-networks.")
+ {:status-code 400})))
+ (setting/set-value-of-type! :string :map-tile-server-url new-value)))
diff --git a/src/metabase/util/http.clj b/src/metabase/util/http.clj
index c1721abe0cb..3badfb82bee 100644
--- a/src/metabase/util/http.clj
+++ b/src/metabase/util/http.clj
@@ -176,15 +176,10 @@
(throw (ex-info "Refusing to connect to a non-permitted network address"
{:ssrf true :policy policy :host host}))))))))
-(def ^DnsResolver ^:private ssrf-safe-dns-resolver
- "The strict `:external-only` resolver (public addresses only) used by [[fetch-bytes]].
- See [[network-policy-dns-resolver]]."
- (network-policy-dns-resolver :external-only))
-
(defn safe-url?
"True if `url` is safe to fetch from untrusted input: HTTPS scheme, no userinfo, and a real DNS
hostname (not an IP literal, not localhost/metadata/internal). Note this is a cheap pre-check;
- the resolved-IP validation in [[ssrf-safe-dns-resolver]] is what closes the rebinding gap."
+ the resolved-IP validation in [[network-policy-dns-resolver]] is what closes the rebinding gap."
[^String url]
(try
(let [parsed (URL. url)
@@ -198,6 +193,23 @@
(not (some #(str/ends-with? host %) blocked-fetch-host-suffixes))))
(catch Throwable _ false)))
+(defn- fetchable-url?
+ "Whether [[fetch-bytes]] may request `url` under `policy`.
+
+ Under the default `:external-only` this is [[safe-url?]] -- the strict pre-check for a URL that came from
+ untrusted input. A looser policy is a deliberate admin decision that Metabase may reach that network (an
+ on-prem tile server, say), so plain `http` and IP-literal hosts are accepted there; which addresses are
+ actually reachable is still decided by the policy's [[network-policy-dns-resolver]] at connect time."
+ [^String url policy]
+ (if (= policy :external-only)
+ (safe-url? url)
+ (try
+ (let [parsed (URL. url)]
+ (and (contains? #{"http" "https"} (lower-case-en (str (.getProtocol parsed))))
+ (str/blank? (str (.getUserInfo parsed)))
+ (not (str/blank? (str (.getHost parsed))))))
+ (catch Throwable _ false))))
+
(defn- read-bounded
"Read up to `max` bytes from `in`; returns the byte[] or nil if the stream exceeds `max`."
^bytes [^InputStream in max]
@@ -226,21 +238,27 @@
:allowed-content-types set of lower-cased content-types to accept; nil/empty accepts any
:max-bytes download cap in bytes (default 20 MB)
:timeout-ms socket + connection timeout (default 8000)
- :user-agent `User-Agent` header (default a descriptive Metabase UA)"
+ :user-agent `User-Agent` header (default a descriptive Metabase UA)
+ :network-policy which networks may be reached, per [[address-allowed-for-network-policy?]]
+ (default `:external-only`). Anything looser also relaxes the URL pre-check to
+ allow `http` and IP-literal hosts, since those are the shape an internal host
+ an admin has deliberately allowed usually takes."
([url] (fetch-bytes url nil))
- ([url {:keys [allowed-content-types max-bytes timeout-ms user-agent]
- :or {max-bytes fetch-default-max-bytes
- timeout-ms fetch-default-timeout-ms
- user-agent fetch-default-user-agent}}]
- (when (safe-url? url)
+ ([url {:keys [allowed-content-types max-bytes network-policy timeout-ms user-agent]
+ :or {max-bytes fetch-default-max-bytes
+ network-policy :external-only
+ timeout-ms fetch-default-timeout-ms
+ user-agent fetch-default-user-agent}}]
+ (when (fetchable-url? url network-policy)
(try
- (let [resp (http/get url {:as :stream
- :redirect-strategy :none
- :socket-timeout timeout-ms
- :connection-timeout timeout-ms
- :throw-exceptions false
- :headers {"User-Agent" user-agent}
- :dns-resolver ssrf-safe-dns-resolver})
+ (let [resp (http/get url (m/assoc-some
+ {:as :stream
+ :redirect-strategy :none
+ :socket-timeout timeout-ms
+ :connection-timeout timeout-ms
+ :throw-exceptions false
+ :headers {"User-Agent" user-agent}}
+ :dns-resolver (network-policy-dns-resolver network-policy)))
ctype (response-content-type resp)
^InputStream body (:body resp)]
(try
diff --git a/test/metabase/channel/api/channel_test.clj b/test/metabase/channel/api/channel_test.clj
index 1fcde494926..1d9594388e4 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-host-strategy :allow-all]
+ (mt/with-temporary-setting-values [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}
@@ -184,7 +184,7 @@
:auth-method "none"
:auth-info {}}})))))))
-(deftest test-channel-host-strategy-test
+(deftest test-channel-allowed-networks-test
(let [channel-test (fn [url status-code]
(mt/user-http-request :crowberto :post status-code "channel/test"
{:type "channel/http"
@@ -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-host-strategy :allow-private]
+ (mt/with-temporary-setting-values [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-host-strategy :allow-all]
+ (mt/with-temporary-setting-values [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.clj
index 0189f1cdf07..ceaef44e993 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-host-strategy :allow-all]
+ (mt/with-temporary-setting-values [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-host-strategy :allow-all]
+ (mt/with-temporary-setting-values [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-host-strategy :allow-all]
+ (mt/with-temporary-setting-values [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-host-strategy :allow-all]
+ (mt/with-temporary-setting-values [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-host-strategy :allow-all]
+ (mt/with-temporary-setting-values [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-host-strategy :allow-all]
+ (mt/with-temporary-setting-values [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-host-strategy :external-only]
+ (mt/with-temporary-setting-values [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-host-strategy :external-only]
+ (mt/with-temporary-setting-values [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-host-strategy strategy]
+ (mt/with-temporary-setting-values [http-channel-allowed-networks 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-host-strategy :allow-all]
+ (mt/with-temporary-setting-values [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.clj
index 4136bde0912..9819e850bcf 100644
--- a/test/metabase/channel/render/maps_test.clj
+++ b/test/metabase/channel/render/maps_test.clj
@@ -6,6 +6,7 @@
[metabase.channel.render.maps :as maps]
[metabase.pulse.render.test-util :as render.tu]
[metabase.test :as mt]
+ [metabase.util.http :as u.http]
[metabase.util.match :as match])
(:import
(java.awt Color)
@@ -73,3 +74,44 @@
^Color high (grid-color 10.0 0.0 10.0)]
(is (> (.getGreen low) (.getRed low)) "low end is greenish")
(is (> (.getRed high) (.getGreen high)) "high end is reddish"))))
+
+(deftest fetch-tile-refuses-internal-hosts-test
+ (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 reachable
+ from any subscription render"
+ (mt/with-temporary-setting-values [map-tile-server-allowed-networks :external-only]
+ (let [requested (atom [])]
+ (fake/with-fake-routes {#".*" (fn [req]
+ (swap! requested conj (:url req))
+ {:status 200 :headers {} :body (render.tu/blank-tile-png)})}
+ (doseq [template ["http://127.0.0.1:8899/{z}/{x}/{y}.png"
+ "http://localhost:8899/{z}/{x}/{y}.png"
+ "http://169.254.169.254/{z}/{x}/{y}.png"
+ "http://10.0.0.1/{z}/{x}/{y}.png"
+ ;; http is not fetched server-side either: under this policy the hardened
+ ;; client is https-only
+ "http://tile.example.com/{z}/{x}/{y}.png"]]
+ (testing template
+ (is (nil? (#'maps/fetch-tile template 3 4 2)))))
+ (is (empty? @requested)
+ "the JVM issued no outbound request at all"))))))
+
+(deftest fetch-tile-honors-allow-private-test
+ (testing "under allow-private (the self-hosted default) a tile server on the deployment's own network is
+ fetched, 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]
+ (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,
+ which clj-http-fake would bypass, so assert on the policy itself"
+ ;; only hosts that certainly resolve: an unresolvable host is deliberately *allowed* by the policy
+ ;; check (a DNS outage should surface as a connection error, not an accusation)
+ (doseq [host ["127.0.0.1" "localhost" "169.254.169.254"]]
+ (testing host
+ (is (false? (u.http/host-allowed-for-network-policy? :allow-private host)))))
+ (is (true? (u.http/host-allowed-for-network-policy? :allow-private "10.0.0.1")))))
+
+(deftest fetch-tile-fetches-public-tiles-test
+ (testing "a public https tile server is still fetched"
+ (fake/with-fake-routes (render.tu/fake-tile-routes #"https://.*\.tile\.example\.com/.*")
+ (is (some? (#'maps/fetch-tile "https://{s}.tile.example.com/{z}/{x}/{y}.png" 5 4 2))))))
diff --git a/test/metabase/pulse/api/pulse_test.clj b/test/metabase/pulse/api/pulse_test.clj
index 64cb2554b16..ca21f554c19 100644
--- a/test/metabase/pulse/api/pulse_test.clj
+++ b/test/metabase/pulse/api/pulse_test.clj
@@ -1065,7 +1065,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-host-strategy :allow-all]
+ (mt/with-temporary-setting-values [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.clj
index b96f76df82a..139f2a5ed25 100644
--- a/test/metabase/pulse/send_test.clj
+++ b/test/metabase/pulse/send_test.clj
@@ -652,7 +652,7 @@
(swap! requests conj req)
{:status 200
:body "ok"}))]
- (mt/with-temporary-setting-values [http-channel-host-strategy :allow-all]
+ (mt/with-temporary-setting-values [http-channel-allowed-networks :allow-all]
(notification.tu/with-notification-testing-setup!
(channel.http-test/with-server [url [endpoint]]
(mt/with-temp
diff --git a/test/metabase/server/middleware/security_test.clj b/test/metabase/server/middleware/security_test.clj
index 46d9cc5f1b6..edfd734dcb0 100644
--- a/test/metabase/server/middleware/security_test.clj
+++ b/test/metabase/server/middleware/security_test.clj
@@ -957,8 +957,8 @@
(is (= "img-src 'self' data: https://*.tile.openstreetmap.org"
(csp-directive "img-src")))))
(testing "custom tile server host and port are allowed; path and query (e.g. api keys) are dropped"
- (mt/with-temporary-setting-values [map-tile-server-url "https://tiles.example.com:8443/{z}/{x}/{y}.png?apikey=SECRET"]
- (is (= "img-src 'self' data: https://tiles.example.com:8443"
+ (mt/with-temporary-setting-values [map-tile-server-url "https://example.com:8443/{z}/{x}/{y}.png?apikey=SECRET"]
+ (is (= "img-src 'self' data: https://example.com:8443"
(csp-directive "img-src")))))
(testing "a relative tile template contributes no host"
(mt/with-temporary-setting-values [map-tile-server-url "/local/{z}/{x}/{y}.png"]
diff --git a/test/metabase/tiles/settings_test.clj b/test/metabase/tiles/settings_test.clj
new file mode 100644
index 00000000000..d48573c5cdd
--- /dev/null
+++ b/test/metabase/tiles/settings_test.clj
@@ -0,0 +1,99 @@
+(ns metabase.tiles.settings-test
+ (:require
+ [clojure.test :refer :all]
+ [metabase.premium-features.test-util :as premium-features.tu]
+ [metabase.test :as mt]
+ [metabase.tiles.settings :as tiles.settings]))
+
+(def ^:private osm-template "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")
+
+(defn- set-tile-url!
+ "Set the tile URL, returning the value actually stored."
+ [template]
+ (tiles.settings/map-tile-server-url! template)
+ (tiles.settings/map-tile-server-url))
+
+(deftest map-tile-server-url-rejects-unreachable-hosts-test
+ (testing "under the default policy the setter refuses loopback, link-local, and unresolvable hosts"
+ (mt/with-temporary-setting-values [map-tile-server-url osm-template]
+ (doseq [template ["http://127.0.0.1:8899/{z}/{x}/{y}.png"
+ "http://localhost:8899/{z}/{x}/{y}.png"
+ "http://[::1]/{z}/{x}/{y}.png"
+ "http://169.254.169.254/{z}/{x}/{y}.png" ; cloud metadata
+ "http://0xa9fea9fe/{z}/{x}/{y}.png" ; the same, in hex
+ ;; a host that resolves to nothing is not trusted either -- that is how the
+ ;; obfuscated literal above presents itself on a modern JDK
+ "https://tiles.invalid/{z}/{x}/{y}.png"
+ "ftp://example.com/{z}/{x}/{y}.png"
+ "file:///etc/passwd"]]
+ (testing template
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Invalid map tile server URL"
+ (tiles.settings/map-tile-server-url! template)))
+ (is (= osm-template (tiles.settings/map-tile-server-url))
+ "the rejected value is not stored"))))))
+
+(deftest map-tile-server-allowed-networks-default-test
+ (testing "the default policy depends on where we are running: on Cloud a tile server on an internal
+ address 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]
+ (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]
+ (premium-features.tu/with-premium-features #{:hosting}
+ (is (= :allow-all (tiles.settings/map-tile-server-allowed-networks))))))))
+
+(deftest map-tile-server-url-allows-private-tile-servers-test
+ (testing "self-hosted, a tile server on the deployment's own network is a long-standing, legitimate
+ configuration: this setting is consumed by the browser, so an on-prem instance with no internet
+ egress 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))))))))
+ (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")))))))
+
+(deftest map-tile-server-url-accepts-valid-templates-test
+ (testing "the setter still accepts the templates admins legitimately configure"
+ (mt/with-temporary-setting-values [map-tile-server-url osm-template]
+ (doseq [template [osm-template
+ "http://192.0.2.0/{z}/{x}/{y}.png" ; public IP literal
+ "https://example.com:8443/{z}/{x}/{y}.png?apikey=SEKRIT"
+ "/local/{z}/{x}/{y}.png"]] ; same-origin, browser-only
+ (testing template
+ (is (= template (set-tile-url! template)))))
+ (testing "clearing the setting falls back to the default"
+ (tiles.settings/map-tile-server-url! nil)
+ (is (= osm-template (tiles.settings/map-tile-server-url)))))))
+
+(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]
+ (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]
+ (doseq [template ["http://127.0.0.1:8899/{z}/{x}/{y}.png"
+ "https://tiles.invalid/{z}/{x}/{y}.png"]]
+ (testing template
+ (is (= template (set-tile-url! template)))))
+ (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))))))
diff --git a/test/metabase/util/http_test.clj b/test/metabase/util/http_test.clj
index 9f08f115c4c..4a8b9ab4fd3 100644
--- a/test/metabase/util/http_test.clj
+++ b/test/metabase/util/http_test.clj
@@ -203,12 +203,6 @@
(is (thrown? ExceptionInfo
(http/address-allowed-for-network-policy? :allow-everything (InetAddress/getByName "127.0.0.1"))))))
-(deftest ^:parallel ssrf-safe-dns-resolver-test
- (testing "the validating resolver throws when a host resolves to a non-public address"
- ;; `localhost` resolves to loopback (no network needed) -> must be refused
- (is (thrown? ExceptionInfo
- (.resolve ^DnsResolver @#'http/ssrf-safe-dns-resolver "localhost")))))
-
(deftest ^:parallel network-policy-dns-resolver-test
(testing ":allow-all imposes no restriction, so there is no resolver (clj-http uses its default)"
(is (nil? (http/network-policy-dns-resolver :allow-all))))
@@ -237,6 +231,30 @@
"https://metadata.google.internal/x.png"]]
(is (nil? (http/fetch-bytes url)) (str "should not fetch: " url)))))
+(deftest ^:parallel fetchable-url?-test
+ (let [fetchable-url? #'http/fetchable-url?]
+ (testing ":external-only (the default) is the strict untrusted-input check"
+ (doseq [url allowed-urls]
+ (is (true? (boolean (fetchable-url? url :external-only))) url))
+ (doseq [url blocked-urls]
+ (is (false? (boolean (fetchable-url? url :external-only))) url)))
+ (testing "a looser policy accepts the http and IP-literal hosts an admin-configured internal service uses"
+ (doseq [url ["http://tiles.internal/{z}.png"
+ "http://10.0.0.5/x.png"
+ "https://10.0.0.5/x.png"
+ "http://127.0.0.1:8899/x.png"]] ; still refused later, by the DNS resolver
+ (is (true? (boolean (fetchable-url? url :allow-private))) url)))
+ (testing "a looser policy is not no policy: non-http schemes, userinfo and hostless URLs stay out"
+ (doseq [url ["file:///etc/passwd"
+ "ftp://example.com/a.png"
+ "javascript:alert(1)"
+ "http://user:[email protected]/x.png"
+ "https:///x.png"
+ "not a url"
+ ""]]
+ (doseq [policy [:allow-private :allow-all]]
+ (is (false? (boolean (fetchable-url? url policy))) (str policy " " url)))))))
+
(deftest ^:parallel read-bounded-test
(testing "reads the whole stream when under the cap"
(is (= "hello" (String. ^bytes (#'http/read-bounded (ByteArrayInputStream. (.getBytes "hello")) 100)))))