Skip to content

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

Allow embedding sdk target for saml popups (#80479)

Phoomparin Mano582d66e9c117
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 61d5dc81305..31481e6de98 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -3768,6 +3768,7 @@
embedding
premium-features
request
+ server
session
settings
sso
diff --git a/enterprise/backend/src/metabase_enterprise/sso/integrations/saml.clj b/enterprise/backend/src/metabase_enterprise/sso/integrations/saml.clj
index c1a0b5f5ef9..c2272dbae54 100644
--- a/enterprise/backend/src/metabase_enterprise/sso/integrations/saml.clj
+++ b/enterprise/backend/src/metabase_enterprise/sso/integrations/saml.clj
@@ -39,9 +39,11 @@
[metabase-enterprise.sso.settings :as sso-settings]
[metabase.api.common :as api]
[metabase.auth-identity.core :as auth-identity]
+ [metabase.embedding.settings :as embed.settings]
[metabase.embedding.util :as embed.util]
[metabase.premium-features.core :as premium-features]
[metabase.request.core :as request]
+ [metabase.server.middleware.security :as mw.security]
[metabase.session.core :as session]
[metabase.system.core :as system]
[metabase.util :as u]
@@ -164,7 +166,7 @@
(relay-state/relay-state-key? relay-state)
(if-let [{:keys [continue_url origin embedding]} (relay-state/find-unexpired relay-state)]
(if embedding
- {:mode :embedding, :continue-url continue_url, :origin (or origin "*"), :relay-key relay-state}
+ {:mode :embedding, :continue-url continue_url, :origin origin, :relay-key relay-state}
{:mode :redirect, :continue-url continue_url, :relay-key relay-state})
{:mode :expired})
@@ -188,6 +190,14 @@
(when (= mode :expired)
(throw (ex-info (tru "Invalid authentication token")
{:status-code 401})))
+ ;; Validate the popup callback target against the configured SDK embedding origins.
+ (when (= mode :embedding)
+ (when-not (and origin
+ (mw.security/approved-origin? origin (embed.settings/embedding-app-origins-sdk)))
+ (log/warn "Rejecting SAML embedding login: popup origin is not an approved embedding origin"
+ {:origin origin})
+ (throw (ex-info (tru "This origin is not an approved embedding origin.")
+ {:status-code 400}))))
(sso-utils/check-sso-redirect continue-url)
(try
(let [redirect-url (or continue-url (system/site-url))
diff --git a/enterprise/backend/src/metabase_enterprise/sso/integrations/saml_utils.clj b/enterprise/backend/src/metabase_enterprise/sso/integrations/saml_utils.clj
index 3f2a53aea4b..3fba859ee3c 100644
--- a/enterprise/backend/src/metabase_enterprise/sso/integrations/saml_utils.clj
+++ b/enterprise/backend/src/metabase_enterprise/sso/integrations/saml_utils.clj
@@ -1,7 +1,8 @@
(ns metabase-enterprise.sso.integrations.saml-utils
"Functions for handling SAML authentication with the SDK side, including HTML popups"
(:require
- [java-time.api :as t])
+ [java-time.api :as t]
+ [metabase.util.json :as json])
(:import
(java.time Instant)))
@@ -15,7 +16,7 @@
<title>Authentication Complete</title>
<script nonce=\"" nonce "\">
const authData = {
- id: \"" key "\",
+ id: " (json/encode key) ",
exp: " (.getEpochSecond exp) ",
iat: " (.getEpochSecond iat) ",
status: \"ok\"
@@ -25,7 +26,7 @@
window.opener.postMessage({
type: 'SAML_AUTH_COMPLETE',
authData: authData
- }, '" origin "');
+ }, " (json/encode origin) ");
setTimeout(function() {
window.close();
@@ -35,7 +36,7 @@
document.body.innerHTML += '<p>Error: ' + e.message + '</p>';
}
} else {
- window.location.href = '" continue-url "';
+ window.location.href = " (json/encode continue-url) ";
}
</script>
</head>
diff --git a/enterprise/backend/test/metabase_enterprise/sso/integrations/saml_test.clj b/enterprise/backend/test/metabase_enterprise/sso/integrations/saml_test.clj
index aea87ae47e4..0ba2d35036e 100644
--- a/enterprise/backend/test/metabase_enterprise/sso/integrations/saml_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/sso/integrations/saml_test.clj
@@ -907,6 +907,21 @@
(testing "the relay-state entry is consumed (single use)"
(is (nil? (relay-state/find-unexpired relay-key)))))))))))
+(deftest saml-embedding-unapproved-origin-rejected-test
+ (testing "an embedding login rejects an unapproved popup origin"
+ (with-other-sso-types-disabled!
+ (with-saml-default-setup!
+ (do-with-some-validators-disabled!
+ (fn []
+ (let [relay-key (embedding-relay-state-key! "https://evil.example")
+ req-options (saml-post-request-options* (saml-test-response) relay-key)
+ response (client/client-real-response :post 400 "/auth/sso" req-options)]
+ (testing "the login is refused with 400"
+ (is (= 400 (:status response))))
+ (testing "the rejected response does not render the popup"
+ (is (not (str/includes? (str (:body response)) "SAML_AUTH_COMPLETE")))
+ (is (not (str/includes? (str (:body response)) "authData")))))))))))
+
(deftest saml-stored-key-survives-failed-login-test
(testing "a stored RelayState key is consumed only on success — a failed login keeps it so the user can retry"
(with-other-sso-types-disabled!
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.clj
index 510e403769c..38c46d09d7d 100644
--- a/enterprise/backend/test/metabase_enterprise/sso/integrations/saml_utils_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/sso/integrations/saml_utils_test.clj
@@ -2,7 +2,8 @@
(:require
[clojure.string :as str]
[clojure.test :refer :all]
- [metabase-enterprise.sso.integrations.saml-utils :as saml-utils]))
+ [metabase-enterprise.sso.integrations.saml-utils :as saml-utils]
+ [metabase.util.json :as json]))
(set! *warn-on-reflection* true)
@@ -17,3 +18,19 @@
(is (= "text/html" (get-in response [:headers "Content-Type"])))
(is (str/includes? (:body response)
(str "<script nonce=\"" nonce "\">"))))))
+
+(deftest ^:parallel popup-values-are-json-encoded-test
+ (testing "the popup script uses JSON-encoded values"
+ (let [origin-with-apostrophe "https://app.example/it's"
+ body (:body (saml-utils/create-token-response {:key "session-key"}
+ origin-with-apostrophe
+ "https://app.example/dest"
+ "noncevalue1"))]
+ (testing "the postMessage target is JSON-encoded"
+ (is (not (str/includes? body "}, '"))
+ "postMessage target must not use single quotes")
+ (is (str/includes? body (str "}, " (json/encode origin-with-apostrophe) ")"))
+ "postMessage target must be JSON-encoded"))
+ (testing "the session key is JSON-encoded"
+ (is (str/includes? body (str "id: " (json/encode "session-key")))
+ "session key must be JSON-encoded")))))
diff --git a/enterprise/backend/test/metabase_enterprise/sso/test_setup.clj b/enterprise/backend/test/metabase_enterprise/sso/test_setup.clj
index 96380d93b0d..a653319a236 100644
--- a/enterprise/backend/test/metabase_enterprise/sso/test_setup.clj
+++ b/enterprise/backend/test/metabase_enterprise/sso/test_setup.clj
@@ -47,6 +47,7 @@
saml-keystore-path nil
saml-keystore-password nil
saml-keystore-alias nil
+ embedding-app-origins-sdk "https://app.example.com"
site-url "http://localhost:3000"]
(mt/with-premium-features current-features
(f))))))