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
(ns metabase-enterprise.data-apps.csp-test
(:require
[clojure.string :as str]
[clojure.test :refer :all]
[metabase.config.core :as config]
[metabase.server.middleware.security :as mw.security]
[metabase.test :as mt]
[toucan2.core :as t2]))
(set! *warn-on-reflection* true)
(defn- connect-src-for!
"The `connect-src` directive of the CSP that the security middleware emits for
a signed-in request to `uri` (a signed-out one never carries an app's hosts)."
([uri]
(connect-src-for! uri {:metabase-user-id (mt/user->id :rasta)}))
([uri request]
(with-redefs [config/is-dev? false]
(let [handler (mw.security/add-security-headers
(fn [_req respond _raise] (respond {:status 200 :headers {} :body "ok"})))
response (handler (merge {:uri uri :headers {}} request) identity identity)
csp (get-in response [:headers "Content-Security-Policy"])]
(->> (str/split csp #";\s*")
(filter #(str/starts-with? % "connect-src "))
first)))))
(deftest data-app-connect-src-allowed-hosts-test
(mt/with-premium-features #{:data-apps}
(mt/with-temp [:model/DataApp {app-name :name} {:allowed_hosts ["https://api.example.com" "https://*.acme.com"]}]
(testing "the data-app iframe document's connect-src includes the app's allowed_hosts (and keeps 'self')"
(let [cs (connect-src-for! (str "/embed/apps/" app-name))]
(is (str/includes? cs "https://api.example.com"))
(is (str/includes? cs "https://*.acme.com"))
(is (str/includes? cs "'self'"))))
(testing "a deeper sub-route of the same app gets them too"
(is (str/includes? (connect-src-for! (str "/embed/apps/" app-name "/sub/route"))
"https://api.example.com")))
(testing "the main app's connect-src does NOT include them"
(is (not (str/includes? (connect-src-for! "/") "https://api.example.com"))))
(testing "an unknown app contributes no hosts"
(is (not (str/includes? (connect-src-for! "/embed/apps/nope")
"api.example.com"))))
(testing "a signed-out visitor never gets them, even for a real app"
(is (not (str/includes? (connect-src-for! (str "/embed/apps/" app-name) {})
"api.example.com")))))))
(deftest data-app-connect-src-hosts-fn-test
(mt/with-temp [:model/DataApp {app-id :id app-name :name} {:allowed_hosts ["https://api.example.com"]}]
(testing "with the :data-apps feature the EE impl returns the app's allowed_hosts"
(mt/with-premium-features #{:data-apps}
(is (= ["https://api.example.com"] (mw.security/data-app-connect-src-hosts app-name)))
(testing "including a disabled app's, whose document only superusers reach"
(t2/update! :model/DataApp :id app-id {:enabled false})
(is (= ["https://api.example.com"] (mw.security/data-app-connect-src-hosts app-name))))))
(testing "without the feature it falls back to []"
(mt/with-premium-features #{}
(is (= [] (mw.security/data-app-connect-src-hosts app-name)))))))