Skip to content

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

Whitelist product analytics ingestion origins for data apps (#82483)

Phoomparin Mano64e7f5cfc89d
diff --git a/enterprise/backend/src/metabase_enterprise/data_apps/api.clj b/enterprise/backend/src/metabase_enterprise/data_apps/api.clj
index 7d1f197f8f8..d4afbb31541 100644
--- a/enterprise/backend/src/metabase_enterprise/data_apps/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/data_apps/api.clj
@@ -19,11 +19,14 @@
[metabase.lib-be.core :as lib-be]
[metabase.lib-be.schema :as lib-be.schema]
[metabase.lib.core :as lib]
+ [metabase.settings.core :as setting]
+ [metabase.util :as u]
[metabase.util.i18n :refer [tru]]
[metabase.util.json :as json]
[metabase.util.malli.schema :as ms])
(:import
- (java.io ByteArrayInputStream)))
+ (java.io ByteArrayInputStream)
+ (java.net URI)))
(set! *warn-on-reflection* true)
@@ -43,6 +46,27 @@
;;; ------------------------------------------------ Helpers ------------------------------------------------
+(defn- metaplow-origin
+ "Origin of the configured Metaplow collector, without its `/api/send` path.
+ The data-app sandbox accepts origins, not URLs with paths."
+ []
+ (when-let [url (setting/get-value-of-type :string :metaplow-url)]
+ (try
+ (let [uri (URI. url)
+ scheme (some-> (.getScheme uri) u/lower-case-en)
+ host (.getHost uri)
+ port (.getPort uri)]
+ (when (and (#{"http" "https"} scheme) host)
+ (str scheme "://" host (when-not (= -1 port) (str ":" port)))))
+ (catch Exception _))))
+
+(defn- bundle-allowed-hosts
+ "Origins the bundle may fetch, including the configured analytics collector."
+ [allowed-hosts]
+ (let [origin (metaplow-origin)]
+ (cond-> allowed-hosts
+ origin (conj origin))))
+
(defn- repo-status []
(let [url (data-app.sync/repo-url)]
{:configured (some? url)
@@ -390,10 +414,12 @@
(if (and bundle (pos? (alength bundle)))
(respond {:status 200
:headers (-> bundle-response-headers
- ;; JSON array of origins the sandboxed bundle may fetch/XHR; the
- ;; iframe reads this to configure its Near-Membrane fetch allowlist.
+ ;; JSON array of origins the sandboxed bundle may fetch/XHR. Include
+ ;; the configured product analytics collector so SDK analytics work
+ ;; without granting access to the collector for another environment.
+ ;; The iframe reads this to configure its Near-Membrane fetch allowlist.
(assoc "X-Metabase-Data-App-Allowed-Hosts"
- (json/encode (:allowed_hosts row)))
+ (json/encode (bundle-allowed-hosts (:allowed_hosts row))))
(cond-> etag (assoc "ETag" etag)))
:body (ByteArrayInputStream. bundle)})
(respond {:status 404
diff --git a/enterprise/backend/test/metabase_enterprise/data_apps/api_test.clj b/enterprise/backend/test/metabase_enterprise/data_apps/api_test.clj
index d78a438eb01..d58b2c0bdd3 100644
--- a/enterprise/backend/test/metabase_enterprise/data_apps/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/data_apps/api_test.clj
@@ -13,6 +13,7 @@
[metabase.lib.metadata :as lib.metadata]
[metabase.permissions.core :as perms]
[metabase.test :as mt]
+ [metabase.util.json :as json]
[toucan2.core :as t2]))
(set! *warn-on-reflection* true)
@@ -737,15 +738,24 @@
:allowed_hosts ["https://api.example.com"])
;; publish the app so its bundle is served (crowberto, as a superuser, can read the collection)
(data-app.resources/ensure-resources! (t2/select-one :model/DataApp :name "demo"))
- (testing "the bundle response carries the app's allowed_hosts as a JSON header"
- (let [resp (mt/user-http-request-full-response :crowberto :get 200 "apps/demo/bundle")]
- (is (= "[\"https://api.example.com\"]"
- (get-in resp [:headers "X-Metabase-Data-App-Allowed-Hosts"])))))
- (testing "an app with no allowed_hosts still sends the header as an empty JSON array"
- (t2/update! :model/DataApp :name "demo" {:allowed_hosts []})
- (let [resp (mt/user-http-request-full-response :crowberto :get 200 "apps/demo/bundle")]
- (is (= "[]"
- (get-in resp [:headers "X-Metabase-Data-App-Allowed-Hosts"]))))))))
+ (testing "the bundle response normalizes the configured product analytics origin"
+ (mt/with-temporary-setting-values
+ [metaplow-url "HTTPS://product-analytics-ingestion.metabase.com/api/send"]
+ (let [resp (mt/user-http-request-full-response :crowberto :get 200 "apps/demo/bundle")]
+ (is (= (json/encode ["https://api.example.com"
+ "https://product-analytics-ingestion.metabase.com"])
+ (get-in resp [:headers "X-Metabase-Data-App-Allowed-Hosts"]))))))
+ (testing "the configured staging origin replaces the production origin"
+ (mt/with-temporary-setting-values [metaplow-url "https://product-analytics-ingestion.staging.metabase.com/api/send"]
+ (let [resp (mt/user-http-request-full-response :crowberto :get 200 "apps/demo/bundle")]
+ (is (= (json/encode ["https://api.example.com"
+ "https://product-analytics-ingestion.staging.metabase.com"])
+ (get-in resp [:headers "X-Metabase-Data-App-Allowed-Hosts"]))))))
+ (testing "an unset product analytics URL leaves the app's allowlist unchanged"
+ (mt/with-temporary-setting-values [metaplow-url nil]
+ (let [resp (mt/user-http-request-full-response :crowberto :get 200 "apps/demo/bundle")]
+ (is (= (json/encode ["https://api.example.com"])
+ (get-in resp [:headers "X-Metabase-Data-App-Allowed-Hosts"])))))))))
(deftest list-includes-allowed-hosts-test
(mt/with-premium-features #{:data-apps-preview}