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
Send the SAML SessionIndex in single logout requests (#82441)
bryan1b57292d096c
diff --git a/deps.edn b/deps.ednindex 304893ff761..cbd0816d465 100644--- a/deps.edn+++ b/deps.edn@@ -93,7 +93,7 @@:exclusions [org.clojure/data.avl]}medley/medley {:mvn/version "1.4.0"} ; lightweight lib of useful functionsmetabase/connection-pool {:mvn/version "1.2.0"} ; simple wrapper around C3P0. JDBC connection pools- metabase/saml20-clj {:mvn/version "4.2.1"+ metabase/saml20-clj {:mvn/version "4.3.0":exclusions [; EE SAML integration TODO: bump version when we release the libraryorg.bouncycastle/bcpkix-jdk15onorg.bouncycastle/bcprov-jdk15ondiff --git a/enterprise/backend/src/metabase_enterprise/sso/api/sso.clj b/enterprise/backend/src/metabase_enterprise/sso/api/sso.cljindex 74e48ddda8b..577821928dd 100644--- a/enterprise/backend/src/metabase_enterprise/sso/api/sso.clj+++ b/enterprise/backend/src/metabase_enterprise/sso/api/sso.clj@@ -102,7 +102,7 @@[_route-params _query-params _body {cookies :cookies, :as _request}](let [metabase-session-key (get-in cookies [request/metabase-session-cookie :value])metabase-session-key-hashed (session/hash-session-key metabase-session-key)- {:keys [email sso_source]}+ {:keys [email sso_source saml_session_index saml_name_id saml_name_id_format]}(sso.db/session-user-email-and-source metabase-session-key-hashed)];; If a user doesn't have SLO setup on their IdP,;; they will never hit "/handle_slo" so we must delete the session here:@@ -115,7 +115,15 @@:credential (metabase-enterprise.sso.integrations.saml/sp-cert-keystore-details):idp-url (sso-settings/saml-identity-provider-slo-uri):issuer (sso-settings/saml-application-name)- :user-email email+ ;; Name the subject the way the IdP does. Its NameID is not necessarily the user's email+ ;; - Auth0 sends an opaque `auth0|<id>` - and a LogoutRequest naming an identifier the IdP+ ;; never issued matches no session. Fall back to email for sessions predating this column.+ :user-email (or saml_name_id email)+ :name-id-format saml_name_id_format+ ;; Identifies which session to end. IdPs that map several sessions to one subject (Auth0)+ ;; reject a LogoutRequest without it. Omitted when we have none, which keeps the request+ ;; unchanged for IdPs that never sent one.+ :session-index saml_session_index:relay-state (u/encode-base64(str (system/site-url) metabase-slo-redirect-url))))}))diff --git a/enterprise/backend/src/metabase_enterprise/sso/db.clj b/enterprise/backend/src/metabase_enterprise/sso/db.cljindex 3b1b516cb4e..8765e862fab 100644--- a/enterprise/backend/src/metabase_enterprise/sso/db.clj+++ b/enterprise/backend/src/metabase_enterprise/sso/db.clj@@ -7,9 +7,14 @@[toucan2.core :as t2]))(mu/defn session-user-email-and-source- "The email and SSO source of the User owning the Session with `session-key-hashed`, or nil."+ "The email and SSO source of the Session with `session-key-hashed`, plus the identifiers the IdP+ used for it, or nil.++ The `saml_*` columns are NULL for non-SAML sessions and for SAML sessions created before we+ started recording them."[session-key-hashed :- :string]- (t2/query-one {:select [:u.email :u.sso_source]+ (t2/query-one {:select [:u.email :u.sso_source :session.saml_session_index+ :session.saml_name_id :session.saml_name_id_format]:from [[:core_user :u]]:join [[:core_session :session] [:= :u.id :session.user_id]]:where [:= :key_hashed session-key-hashed]}))diff --git a/enterprise/backend/src/metabase_enterprise/sso/providers/saml.clj b/enterprise/backend/src/metabase_enterprise/sso/providers/saml.cljindex dae81d9f6d9..3d4c2273f68 100644--- a/enterprise/backend/src/metabase_enterprise/sso/providers/saml.clj+++ b/enterprise/backend/src/metabase_enterprise/sso/providers/saml.clj@@ -64,6 +64,20 @@{:status-code 401})))attrs))+(defn- saml-response->session-identity+ "What the IdP used to identify this login: its `SessionIndex`, and the `NameID` (with `Format`)+ naming the subject.++ Recorded on the session because single logout must address the session and subject the IdP knows.+ The IdP's NameID is not necessarily the user's email - Auth0, for instance, sends an opaque+ `auth0|<id>` - so a LogoutRequest built from the email names a subject the IdP never issued.+ Any of these may be nil if the IdP did not send them."+ [saml-response]+ (let [{:keys [session-index name-id]} (first (saml/assertions saml-response))]+ {:session-index session-index+ :name-id (:value name-id)+ :name-id-format (:format name-id)}))+(methodical/defmethod auth-identity/authenticate :provider/saml[_provider {:keys [redirect-url relay-state] :as request}](cond@@ -116,6 +130,7 @@:issuer]:issuer (sso-settings/saml-identity-provider-issuer)})attrs (saml-response->attributes validated-response)+ session-identity (saml-response->session-identity validated-response)email (get attrs (sso-settings/saml-attribute-email))first-name (get attrs (sso-settings/saml-attribute-firstname))last-name (get attrs (sso-settings/saml-attribute-lastname))@@ -137,8 +152,11 @@:sso_source :saml:login_attributes user-attributes}:tenant-slug tenant-slug- :saml-data {:group-names groups- :user-attributes user-attributes}+ :saml-data (merge {:group-names groups+ :user-attributes user-attributes}+ ;; Kept so the session row can record them: single logout sends them+ ;; back to name the session and subject the IdP should end.+ session-identity):provider-id email})(catch clojure.lang.ExceptionInfo e(log/errorf "SAML authentication failed: %s" (.getMessage e))diff --git a/enterprise/backend/test/metabase_enterprise/sso/api/sso_test.clj b/enterprise/backend/test/metabase_enterprise/sso/api/sso_test.cljindex 64e35b9317e..842487162ed 100644--- a/enterprise/backend/test/metabase_enterprise/sso/api/sso_test.clj+++ b/enterprise/backend/test/metabase_enterprise/sso/api/sso_test.clj@@ -10,7 +10,9 @@[metabase.test :as mt][metabase.test.fixtures :as fixtures][metabase.test.http-client :as client]- [metabase.util.random :as u.random]))+ [metabase.util :as u]+ [metabase.util.random :as u.random]+ [ring.util.codec :as codec]))(set! *warn-on-reflection* true)@@ -56,6 +58,50 @@"%2FYw11yAqadb8%3D")(:saml-logout-url response))))))))))))))+(deftest saml-logout-session-index+ (testing "the SessionIndex recorded at login is sent in the LogoutRequest"+ ;; Auth0 maps several sessions to one email and rejects a LogoutRequest that does not name+ ;; which session to end (#77837).+ (mt/with-premium-features #{:sso-saml}+ (mt/with-temporary-setting-values [saml-enabled true+ saml-identity-provider-uri "http://idp.example.com/login"+ saml-identity-provider-certificate (slurp "test_resources/sso/auth0-public-idp.cert")+ saml-keystore-path "test_resources/keystore.jks"+ saml-keystore-password "123456"+ saml-keystore-alias "sp"+ site-url "http://localhost:3000"+ saml-slo-enabled true+ saml-identity-provider-slo-uri "http://idp.example.com/logout"]+ (binding [client/*url-prefix* ""]+ (letfn [(logout-url [session-index]+ (let [session-key (session/generate-session-key)]+ (mt/with-temp [:model/User user {:email "[email protected]" :sso_source "saml"}+ :model/Session _ {:user_id (:id user)+ :id (session/generate-session-id)+ :key_hashed (session/hash-session-key session-key)+ :saml_session_index session-index}]+ (-> (client/client :post "/auth/sso/logout"+ (assoc-in {} [:request-options :cookies+ request/metabase-session-cookie :value]+ session-key))+ :saml-logout-url))))+ (saml-request [url]+ ;; The LogoutRequest is deflated + base64'd into the SAMLRequest query param.+ (let [param (second (re-find #"SAMLRequest=([^&]+)" url))+ bytes (-> param codec/url-decode u/decode-base64-to-bytes)]+ (with-open [in (java.util.zip.InflaterInputStream.+ (java.io.ByteArrayInputStream. bytes)+ (java.util.zip.Inflater. true))]+ (slurp in))))]+ (testing "a session with a SessionIndex names it in the request"+ (let [xml (saml-request (logout-url "_session-index-from-idp"))]+ (is (str/includes? xml "<saml2p:SessionIndex>_session-index-from-idp</saml2p:SessionIndex>"))))+ (testing "a session without one omits the element entirely"+ ;; A fabricated or fixed SessionIndex broke Azure AD before (#41600), so IdPs that+ ;; never send one must keep seeing exactly the request they saw before.+ (let [xml (saml-request (logout-url nil))]+ (is (not (str/includes? xml "SessionIndex")))))))))))+(def ^:private default-jwt-secret (u.random/secure-hex 32))(defn- with-jwt-settings! [f]diff --git a/enterprise/backend/test/metabase_enterprise/sso/integrations/saml_test.clj b/enterprise/backend/test/metabase_enterprise/sso/integrations/saml_test.cljindex 73c049f46f8..1670d00ad94 100644--- a/enterprise/backend/test/metabase_enterprise/sso/integrations/saml_test.clj+++ b/enterprise/backend/test/metabase_enterprise/sso/integrations/saml_test.clj@@ -348,6 +348,25 @@(is (= (some-saml-attributes "rasta")(saml-login-attributes "[email protected]")))))))))))+(deftest login-records-session-index-test+ (testing "the SessionIndex from the IdP's assertion is stored on the session it creates"+ ;; Single logout sends this back so the IdP knows which session to end (#77837).+ (with-other-sso-types-disabled!+ (with-saml-default-setup!+ (do-with-some-validators-disabled!+ (fn []+ (let [req-options (saml-post-request-options (saml-test-response)+ default-redirect-uri)+ response (client/client-real-response :post 302 "/auth/sso" req-options)]+ (is (successful-login? response))+ ;; `rasta` may already own sessions from other fixtures, so look up the one this+ ;; login just created via its session cookie.+ (let [session-key (get-in response [:cookies request/metabase-session-cookie :value])]+ (is (= "_8vKjURdHz4jghbYbj48khvBkLaEeyEqc"+ (t2/select-one-fn :saml_session_index :model/Session+ :key_hashed (session/hash-session-key session-key)))+ "the SessionIndex in test_resources/saml-test-response.xml")))))))))+(deftest validate-signatures-test;; they were edited by hand I think, so the signatures are now incorrect (?)(testing "The sample responses should normally fail because the <Assertion> signatures don't match"diff --git a/resources/migrations/058_update_migrations.yaml b/resources/migrations/058_update_migrations.yamlindex fe2c297fdd9..0d35b65d4e3 100644--- a/resources/migrations/058_update_migrations.yaml+++ b/resources/migrations/058_update_migrations.yaml@@ -1635,6 +1635,63 @@ databaseChangeLog:- customChange:class: "metabase.app_db.custom_migrations.BackfillExampleDashboardIdValueWithAad"+ - changeSet:+ id: v58.2026-09-15T00:00:00+ author: Bryan Maass+ comment: Add saml_session_index to core_session+ changes:+ - addColumn:+ tableName: core_session+ columns:+ - column:+ name: saml_session_index+ type: varchar(254)+ remarks: SessionIndex from the SAML login assertion, sent back in the SLO LogoutRequest to identify this session. NULL for non-SAML sessions and for sessions created before this column existed.+ constraints:+ nullable: true+ rollback:+ - dropColumn:+ tableName: core_session+ columnName: saml_session_index++ - changeSet:+ id: v58.2026-09-15T00:00:01+ author: Bryan Maass+ comment: Add saml_name_id to core_session+ changes:+ - addColumn:+ tableName: core_session+ columns:+ - column:+ name: saml_name_id+ type: varchar(254)+ remarks: NameID from the SAML login assertion. Sent back in the SLO LogoutRequest, since the IdP identifies the subject by this and not by email. NULL for non-SAML sessions.+ constraints:+ nullable: true+ rollback:+ - dropColumn:+ tableName: core_session+ columnName: saml_name_id++ - changeSet:+ id: v58.2026-09-15T00:00:02+ author: Bryan Maass+ comment: Add saml_name_id_format to core_session+ changes:+ - addColumn:+ tableName: core_session+ columns:+ - column:+ name: saml_name_id_format+ type: varchar(254)+ remarks: Format attribute of the SAML NameID. Echoed back in the LogoutRequest because some IdPs compare it literally. NULL when the IdP sent none.+ constraints:+ nullable: true+ rollback:+ - dropColumn:+ tableName: core_session+ columnName: saml_name_id_format+# >>>>>>>>>> DO NOT ADD NEW MIGRATIONS BELOW THIS LINE! ADD THEM ABOVE <<<<<<<<<<########################################################################################################################diff --git a/src/metabase/auth_identity/db.clj b/src/metabase/auth_identity/db.cljindex fb6294e3f0d..efb6cb24b1e 100644--- a/src/metabase/auth_identity/db.clj+++ b/src/metabase/auth_identity/db.clj@@ -119,13 +119,20 @@(t2/insert-returning-instance! [:model/User :id :last_login :is_active :tenant_id] row))(mu/defn insert-session!- "Insert a Session and return the inserted instance."+ "Insert a Session and return the inserted instance.++ `opts` carries provider-specific extras. The `:saml-*` keys come from a SAML login assertion and+ are stored so single logout can name the session and subject the IdP knows."[session-id :- :stringuser-id :- ::lib.schema.id/userauth-identity-id :- [:maybe ms/PositiveInt]session-key :- :stringexpires-at :- [:maybe ms/TemporalInstant]- mfa-auth-identity-id :- [:maybe ms/PositiveInt]]+ mfa-auth-identity-id :- [:maybe ms/PositiveInt]+ opts :- [:map {:closed true}+ [:saml-session-index {:optional true} [:maybe :string]]+ [:saml-name-id {:optional true} [:maybe :string]]+ [:saml-name-id-format {:optional true} [:maybe :string]]]](t2/insert-returning-instance! :model/Session;; Without setting the ID here we can't return an instance on MySQL:id session-id@@ -133,4 +140,7 @@:auth_identity_id auth-identity-id:session_key session-key:expires_at expires-at- :mfa_auth_identity_id mfa-auth-identity-id))+ :mfa_auth_identity_id mfa-auth-identity-id+ :saml_session_index (:saml-session-index opts)+ :saml_name_id (:saml-name-id opts)+ :saml_name_id_format (:saml-name-id-format opts)))diff --git a/src/metabase/auth_identity/provider.clj b/src/metabase/auth_identity/provider.cljindex f3bc27d59f5..7ac81d5df6c 100644--- a/src/metabase/auth_identity/provider.clj+++ b/src/metabase/auth_identity/provider.clj@@ -373,8 +373,14 @@(assoc request :success? false:error disabled-account-snippet:message disabled-account-message)- (let [{:keys [user device-info]} request- session (auth-session/create-session-with-auth-tracking! user device-info provider)]+ (let [{:keys [user device-info saml-data]} request+ session (auth-session/create-session-with-auth-tracking!+ user device-info provider nil+ ;; SAML logins carry the IdP's own identifiers; single logout needs them to+ ;; name the session and subject to end. Other providers have none and store NULL.+ {:saml-session-index (:session-index saml-data)+ :saml-name-id (:name-id saml-data)+ :saml-name-id-format (:name-id-format saml-data)})](assoc request :session session))))(methodical/defmethod login! ::providerdiff --git a/src/metabase/auth_identity/session.clj b/src/metabase/auth_identity/session.cljindex ee24f349441..b58e0b4ec4b 100644--- a/src/metabase/auth_identity/session.clj+++ b/src/metabase/auth_identity/session.clj@@ -20,11 +20,17 @@The session row and its `login_history` row are written in one transaction: `login_history.session_id` has a foreignkey to `core_session`, so a concurrent session delete (e.g. a password change, which invalidates the user's sessions)- must not be able to remove the freshly-created session between the two inserts and break that reference."+ must not be able to remove the freshly-created session between the two inserts and break that reference.++ `opts` carries provider-specific extras for the session row; see [[auth-identity.db/insert-session!]]."([user :- ::users.schema/userdevice-info :- [:maybe request/DeviceInfo]provider :- :keyword- mfa-auth-identity-id :- [:maybe ms/PositiveInt]]+ mfa-auth-identity-id :- [:maybe ms/PositiveInt]+ opts :- [:map {:closed true}+ [:saml-session-index {:optional true} [:maybe :string]]+ [:saml-name-id {:optional true} [:maybe :string]]+ [:saml-name-id-format {:optional true} [:maybe :string]]]](let [user-id (u/the-id user)provider-str (name provider)auth-identity (auth-identity.db/auth-identity-expiry user-id provider-str)@@ -33,7 +39,7 @@session-id (string/random-string 12)session (t2/with-transaction [_](u/prog1 (auth-identity.db/insert-session! session-id user-id auth-identity-id session-key- (:expires_at auth-identity) mfa-auth-identity-id)+ (:expires_at auth-identity) mfa-auth-identity-id opts)(when provider-str(log/debugf "Updating last_used_at for user %s with provider %s" user-id provider-str)(auth-identity.db/touch-auth-identity! auth-identity-id))@@ -42,5 +48,12 @@(assoc session:key session-key:type (if (some-> (request/current-request) request/embedded?) :full-app-embed :normal))))++ ([user :- ::users.schema/user+ device-info :- [:maybe request/DeviceInfo]+ provider :- :keyword+ mfa-auth-identity-id :- [:maybe ms/PositiveInt]]+ (create-session-with-auth-tracking! user device-info provider mfa-auth-identity-id {}))+([user :- ::users.schema/user device-info :- [:maybe request/DeviceInfo] provider :- :keyword]- (create-session-with-auth-tracking! user device-info provider nil)))+ (create-session-with-auth-tracking! user device-info provider nil {})))diff --git a/src/metabase/session/schema.clj b/src/metabase/session/schema.cljindex 2ab1ed26b81..27e94c3c359 100644--- a/src/metabase/session/schema.clj+++ b/src/metabase/session/schema.clj@@ -25,10 +25,13 @@(mr/def ::session.update"What an update (or insert) of a Session accepts: every column of `:core_session` except `id`, all optional."[:map {:closed true}- [:user_id {:optional true} [:maybe ::lib.schema.id/user]]- [:created_at {:optional true} [:maybe ms/TemporalInstant]]- [:anti_csrf_token {:optional true} [:maybe :string]]- [:key_hashed {:optional true} [:maybe :string]]- [:auth_identity_id {:optional true} [:maybe ms/PositiveInt]]- [:expires_at {:optional true} [:maybe ms/TemporalInstant]]- [:last_active_at {:optional true} [:maybe ms/TemporalInstant]]])+ [:user_id {:optional true} [:maybe ::lib.schema.id/user]]+ [:created_at {:optional true} [:maybe ms/TemporalInstant]]+ [:anti_csrf_token {:optional true} [:maybe :string]]+ [:key_hashed {:optional true} [:maybe :string]]+ [:auth_identity_id {:optional true} [:maybe ms/PositiveInt]]+ [:expires_at {:optional true} [:maybe ms/TemporalInstant]]+ [:last_active_at {:optional true} [:maybe ms/TemporalInstant]]+ [:saml_session_index {:optional true} [:maybe :string]]+ [:saml_name_id {:optional true} [:maybe :string]]+ [:saml_name_id_format {:optional true} [:maybe :string]]])