Skip to content

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

Update channel test permission model (#62015)

Ngoc Khuat7948d6df58c5
diff --git a/e2e/runner/cypress-runner-backend.js b/e2e/runner/cypress-runner-backend.js
index 6e3d91ed2bd..c8c940aa61e 100755
--- a/e2e/runner/cypress-runner-backend.js
+++ b/e2e/runner/cypress-runner-backend.js
@@ -48,6 +48,7 @@ const CypressBackend = {
MB_LAST_ANALYTICS_CHECKSUM: "-1",
MB_DB_CONNECTION_URI: "", // ignore connection URI in favor of the db file
MB_CONFIG_FILE_PATH: "__cypress__", // ignore config.yml
+ MB_HTTP_CHANNEL_HOST_STRATEGY: "allow-all", // we use a local webhook service for testing
};
/**
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 9f9f8d3d570..7140ff71cbb 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
@@ -1,6 +1,7 @@
(ns metabase-enterprise.advanced-permissions.api.channel-test
(:require
[clojure.test :refer :all]
+ [metabase.channel.impl.http-test :as channel.http-test]
[metabase.notification.test-util :as notification.tu]
[metabase.permissions.models.permissions :as perms]
[metabase.test :as mt]))
@@ -23,9 +24,17 @@
(testing (format "create channel setting with %s user" (mt/user-descriptor user))
(mt/user-http-request user :post status "channel" (assoc notification.tu/default-can-connect-channel :name (mt/random-name)))))
(get-channel [user status]
- (testing (format "get user with %s user" (mt/user-descriptor user))
+ (testing (format "get channel with %s user" (mt/user-descriptor user))
(mt/with-temp [:model/Channel {id :id} notification.tu/default-can-connect-channel]
(mt/user-http-request user :get status (str "channel/" id)))))
+ (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/user-http-request user :post status "channel/test" {:type "channel/http"
+ :details {:url (str url (:path channel.http-test/post-200))
+ :auth-method "none"
+ :auth-info {}}})))))
(include-details [user include-details?]
(mt/with-temp [:model/Channel {id :id} notification.tu/default-can-connect-channel]
(testing (format "GET /api/channel/:id with %s user" (mt/user-descriptor user))
@@ -39,8 +48,10 @@
(create-channel user 403)
(update-channel user 403)
(get-channel user 403)
+ (test-channel user 403)
(create-channel :crowberto 200)
(update-channel :crowberto 200)
+ (test-channel :crowberto 200)
(include-details :crowberto true)))
(testing "if `advanced-permissions` is enabled"
@@ -49,15 +60,19 @@
(create-channel user 403)
(update-channel user 403)
(get-channel user 403)
+ (test-channel user 403)
(create-channel :crowberto 200)
(update-channel :crowberto 200)
+ (test-channel :crowberto 200)
(include-details :crowberto true))
(testing "succeed if user's group has `setting` permission"
(perms/grant-application-permissions! group :setting)
(create-channel user 200)
(update-channel user 200)
- (create-channel :crowberto 200)
+ (test-channel user 200)
(update-channel user 200)
+ (create-channel :crowberto 200)
(include-details :crowberto true)
+ (test-channel :crowberto 200)
(include-details user true)))))))))
diff --git a/src/metabase/channel/api/channel.clj b/src/metabase/channel/api/channel.clj
index d5fedaa3d93..1afa2ca92d8 100644
--- a/src/metabase/channel/api/channel.clj
+++ b/src/metabase/channel/api/channel.clj
@@ -103,4 +103,5 @@
{:keys [type details]} :- [:map
[:type ChannelType]
[:details :map]]]
+ (perms/check-has-application-permission :setting)
(test-channel-connection! type details))
diff --git a/src/metabase/channel/impl/http.clj b/src/metabase/channel/impl/http.clj
index 02d82bc2081..3204bfd10f9 100644
--- a/src/metabase/channel/impl/http.clj
+++ b/src/metabase/channel/impl/http.clj
@@ -4,8 +4,10 @@
[java-time.api :as t]
[metabase.channel.core :as channel]
[metabase.channel.render.core :as channel.render]
+ [metabase.channel.settings :as channel.settings]
[metabase.channel.shared :as channel.shared]
[metabase.channel.urls :as urls]
+ [metabase.util :as u]
[metabase.util.i18n :refer [tru]]
[metabase.util.json :as json]
[metabase.util.malli :as mu]
@@ -37,6 +39,9 @@
(mu/defmethod channel/send! :channel/http
[{{:keys [url method auth-method auth-info]} :details} :- HTTPChannel
request]
+ (when-not (u/valid-host? (channel.settings/http-channel-host-strategy) url)
+ (throw (ex-info (tru "URLs referring to hosts that supply internal hosting metadata are prohibited.")
+ {:status-code 400})))
(let [req (merge
{:accept :json
:content-type :json
diff --git a/src/metabase/channel/settings.clj b/src/metabase/channel/settings.clj
index 416a53ac5ec..b4705eb6d34 100644
--- a/src/metabase/channel/settings.clj
+++ b/src/metabase/channel/settings.clj
@@ -302,3 +302,19 @@
:export? false
:visibility :internal
:audit :getter)
+
+(defsetting http-channel-host-strategy
+ (deferred-tru (str "Controls which types of hosts are allowed as HTTP channel destinations.\n"
+ "Options:\n"
+ "- external-only (default - only external hosts)\n"
+ "- allow-private (external + private networks but NOT localhost)\n"
+ "- allow-all (no restrictions including localhost).\n"))
+ :type :keyword
+ :visibility :internal
+ :default :external-only
+ :export? false
+ :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)))
diff --git a/src/metabase/util.cljc b/src/metabase/util.cljc
index c8b9a4a6eba..976928f7fac 100644
--- a/src/metabase/util.cljc
+++ b/src/metabase/util.cljc
@@ -10,6 +10,7 @@
[clojure.pprint :as pprint]
^{:clj-kondo/ignore [:discouraged-namespace]}
[metabase.util.jvm :as u.jvm]
+ [metabase.util.http :as u.http]
[metabase.util.string :as u.str]
[potemkin :as p]
[puget.printer]
@@ -78,7 +79,9 @@
with-timeout
with-us-locale]
[u.str
- build-sentence]))
+ build-sentence]
+ [u.http
+ valid-host?]))
(defmacro or-with
"Like or, but determines truthiness with `pred`."
diff --git a/src/metabase/util/http.clj b/src/metabase/util/http.clj
index 7bfd9793947..c1bdef05a00 100644
--- a/src/metabase/util/http.clj
+++ b/src/metabase/util/http.clj
@@ -2,7 +2,11 @@
(:require
[clj-http.client :as http]
[medley.core :as m]
- [metabase.util.json :as json]))
+ [metabase.util.json :as json])
+ (:import
+ (java.net InetAddress URL)))
+
+(set! *warn-on-reflection* true)
(defn- parse-http-headers [headers]
(json/decode headers))
@@ -14,3 +18,26 @@
(string? headers) parse-http-headers)
response (http/get url (m/assoc-some {:as :json} :headers headers))]
(:body response)))
+
+(def ^:private invalid-hosts
+ #{"metadata.google.internal"}) ; internal metadata for GCP
+
+(defn valid-host?
+ "Check whether url is valid based on the given strategy:
+ :external-only - only external hosts
+ :allow-private - external + private networks but not localhost/loopback
+ :allow-all - no restrictions"
+ [strategy url]
+ (case strategy
+ :allow-all true
+ ;; For both :external-only and :allow-private, we need to check the host
+ (let [^URL url (if (string? url) (URL. url) url)
+ host (.getHost url)
+ host-name (InetAddress/getByName host)]
+ (and
+ (not (contains? invalid-hosts host))
+ (not (.isLinkLocalAddress host-name))
+ (not (.isLoopbackAddress host-name))
+ ;; Only block site-local (private) addresses for :external-only
+ (or (= strategy :allow-private)
+ (not (.isSiteLocalAddress host-name)))))))
diff --git a/test/metabase/channel/api/channel_test.clj b/test/metabase/channel/api/channel_test.clj
index 8ad0b6b69de..4230fe9e190 100644
--- a/test/metabase/channel/api/channel_test.clj
+++ b/test/metabase/channel/api/channel_test.clj
@@ -135,6 +135,16 @@
(mt/user-http-request :crowberto :put 400 (str "channel/" (:id chn-1))
(assoc chn-1 :type "metabase/metabase-test")))))))
+(deftest test-cahnnel-permission-test
+ (testing "only admin can test channel"
+ (mt/user-http-request :crowberto :post 200 "channel/test"
+ (assoc default-test-channel :details {:return-type "return-value"
+ :return-value true})))
+ (testing "non-admin gets 403"
+ (mt/user-http-request :rasta :post 403 "channel/test"
+ (assoc default-test-channel :details {:return-type "return-value"
+ :return-value true}))))
+
(deftest test-channel-connection-test
(testing "return 200 if channel connects successfully"
(is (= {:ok true}
@@ -157,32 +167,71 @@
:return-value {:errors {:email "Invalid email"}}}))))))
(deftest test-channel-http-test
- (channel.http-test/with-server [url [channel.http-test/post-200 channel.http-test/post-400]]
- (testing "status-code=200 endpoint"
- (is (= {:ok true}
- (mt/user-http-request :crowberto :post 200 "channel/test"
- {:type "channel/http"
- :details {:url (str url (:path channel.http-test/post-200))
- :auth-method "none"
- :auth-info {}}}))))
- (testing "status-code=400 endpoint"
- (is (= {:message "Failed to connect to channel"
- :data {:request-status 400
- :request-body "Bad request"}}
- (mt/user-http-request :crowberto :post 400 "channel/test"
- {:type "channel/http"
- :details {:url (str url (:path channel.http-test/post-400))
- :auth-method "none"
- :auth-info {}}}))))
- (testing "status-code=404 endpoint"
- (is (= {:message "Failed to connect to channel"
- :data {:request-status 404
- :request-body "Not found."}}
- (mt/user-http-request :crowberto :post 400 "channel/test"
- {:type "channel/http"
- :details {:url (str url "/unknown42")
- :auth-method "none"
- :auth-info {}}}))))))
+ (mt/with-temporary-setting-values [http-channel-host-strategy :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}
+ (mt/user-http-request :crowberto :post 200 "channel/test"
+ {:type "channel/http"
+ :details {:url (str url (:path channel.http-test/post-200))
+ :auth-method "none"
+ :auth-info {}}}))))
+ (testing "status-code=400 endpoint"
+ (is (= {:message "Failed to connect to channel"
+ :data {:request-status 400
+ :request-body "Bad request"}}
+ (mt/user-http-request :crowberto :post 400 "channel/test"
+ {:type "channel/http"
+ :details {:url (str url (:path channel.http-test/post-400))
+ :auth-method "none"
+ :auth-info {}}}))))
+ (testing "status-code=404 endpoint"
+ (is (= {:message "Failed to connect to channel"
+ :data {:request-status 404
+ :request-body "Not found."}}
+ (mt/user-http-request :crowberto :post 400 "channel/test"
+ {:type "channel/http"
+ :details {:url (str url "/unknown42")
+ :auth-method "none"
+ :auth-info {}}})))))))
+
+(deftest test-channel-host-strategy-test
+ (let [channel-test (fn [url status-code]
+ (mt/user-http-request :crowberto :post status-code "channel/test"
+ {:type "channel/http"
+ :details {:url url
+ :auth-method "none"
+ :auth-info {}}}))]
+
+ (testing "external-only strategy (default)"
+ (testing "blocks localhost addresses"
+ (is (= "URLs referring to hosts that supply internal hosting metadata are prohibited."
+ (:message
+ (channel-test "http://127.0.0.1:3000/api/health" 400)))))
+ (testing "blocks private network addresses"
+ (is (= "URLs referring to hosts that supply internal hosting metadata are prohibited."
+ (:message
+ (channel-test "http://192.168.1.100/api/health" 400)))))
+ (testing "blocks link local addresses"
+ (is (= "URLs referring to hosts that supply internal hosting metadata are prohibited."
+ (: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]
+ (testing "still blocks localhost addresses"
+ (is (= "URLs referring to hosts that supply internal hosting metadata are prohibited."
+ (:message
+ (channel-test "http://127.0.0.1:3000/api/health" 400)))))
+ (testing "still blocks link local addresses"
+ (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]
+ (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))))))
(deftest channel-audit-log-test
(testing "audit log for channel apis"
diff --git a/test/metabase/channel/impl/http_test.clj b/test/metabase/channel/impl/http_test.clj
index e2cd348dbf3..b445fd4cd87 100644
--- a/test/metabase/channel/impl/http_test.clj
+++ b/test/metabase/channel/impl/http_test.clj
@@ -144,97 +144,101 @@
(catch Exception e#
(ex-data e#))))
-(deftest ^:parallel can-connect-no-auth-test
- (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))
- :auth-method "none"
- :method "get"}))]
-
- (testing "connect successfully with 200"
- (is (true? (can-connect?* get-200))))
- (testing "connect successfully with 302 redirect to 200"
- (is (true? (can-connect?* get-302-redirect-200))))
- (testing "failed to connect with a 302 that redirects to 400"
- (is (= {:request-status 400
- :request-body "Bad request"}
- (exception-data (can-connect?* get-302-redirect-400)))))
- (testing "failed to conenct to a 400"
- (is (= {:request-status 400
- :request-body "Bad request"}
- (exception-data (can-connect?* get-400)))))
- (is (=? {:request-status 500
- :request-body "Internal server error"}
- (exception-data (can-connect?* get-500)))))))
-
-(deftest ^:parallel can-connect-header-auth-test
- (with-server [url [(make-route :get "/user"
- (fn [x]
- (if (= "SECRET" (get-in x [:headers "x-api-key"]))
- {:status 200
- :body "Hello, world!"}
- {:status 401
- :body "Unauthorized"})))]]
- (testing "connect successfully with header auth"
- (is (true? (can-connect? {:url (str url "/user")
- :method "get"
- :auth-method "header"
- :auth-info {:x-api-key "SECRET"}}))))
-
- (testing "fail to connect with header auth"
- (is (= {:request-status 401
- :request-body "Unauthorized"}
- (exception-data (can-connect? {:url (str url "/user")
- :method "get"
- :auth-method "header"
- :auth-info {:x-api-key "WRONG"}})))))))
-
-(deftest ^:parallel can-connect-query-param-auth-test
- (with-server [url [(make-route :get "/user"
- (fn [x]
- (if (= ["qnkhuat" "secretpassword"]
- [(get-in x [:query-params "username"]) (get-in x [:query-params "password"])])
- {:status 200
- :body "Hello, world!"}
- {:status 401
- :body "Unauthorized"})))]]
- (testing "connect successfully with query-param auth"
- (is (true? (can-connect? {:url (str url "/user")
- :method "get"
- :auth-method "query-param"
- :auth-info {:username "qnkhuat"
- :password "secretpassword"}}))))
- (testing "fail to connect with query-param auth"
- (is (= {:request-status 401
- :request-body "Unauthorized"}
- (exception-data (can-connect? {:url (str url "/user")
- :method "get"
- :auth-method "query-param"
- :auth-info {:username "qnkhuat"
- :password "wrongpassword"}})))))))
-
-(deftest ^:parallel can-connect-request-body-auth-test
- (with-server [url [(make-route :post "/user"
- (fn [x]
- (if (= "SECRET_TOKEN" (get-in x [:body :token]))
- {:status 200
- :body "Hello, world!"}
- {:status 401
- :body "Unauthorized"})))]]
- (testing "connect successfully with request-body auth"
- (is (true? (can-connect? {:url (str url "/user")
- :method "post"
- :auth-method "request-body"
- :auth-info {:token "SECRET_TOKEN"}}))))
- (testing "fail to connect with request-body auth"
- (is (= {:request-status 401
- :request-body "Unauthorized"}
- (exception-data (can-connect? {:url (str url "/user")
- :method "post"
- :auth-method "request-body"
- :auth-info {:token "WRONG_TOKEN"}})))))))
-
-(deftest ^:parallel can-connect?-errors-test
+(deftest can-connect-no-auth-test
+ (mt/with-temporary-setting-values [http-channel-host-strategy :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))
+ :auth-method "none"
+ :method "get"}))]
+
+ (testing "connect successfully with 200"
+ (is (true? (can-connect?* get-200))))
+ (testing "connect successfully with 302 redirect to 200"
+ (is (true? (can-connect?* get-302-redirect-200))))
+ (testing "failed to connect with a 302 that redirects to 400"
+ (is (= {:request-status 400
+ :request-body "Bad request"}
+ (exception-data (can-connect?* get-302-redirect-400)))))
+ (testing "failed to conenct to a 400"
+ (is (= {:request-status 400
+ :request-body "Bad request"}
+ (exception-data (can-connect?* get-400)))))
+ (is (=? {:request-status 500
+ :request-body "Internal server error"}
+ (exception-data (can-connect?* get-500))))))))
+
+(deftest can-connect-header-auth-test
+ (mt/with-temporary-setting-values [http-channel-host-strategy :allow-all]
+ (with-server [url [(make-route :get "/user"
+ (fn [x]
+ (if (= "SECRET" (get-in x [:headers "x-api-key"]))
+ {:status 200
+ :body "Hello, world!"}
+ {:status 401
+ :body "Unauthorized"})))]]
+ (testing "connect successfully with header auth"
+ (is (true? (can-connect? {:url (str url "/user")
+ :method "get"
+ :auth-method "header"
+ :auth-info {:x-api-key "SECRET"}}))))
+
+ (testing "fail to connect with header auth"
+ (is (= {:request-status 401
+ :request-body "Unauthorized"}
+ (exception-data (can-connect? {:url (str url "/user")
+ :method "get"
+ :auth-method "header"
+ :auth-info {:x-api-key "WRONG"}}))))))))
+
+(deftest can-connect-query-param-auth-test
+ (mt/with-temporary-setting-values [http-channel-host-strategy :allow-all]
+ (with-server [url [(make-route :get "/user"
+ (fn [x]
+ (if (= ["qnkhuat" "secretpassword"]
+ [(get-in x [:query-params "username"]) (get-in x [:query-params "password"])])
+ {:status 200
+ :body "Hello, world!"}
+ {:status 401
+ :body "Unauthorized"})))]]
+ (testing "connect successfully with query-param auth"
+ (is (true? (can-connect? {:url (str url "/user")
+ :method "get"
+ :auth-method "query-param"
+ :auth-info {:username "qnkhuat"
+ :password "secretpassword"}}))))
+ (testing "fail to connect with query-param auth"
+ (is (= {:request-status 401
+ :request-body "Unauthorized"}
+ (exception-data (can-connect? {:url (str url "/user")
+ :method "get"
+ :auth-method "query-param"
+ :auth-info {:username "qnkhuat"
+ :password "wrongpassword"}}))))))))
+
+(deftest can-connect-request-body-auth-test
+ (mt/with-temporary-setting-values [http-channel-host-strategy :allow-all]
+ (with-server [url [(make-route :post "/user"
+ (fn [x]
+ (if (= "SECRET_TOKEN" (get-in x [:body :token]))
+ {:status 200
+ :body "Hello, world!"}
+ {:status 401
+ :body "Unauthorized"})))]]
+ (testing "connect successfully with request-body auth"
+ (is (true? (can-connect? {:url (str url "/user")
+ :method "post"
+ :auth-method "request-body"
+ :auth-info {:token "SECRET_TOKEN"}}))))
+ (testing "fail to connect with request-body auth"
+ (is (= {:request-status 401
+ :request-body "Unauthorized"}
+ (exception-data (can-connect? {:url (str url "/user")
+ :method "post"
+ :auth-method "request-body"
+ :auth-info {:token "WRONG_TOKEN"}}))))))))
+
+(deftest can-connect?-errors-test
(testing "throws an appriopriate errors if details are invalid"
(testing "invalid url"
(is (= {:errors {:url [(deferred-tru "value must be a valid URL.")]}}
@@ -249,102 +253,105 @@
(is (=? {:errors {:xyz ["disallowed key"]}}
(exception-data (can-connect? {:xyz "hello world"})))))
- (with-server [url [get-400]]
- (is (= {:request-body "Bad request"
- :request-status 400}
- (exception-data (can-connect? {:url (str url (:path get-400))
- :method "get"
- :auth-method "none"})))))
-
- (with-server [url [(make-route :get "/test_http_channel_400"
- (fn [_]
- {:status 400
- :body {:message "too bad"}}))]]
- (testing "attempt to json parse the response body if it's a string"
- (is (= {:request-body {"message" "too bad"}
+ (mt/with-temporary-setting-values [http-channel-host-strategy :allow-all]
+ (with-server [url [get-400]]
+ (is (= {:request-body "Bad request"
:request-status 400}
(exception-data (can-connect? {:url (str url (:path get-400))
:method "get"
- :auth-method "none"}))))))))
-
-(deftest ^:parallel send!-test
- (testing "basic send"
- (with-captured-http-requests [requests]
- (channel/send! {:type :channel/http
- :details {:url "https://www.secret_service.xyz"
- :auth-method "none"
- :method "get"}}
- nil)
- (is (= (merge default-request
- {:method :get
- :url "https://www.secret_service.xyz"})
- (first @requests)))))
-
- (testing "default method is post"
- (with-captured-http-requests [requests]
- (channel/send! {:type :channel/http
- :details {:url "https://www.secret_service.xyz"
- :auth-method "none"}}
- nil)
- (is (= (merge default-request
- {:method :post
- :url "https://www.secret_service.xyz"})
- (first @requests)))))
-
- (testing "preserves req headers when use auth-method=:header"
- (with-captured-http-requests [requests]
- (channel/send! {:type :channel/http
- :details {:url "https://www.secret_service.xyz"
- :auth-method "header"
- :auth-info {:Authorization "Bearer 123"}
- :method "get"}}
- {:headers {:X-Request-Id "123"}})
- (is (= (merge default-request
- {:method :get
- :url "https://www.secret_service.xyz"
- :headers {:Authorization "Bearer 123"
- :X-Request-Id "123"}})
- (first @requests)))))
-
- (testing "preserves req query-params when use auth-method=:query-param"
- (with-captured-http-requests [requests]
- (channel/send! {:type :channel/http
- :details {:url "https://www.secret_service.xyz"
- :auth-method "query-param"
- :auth-info {:token "123"}
- :method "get"}}
- {:query-params {:page 1}})
- (is (= (merge default-request
- {:method :get
- :url "https://www.secret_service.xyz"
- :query-params {:token "123"
- :page 1}})
- (first @requests))))))
+ :auth-method "none"})))))
+
+ (with-server [url [(make-route :get "/test_http_channel_400"
+ (fn [_]
+ {:status 400
+ :body {:message "too bad"}}))]]
+ (testing "attempt to json parse the response body if it's a string"
+ (is (= {:request-body {"message" "too bad"}
+ :request-status 400}
+ (exception-data (can-connect? {:url (str url (:path get-400))
+ :method "get"
+ :auth-method "none"})))))))))
+
+(deftest send!-test
+ (mt/with-temporary-setting-values [http-channel-host-strategy :allow-all]
+ (testing "basic send"
+ (with-captured-http-requests [requests]
+ (channel/send! {:type :channel/http
+ :details {:url "https://www.secret_service.xyz"
+ :auth-method "none"
+ :method "get"}}
+ nil)
+ (is (= (merge default-request
+ {:method :get
+ :url "https://www.secret_service.xyz"})
+ (first @requests)))))
+
+ (testing "default method is post"
+ (with-captured-http-requests [requests]
+ (channel/send! {:type :channel/http
+ :details {:url "https://www.secret_service.xyz"
+ :auth-method "none"}}
+ nil)
+ (is (= (merge default-request
+ {:method :post
+ :url "https://www.secret_service.xyz"})
+ (first @requests)))))
+
+ (testing "preserves req headers when use auth-method=:header"
+ (with-captured-http-requests [requests]
+ (channel/send! {:type :channel/http
+ :details {:url "https://www.secret_service.xyz"
+ :auth-method "header"
+ :auth-info {:Authorization "Bearer 123"}
+ :method "get"}}
+ {:headers {:X-Request-Id "123"}})
+ (is (= (merge default-request
+ {:method :get
+ :url "https://www.secret_service.xyz"
+ :headers {:Authorization "Bearer 123"
+ :X-Request-Id "123"}})
+ (first @requests)))))
+
+ (testing "preserves req query-params when use auth-method=:query-param"
+ (with-captured-http-requests [requests]
+ (channel/send! {:type :channel/http
+ :details {:url "https://www.secret_service.xyz"
+ :auth-method "query-param"
+ :auth-info {:token "123"}
+ :method "get"}}
+ {:query-params {:page 1}})
+ (is (= (merge default-request
+ {:method :get
+ :url "https://www.secret_service.xyz"
+ :query-params {:token "123"
+ :page 1}})
+ (first @requests)))))))
(deftest alert-http-channel-e2e-test
- (let [received-message (atom nil)
- receive-route (make-route :post "/test_http_channel"
- (fn [res]
- (reset! received-message res)
- {:status 200}))]
- (with-server [url [receive-route]]
- (mt/with-temp [:model/Channel {chn-id :id} {:type :channel/http
- :details {:url (str url (:path receive-route))
- :auth-method "none"}}]
- (notification.tu/with-card-notification
- [notification {:card {:dataset_query (mt/mbql-query checkins {:aggregation [:count]})}
- :handlers [{:channel_type :channel/http
- :channel_id chn-id}]}]
- (notification/send-notification! (t2/select-one :model/Notification (:id notification)) :notification/sync? true)
- (is (=? {:body {:type "alert"
- :alert_id (-> notification :payload :id)
- :alert_creator_id (mt/malli=? int?)
- :alert_creator_name (t2/select-one-fn :common_name :model/User (:creator_id notification))
- :data {:type "question"
- :question_id (-> notification :payload :card_id)
- :question_name (-> notification :payload :card :name)
- :question_url (mt/malli=? [:fn #(str/ends-with? % (-> notification :payload :card_id str))])
- :visualization (mt/malli=? [:fn #(str/starts-with? % "data:image/png;base64")])
- :raw_data {:cols ["count"] :rows [[1000]]}}
- :sent_at (mt/malli=? :any)}}
- @received-message)))))))
+ (mt/with-temporary-setting-values [http-channel-host-strategy :allow-all]
+ (let [received-message (atom nil)
+ receive-route (make-route :post "/test_http_channel"
+ (fn [res]
+ (reset! received-message res)
+ {:status 200}))]
+ (with-server [url [receive-route]]
+ (mt/with-temp [:model/Channel {chn-id :id} {:type :channel/http
+ :details {:url (str url (:path receive-route))
+ :auth-method "none"}}]
+ (notification.tu/with-card-notification
+ [notification {:card {:dataset_query (mt/mbql-query checkins {:aggregation [:count]})}
+ :handlers [{:channel_type :channel/http
+ :channel_id chn-id}]}]
+ (notification/send-notification! (t2/select-one :model/Notification (:id notification)) :notification/sync? true)
+ (is (=? {:body {:type "alert"
+ :alert_id (-> notification :payload :id)
+ :alert_creator_id (mt/malli=? int?)
+ :alert_creator_name (t2/select-one-fn :common_name :model/User (:creator_id notification))
+ :data {:type "question"
+ :question_id (-> notification :payload :card_id)
+ :question_name (-> notification :payload :card :name)
+ :question_url (mt/malli=? [:fn #(str/ends-with? % (-> notification :payload :card_id str))])
+ :visualization (mt/malli=? [:fn #(str/starts-with? % "data:image/png;base64")])
+ :raw_data {:cols ["count"] :rows [[1000]]}}
+ :sent_at (mt/malli=? :any)}}
+ @received-message))))))))
diff --git a/test/metabase/pulse/api/pulse_test.clj b/test/metabase/pulse/api/pulse_test.clj
index 5adb5fb8141..3994a512b75 100644
--- a/test/metabase/pulse/api/pulse_test.clj
+++ b/test/metabase/pulse/api/pulse_test.clj
@@ -1086,44 +1086,45 @@
(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
- (let [requests (atom [])
- endpoint (channel.http-test/make-route
- :post "/test"
- (fn [req]
- (swap! requests conj req)
- {:status 200
- :body "ok"}))]
- (channel.http-test/with-server [url [endpoint]]
- (mt/with-temp
- [:model/Card card {:dataset_query (mt/mbql-query orders {:aggregation [[:count]]})}
- :model/Channel channel {:type :channel/http
- :details {:url (str url "/test")
- :auth-method :none}}]
- (mt/user-http-request :rasta :post 200 "pulse/test"
- {:name (mt/random-name)
- :cards [{:id (:id card)
- :include_csv false
- :include_xls false
- :dashboard_card_id nil}]
- :channels [{:enabled true
- :channel_type "http"
- :channel_id (:id channel)
- :schedule_type "daily"
- :schedule_hour 12
- :schedule_day nil
- :recipients []}]
- :alert_condition "rows"})
- (is (=? {:body {:alert_creator_id (mt/user->id :rasta)
- :alert_creator_name "Rasta Toucan"
- :alert_id nil
- :data {:question_id (:id card)
- :question_name (mt/malli=? string?)
- :question_url (mt/malli=? string?)
- :raw_data {:cols ["count"], :rows [[18760]]},
- :type "question"
- :visualization (mt/malli=? [:fn #(str/starts-with? % "data:image/png;base64,")])}
- :type "alert"}}
- (first @requests)))))))))
+ (mt/with-temporary-setting-values [http-channel-host-strategy :allow-all]
+ (let [requests (atom [])
+ endpoint (channel.http-test/make-route
+ :post "/test"
+ (fn [req]
+ (swap! requests conj req)
+ {:status 200
+ :body "ok"}))]
+ (channel.http-test/with-server [url [endpoint]]
+ (mt/with-temp
+ [:model/Card card {:dataset_query (mt/mbql-query orders {:aggregation [[:count]]})}
+ :model/Channel channel {:type :channel/http
+ :details {:url (str url "/test")
+ :auth-method :none}}]
+ (mt/user-http-request :rasta :post 200 "pulse/test"
+ {:name (mt/random-name)
+ :cards [{:id (:id card)
+ :include_csv false
+ :include_xls false
+ :dashboard_card_id nil}]
+ :channels [{:enabled true
+ :channel_type "http"
+ :channel_id (:id channel)
+ :schedule_type "daily"
+ :schedule_hour 12
+ :schedule_day nil
+ :recipients []}]
+ :alert_condition "rows"})
+ (is (=? {:body {:alert_creator_id (mt/user->id :rasta)
+ :alert_creator_name "Rasta Toucan"
+ :alert_id nil
+ :data {:question_id (:id card)
+ :question_name (mt/malli=? string?)
+ :question_url (mt/malli=? string?)
+ :raw_data {:cols ["count"], :rows [[18760]]},
+ :type "question"
+ :visualization (mt/malli=? [:fn #(str/starts-with? % "data:image/png;base64,")])}
+ :type "alert"}}
+ (first @requests))))))))))
(deftest send-test-pulse-validate-emails-test
(testing (str "POST /api/pulse/test should call " `pulse-channel/validate-email-domains)
diff --git a/test/metabase/pulse/send_test.clj b/test/metabase/pulse/send_test.clj
index 38298d8785f..01702551ce3 100644
--- a/test/metabase/pulse/send_test.clj
+++ b/test/metabase/pulse/send_test.clj
@@ -636,32 +636,33 @@
(swap! requests conj req)
{:status 200
:body "ok"}))]
- (notification.tu/with-notification-testing-setup!
- (channel.http-test/with-server [url [endpoint]]
- (mt/with-temp
- [:model/Card card {:dataset_query (mt/mbql-query orders {:aggregation [[:count]]})}
- :model/Channel channel {:type :channel/http
- :details {:url (str url "/test")
- :auth-method :none}}
- :model/Pulse {pulse-id :id} {:name "Test Pulse"
- :alert_condition "rows"}
- :model/PulseCard _ {:pulse_id pulse-id
- :card_id (:id card)}
- :model/PulseChannel _ {:pulse_id pulse-id
- :channel_type "http"
- :channel_id (:id channel)}]
- (pulse.send/send-pulse! (t2/select-one :model/Pulse pulse-id))
- (is (=? {:body {:alert_creator_id (mt/user->id :rasta)
- :alert_creator_name "Rasta Toucan"
- :alert_id pulse-id
- :data {:question_id (:id card)
- :question_name (mt/malli=? string?)
- :question_url (mt/malli=? string?)
- :raw_data {:cols ["count"], :rows [[18760]]},
- :type "question"
- :visualization (mt/malli=? [:fn #(str/starts-with? % "data:image/png;base64,")])}
- :type "alert"}}
- (first @requests)))))))))
+ (mt/with-temporary-setting-values [http-channel-host-strategy :allow-all]
+ (notification.tu/with-notification-testing-setup!
+ (channel.http-test/with-server [url [endpoint]]
+ (mt/with-temp
+ [:model/Card card {:dataset_query (mt/mbql-query orders {:aggregation [[:count]]})}
+ :model/Channel channel {:type :channel/http
+ :details {:url (str url "/test")
+ :auth-method :none}}
+ :model/Pulse {pulse-id :id} {:name "Test Pulse"
+ :alert_condition "rows"}
+ :model/PulseCard _ {:pulse_id pulse-id
+ :card_id (:id card)}
+ :model/PulseChannel _ {:pulse_id pulse-id
+ :channel_type "http"
+ :channel_id (:id channel)}]
+ (pulse.send/send-pulse! (t2/select-one :model/Pulse pulse-id))
+ (is (=? {:body {:alert_creator_id (mt/user->id :rasta)
+ :alert_creator_name "Rasta Toucan"
+ :alert_id pulse-id
+ :data {:question_id (:id card)
+ :question_name (mt/malli=? string?)
+ :question_url (mt/malli=? string?)
+ :raw_data {:cols ["count"], :rows [[18760]]},
+ :type "question"
+ :visualization (mt/malli=? [:fn #(str/starts-with? % "data:image/png;base64,")])}
+ :type "alert"}}
+ (first @requests))))))))))
(deftest do-not-send-alert-with-archived-card-test
(mt/with-temp
diff --git a/test/metabase/util/http_test.clj b/test/metabase/util/http_test.clj
new file mode 100644
index 00000000000..865c58c66b1
--- /dev/null
+++ b/test/metabase/util/http_test.clj
@@ -0,0 +1,30 @@
+(ns metabase.util.http-test
+ (:require
+ [clojure.test :refer :all]
+ [metabase.util.http :as http]))
+
+(deftest valid-host?-test
+ (testing "external-only strategy (default)"
+ (is (true? (http/valid-host? :external-only "https://example.com")))
+ (is (false? (http/valid-host? :external-only "http://localhost")))
+ (is (false? (http/valid-host? :external-only "http://192.168.1.1"))))
+
+ (testing "external-only strategy explicitly"
+ (is (true? (http/valid-host? :external-only "https://example.com")))
+ (is (false? (http/valid-host? :external-only "http://localhost")))
+ (is (false? (http/valid-host? :external-only "http://192.168.1.1"))))
+
+ (testing "allow-private strategy allows private networks but not localhost"
+ (is (true? (http/valid-host? :allow-private "https://example.com")))
+ (is (true? (http/valid-host? :allow-private "http://192.168.1.1")))
+ (is (true? (http/valid-host? :allow-private "http://10.0.0.1")))
+ (is (true? (http/valid-host? :allow-private "http://172.16.0.1")))
+ (is (false? (http/valid-host? :allow-private "http://localhost")))
+ (is (false? (http/valid-host? :allow-private "http://127.0.0.1")))
+ (is (false? (http/valid-host? :allow-private "http://169.254.1.1"))))
+
+ (testing "allow-all strategy allows everything"
+ (is (true? (http/valid-host? :allow-all "https://example.com")))
+ (is (true? (http/valid-host? :allow-all "http://localhost")))
+ (is (true? (http/valid-host? :allow-all "http://192.168.1.1")))
+ (is (true? (http/valid-host? :allow-all "http://169.254.1.1")))))