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
Add CSP nonce to SAML login popup for embedding (#73861)
Rodrigo López Dato31725857d82b
diff --git a/enterprise/backend/src/metabase_enterprise/sso/integrations/saml.clj b/enterprise/backend/src/metabase_enterprise/sso/integrations/saml.cljindex 8108eea1aaf..431ed1f294f 100644--- a/enterprise/backend/src/metabase_enterprise/sso/integrations/saml.clj+++ b/enterprise/backend/src/metabase_enterprise/sso/integrations/saml.clj@@ -191,7 +191,7 @@;; Login succeeded(:success? login-result)(if token-value- (saml-utils/create-token-response (:session login-result) origin clean-continue-url)+ (saml-utils/create-token-response (:session login-result) origin clean-continue-url (:nonce request))(request/set-session-cookies request(response/redirect (:redirect-url login-result))(:session login-result)diff --git a/enterprise/backend/src/metabase_enterprise/sso/integrations/saml_utils.clj b/enterprise/backend/src/metabase_enterprise/sso/integrations/saml_utils.cljindex 451f479fe58..3f2a53aea4b 100644--- a/enterprise/backend/src/metabase_enterprise/sso/integrations/saml_utils.clj+++ b/enterprise/backend/src/metabase_enterprise/sso/integrations/saml_utils.clj@@ -8,12 +8,12 @@(set! *warn-on-reflection* true)(defn- generate-saml-html-popup- [key ^Instant exp ^Instant iat origin continue-url]+ [key ^Instant exp ^Instant iat origin continue-url nonce](str "<!DOCTYPE html><html><head><title>Authentication Complete</title>- <script>+ <script nonce=\"" nonce "\">const authData = {id: \"" key "\",exp: " (.getEpochSecond exp) ",@@ -49,9 +49,9 @@(defn create-token-response"Create a token response with HTML and JavaScript to post the auth message"- [session origin continue-url]+ [session origin continue-url nonce](let [current-time (t/instant)expiration-time (t/plus current-time (t/seconds 86400))]{:status 200:headers {"Content-Type" "text/html"}- :body (generate-saml-html-popup (:key session) expiration-time current-time origin continue-url)}))+ :body (generate-saml-html-popup (:key session) expiration-time current-time origin continue-url nonce)}))diff --git a/enterprise/backend/test/metabase_enterprise/sso/integrations/saml_test.clj b/enterprise/backend/test/metabase_enterprise/sso/integrations/saml_test.cljindex 9d39073c0f9..047f873e6d0 100644--- a/enterprise/backend/test/metabase_enterprise/sso/integrations/saml_test.clj+++ b/enterprise/backend/test/metabase_enterprise/sso/integrations/saml_test.clj@@ -285,6 +285,26 @@:form-params {:SAMLResponse saml-response:RelayState (u/encode-base64 relay-state)}}})+(defn- extract-csp-script-nonce+ "Pull the `'nonce-<value>'` out of the Content-Security-Policy `script-src` directive."+ [csp-header]+ (some->> csp-header+ (re-find #"script-src[^;]*'nonce-([^']+)'")+ second))++(defn- extract-script-tag-nonce+ "Pull the nonce attribute out of the first `<script nonce=\"...\">` tag in the popup body."+ [body]+ (some->> body+ (re-find #"<script nonce=\"([^\"]+)\">")+ second))++(defn- token-relay-state+ "Build a RelayState whose decoded continue URL carries a valid SDK token + origin."+ [continue-base origin]+ (let [token (token-utils/generate-token)]+ (str continue-base "?token=" token "&origin=" (codec/url-encode origin))))+(defn- some-saml-attributes [user-nickname]{"http://schemas.auth0.com/identities/default/provider" "auth0""http://schemas.auth0.com/nickname" user-nickname@@ -1097,3 +1117,45 @@default-redirect-uri)response (client/client-real-response :post 401 "/auth/sso" req-options)](is (not (successful-login? response))))))))))))))++(deftest popup-csp-nonce-matches-header-test+ (testing "When `/auth/sso` returns the SAML popup HTML, the `<script nonce>` attribute must byte-equal the `'nonce-<X>'` in the response's Content-Security-Policy `script-src` directive."+ (with-other-sso-types-disabled!+ (with-saml-default-setup!+ (do-with-some-validators-disabled!+ (fn []+ (let [relay-state (token-relay-state default-redirect-uri (system/site-url))+ req-options (saml-post-request-options (saml-test-response) relay-state)+ response (client/client-real-response :post 200 "/auth/sso" req-options)+ header-nonce (extract-csp-script-nonce (get-in response [:headers "Content-Security-Policy"]))+ body-nonce (extract-script-tag-nonce (:body response))]+ (testing "CSP header contains a script-src nonce"+ (is (seq header-nonce)))+ (testing "Popup body contains a <script nonce=\"...\"> tag"+ (is (seq body-nonce)))+ (testing "Header nonce matches body script nonce"+ (is (= header-nonce body-nonce))))))))))++(deftest popup-csp-nonce-per-request-unique-test+ (testing "Each `/auth/sso` popup response carries a freshly generated nonce (no static reuse)."+ (with-other-sso-types-disabled!+ (with-saml-default-setup!+ (do-with-some-validators-disabled!+ (fn []+ (let [response-1 (client/client-real-response+ :post 200 "/auth/sso"+ (saml-post-request-options+ (saml-test-response)+ (token-relay-state default-redirect-uri (system/site-url))))+ response-2 (client/client-real-response+ :post 200 "/auth/sso"+ (saml-post-request-options+ (saml-test-response)+ (token-relay-state default-redirect-uri (system/site-url))))+ nonce-1 (extract-script-tag-nonce (:body response-1))+ nonce-2 (extract-script-tag-nonce (:body response-2))]+ (testing "Both responses have nonces"+ (is (seq nonce-1))+ (is (seq nonce-2)))+ (testing "Nonces differ between requests"+ (is (not= nonce-1 nonce-2))))))))))diff --git a/enterprise/backend/test/metabase_enterprise/sso/integrations/saml_utils_test.clj b/enterprise/backend/test/metabase_enterprise/sso/integrations/saml_utils_test.cljnew file mode 100644index 00000000000..510e403769c--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/sso/integrations/saml_utils_test.clj@@ -0,0 +1,19 @@+(ns metabase-enterprise.sso.integrations.saml-utils-test+ (:require+ [clojure.string :as str]+ [clojure.test :refer :all]+ [metabase-enterprise.sso.integrations.saml-utils :as saml-utils]))++(set! *warn-on-reflection* true)++(deftest ^:parallel create-token-response-includes-nonce-attribute-test+ (testing "create-token-response emits <script nonce=\"...\"> with the provided nonce"+ (let [nonce "abc123XYZ09"+ response (saml-utils/create-token-response {:key "session-key"}+ "https://app.example"+ "https://app.example/dest"+ nonce)]+ (is (= 200 (:status response)))+ (is (= "text/html" (get-in response [:headers "Content-Type"])))+ (is (str/includes? (:body response)+ (str "<script nonce=\"" nonce "\">"))))))