Skip to content

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

test(mfa): auth-mechanism matrix + lockout-corner coverage (#77624)

bryan64588d02fbb1
diff --git a/enterprise/backend/src/metabase_enterprise/mfa/management.clj b/enterprise/backend/src/metabase_enterprise/mfa/management.clj
index 5b3f4206f1c..2f949b6f747 100644
--- a/enterprise/backend/src/metabase_enterprise/mfa/management.clj
+++ b/enterprise/backend/src/metabase_enterprise/mfa/management.clj
@@ -25,6 +25,7 @@
[metabase.sso.core :as sso]
[metabase.util.encryption :as encryption]
[metabase.util.i18n :refer [tru]]
+ [metabase.util.log :as log]
[metabase.util.malli.schema :as ms]
[metabase.util.password :as u.password]
[throttle.core :as throttle]
@@ -67,9 +68,15 @@
(t2/select-one-fn :credentials :model/AuthIdentity :user_id user-id :provider "password")]
(and password_hash (u.password/verify-password password password_salt password_hash)))
(when (sso/ldap-enabled)
- (when-let [user-email (t2/select-one-fn :email :model/User user-id)]
- (when-let [user-info (sso/find-user user-email)]
- (sso/verify-password user-info password))))))))
+ ;; an unreachable directory fails closed: re-auth is denied (the user retries when the
+ ;; directory is back; admin/remove needs no re-auth), never an unhandled 500
+ (try
+ (when-let [user-email (t2/select-one-fn :email :model/User user-id)]
+ (when-let [user-info (sso/find-user user-email)]
+ (sso/verify-password user-info password)))
+ (catch Exception e
+ (log/warn e "LDAP re-auth failed because the directory is unreachable")
+ false)))))))
;; Notification emails here are fire-and-log by construction: the messages/send-mfa-*-email!
;; senders route through email/send-message!, which catches and logs delivery failures — so an
@@ -164,14 +171,23 @@
;;; -------------------------------------------------- Admin --------------------------------------------------
(api.macros/defendpoint :post "/admin/remove" :- nil
- "Admin: remove a user's two-factor enrollment entirely — the lockout escape hatch for a lost
- authenticator with no recovery codes. Never feature-gated (a lapsed license must not make
- lockouts permanent). The affected user is notified by email. They re-enroll from scratch —
- there is nothing to \"reset\", the secret lives on their device."
+ "Admin: remove *another* user's two-factor enrollment entirely — the lockout escape hatch for a
+ lost authenticator with no recovery codes. Never feature-gated (a lapsed license must not make
+ lockouts permanent). The affected user is notified by email. They re-enroll from scratch — there
+ is nothing to \"reset\", the secret lives on their device.
+
+ Removing your *own* enrollment is deliberately refused here: this is a user-management endpoint
+ that takes no second factor (the target can't produce one — that's why an admin is removing it),
+ and the admin UI never collects a code. Self-removal goes through the normal Security page, which
+ re-auths with a fresh factor via `/disable`. Without this guard a hijacked admin session could
+ strip its own 2FA with only a cookie, turning transient access into a permanent password bypass."
[_route-params
_query-params
{user-id :user_id} :- [:map [:user_id ms/PositiveInt]]]
(api/check-superuser)
+ (when (= user-id api/*current-user-id*)
+ (throw (ex-info (tru "You cannot administratively remove your own two-factor authentication. Please use the normal removal method in your account settings.")
+ {:status-code 400})))
(when (enrollment/disable! user-id)
(let [user (t2/select-one :model/User :id user-id)]
(messages/send-mfa-removed-by-admin-email! (:email user))
diff --git a/enterprise/backend/test/metabase_enterprise/mfa/admin_test.clj b/enterprise/backend/test/metabase_enterprise/mfa/admin_test.clj
index c49d61bc6ee..717d65a9ba7 100644
--- a/enterprise/backend/test/metabase_enterprise/mfa/admin_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/mfa/admin_test.clj
@@ -5,7 +5,8 @@
[metabase-enterprise.mfa.enrollment :as enrollment]
[metabase-enterprise.mfa.totp :as totp]
[metabase.test :as mt]
- [metabase.test.fixtures :as fixtures]))
+ [metabase.test.fixtures :as fixtures]
+ [toucan2.core :as t2]))
(use-fixtures :once (fixtures/initialize :db :web-server :test-users))
@@ -22,6 +23,23 @@
(mt/user-http-request :crowberto :post 204 "ee/mfa/admin/remove" {:user_id user-id})
(is (nil? (enrollment/enrolled-method user-id)))))))
+(deftest admin-cannot-self-remove-test
+ ;; admin/remove is "manage OTHER users" — self-removal is refused so a hijacked admin cookie
+ ;; can't strip its own 2FA (this endpoint takes no factor; a stolen session has none to give).
+ ;; Removing your own enrollment goes through /disable, which re-auths with a fresh code.
+ (mt/with-temp [:model/AuthIdentity _ {:user_id (mt/user->id :crowberto)
+ :provider "totp"
+ :confirmed_at (t/instant)
+ :credentials {:secret (totp/generate-secret)}}]
+ (try
+ (testing "removing your own id is refused, enrollment intact"
+ (is (re-find #"cannot administratively remove your own"
+ (mt/user-http-request :crowberto :post 400 "ee/mfa/admin/remove"
+ {:user_id (mt/user->id :crowberto)})))
+ (is (= :totp (enrollment/enrolled-method (mt/user->id :crowberto)))))
+ (finally
+ (t2/delete! :model/AuthIdentity :user_id (mt/user->id :crowberto) :provider "totp")))))
+
(deftest admin-remove-is-audited-test
;; audit rows require the :audit-app feature at event time (premium-features/log-enabled?)
(mt/with-premium-features #{:audit-app}
diff --git a/enterprise/backend/test/metabase_enterprise/mfa/gate_test.clj b/enterprise/backend/test/metabase_enterprise/mfa/gate_test.clj
index 9022d422011..93e6a9e8a47 100644
--- a/enterprise/backend/test/metabase_enterprise/mfa/gate_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/mfa/gate_test.clj
@@ -68,6 +68,81 @@
(is (true? (:mfa/pending? gated)))
(is (nil? (:mfa/first-factor gated)))))))))))
+(deftest gate-sso-provider-matrix-test
+ (testing "every SSO provider is exempt for an enrolled user — the IdP owns MFA there"
+ (mt/with-premium-features #{:multi-factor-auth}
+ (mt/with-temporary-setting-values [mfa-enforcement :optional]
+ (with-enrolled-user!
+ (fn [user-id]
+ (let [result {:success? true :user {:id user-id}}]
+ (doseq [provider [:provider/google
+ :provider/saml
+ :provider/jwt
+ :provider/oidc
+ :provider/custom-oidc
+ :provider/slack-connect]]
+ (testing provider
+ (is (= result (auth-identity.provider/apply-mfa-gate provider result))))))))))))
+
+(deftest gate-provider-registry-exhaustiveness-test
+ ;; The gate's fall-through branch is exempt, so a provider nobody classified silently bypasses
+ ;; MFA. This test forces the decision: every provider in the auth-identity hierarchy must appear
+ ;; below. If you added a provider and landed here, decide its MFA posture — challenged providers
+ ;; also go in `metabase-enterprise.mfa.gate/challenged-providers`, session-suppressed ones in
+ ;; `session-suppressed-providers`; exemption is the default and needs only this map.
+ (let [classification {:provider/password :challenged
+ :provider/ldap :challenged
+ :provider/emailed-secret-password-reset :session-suppressed
+ ;; IdP owns MFA for SSO logins
+ :provider/google :exempt
+ :provider/saml :exempt
+ :provider/jwt :exempt
+ :provider/oidc :exempt
+ :provider/custom-oidc :exempt
+ :provider/slack-connect :exempt
+ ;; admin-granted, time-boxed, audited; staff can't hold the second factor
+ :provider/support-access-grant :exempt
+ ;; abstract parent (reset/verification/magic links) — a concrete login
+ ;; child must be classified here on its own when it ships
+ :provider/emailed-secret :exempt
+ ;; the second factor itself, never a first factor
+ :provider/totp :exempt}]
+ (testing "every registered provider has a deliberate MFA classification"
+ (is (= (set (keys classification))
+ (set (descendants :metabase.auth-identity.provider/provider)))))
+ (testing "the gate's behavior matches each classification for an enrolled user"
+ (mt/with-premium-features #{:multi-factor-auth}
+ (mt/with-temporary-setting-values [mfa-enforcement :optional]
+ (with-enrolled-user!
+ (fn [user-id]
+ (let [result {:success? true :user {:id user-id}}]
+ (doseq [[provider posture] classification]
+ (testing provider
+ (let [gated (auth-identity.provider/apply-mfa-gate provider result)]
+ (case posture
+ :challenged (is (= :mfa-required (:success? gated)))
+ :session-suppressed (do (is (true? (:success? gated)))
+ (is (true? (:mfa/pending? gated))))
+ :exempt (is (= result gated))))))))))))))
+
+(deftest gate-env-var-escape-hatch-test
+ ;; The last-admin lockout recovery: the only superuser loses authenticator + recovery codes, so
+ ;; nobody can log in to flip the setting. Restarting with MB_MFA_ENFORCEMENT=off must win over
+ ;; the :optional stored in the DB and let them in single-step.
+ (mt/with-premium-features #{:multi-factor-auth}
+ (mt/with-temporary-setting-values [mfa-enforcement :optional]
+ (with-enrolled-user!
+ (fn [user-id]
+ (let [result {:success? true :user {:id user-id}}]
+ (is (= :mfa-required
+ (:success? (auth-identity.provider/apply-mfa-gate :provider/password result)))
+ "sanity: challenged before the override")
+ (mt/with-temp-env-var-value! [mb-mfa-enforcement "off"]
+ (is (false? (mfa.settings/mfa-enabled?))
+ "the env var beats the DB value")
+ (is (= result (auth-identity.provider/apply-mfa-gate :provider/password result))
+ "the gate no-ops, so the locked-out admin can log in"))))))))
+
(deftest mfa-enforcement-write-path-test
(mt/with-temporary-setting-values [mfa-enforcement :off]
(testing "enabling (:optional) requires the :multi-factor-auth feature (setup is gated; enforcement is not)"
diff --git a/enterprise/backend/test/metabase_enterprise/mfa/ldap_flow_test.clj b/enterprise/backend/test/metabase_enterprise/mfa/ldap_flow_test.clj
index 788043caa40..930f68ed5b2 100644
--- a/enterprise/backend/test/metabase_enterprise/mfa/ldap_flow_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/mfa/ldap_flow_test.clj
@@ -5,9 +5,11 @@
(:require
[clojure.test :refer :all]
[java-time.api :as t]
+ [metabase-enterprise.mfa.enrollment :as enrollment]
[metabase-enterprise.mfa.management :as mfa.management]
[metabase-enterprise.mfa.totp :as totp]
[metabase.session.api :as api.session]
+ [metabase.sso.core :as sso]
[metabase.sso.ldap-test-util :as ldap.test]
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
@@ -73,6 +75,35 @@
(testing "the enroll endpoint's schema also refuses a blank password"
(mt/client session-key :post 400 "ee/mfa/enroll" {:password ""})))))))
+(deftest ldap-directory-unreachable-fails-closed-test
+ ;; Lockout-corner: the directory goes down while a user is enrolled. Re-auth (disable) must fail
+ ;; closed as a denied verification — not a 500 — and must not touch the enrollment. The recovery
+ ;; path that needs no directory, admin/remove, keeps working.
+ (mt/with-premium-features #{:multi-factor-auth}
+ (mt/with-temporary-setting-values [mfa-enforcement :optional]
+ (ldap.test/with-ldap-server!
+ (let [{session-key :id} (mt/client :post 200 "session" {:username sally-email
+ :password sally-directory-password})
+ user-id (t2/select-one-fn :id :model/User :email sally-email)
+ secret (totp/generate-secret)]
+ (try
+ (t2/insert! :model/AuthIdentity {:user_id user-id
+ :provider "totp"
+ :confirmed_at (t/instant)
+ :credentials {:secret secret}})
+ (with-redefs [sso/find-user (fn [& _] (throw (ex-info "connection refused" {})))]
+ (testing "enroll re-auth with the directory down is a 400, not a 500"
+ (is (=? {:errors {:password some?}}
+ (mt/client session-key :post 400 "ee/mfa/enroll"
+ {:password sally-directory-password}))))
+ (testing "the enrollment is untouched"
+ (is (= :totp (enrollment/enrolled-method user-id))))
+ (testing "admin/remove needs no directory and still recovers the account"
+ (mt/user-http-request :crowberto :post 204 "ee/mfa/admin/remove" {:user_id user-id})
+ (is (nil? (enrollment/enrolled-method user-id)))))
+ (finally
+ (t2/delete! :model/AuthIdentity :user_id user-id :provider "totp"))))))))
+
(deftest ldap-only-user-enrolls-with-directory-password-test
(mt/with-premium-features #{:multi-factor-auth}
(mt/with-temporary-setting-values [mfa-enforcement :optional]
diff --git a/enterprise/backend/test/metabase_enterprise/mfa/verification_test.clj b/enterprise/backend/test/metabase_enterprise/mfa/verification_test.clj
index 993e2bc013b..a9e4964d65d 100644
--- a/enterprise/backend/test/metabase_enterprise/mfa/verification_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/mfa/verification_test.clj
@@ -2,6 +2,7 @@
(:require
[clojure.test :refer :all]
[java-time.api :as t]
+ [metabase-enterprise.mfa.enrollment :as enrollment]
[metabase-enterprise.mfa.recovery-codes :as recovery-codes]
[metabase-enterprise.mfa.totp :as totp]
[metabase-enterprise.mfa.verification :as verification]
@@ -194,3 +195,25 @@
(t2/select-one-fn :credentials :model/AuthIdentity
:user_id user-id :provider "totp"))
"sanity: the old key can no longer read the row"))))))
+
+(deftest lost-encryption-key-fails-closed-test
+ (testing "an instance started with the wrong key: verify errors (no bypass, no session), and
+ admin removal — which never decrypts — still recovers the account"
+ (let [secret (totp/generate-secret)
+ k1 "0123456789abcdef-key-A"
+ k2 "totally-different-key-B!"]
+ (mt/with-temp [:model/User {user-id :id} {}]
+ (encryption-test/with-secret-key k1
+ (t2/insert! :model/AuthIdentity {:user_id user-id
+ :provider "totp"
+ :confirmed_at (t/instant)
+ :credentials {:secret secret}}))
+ (encryption-test/with-secret-key k2
+ (testing "verification fails closed — an error, never a false-positive login"
+ (is (thrown? Exception
+ (verification/verify-attempt! user-id (totp/generate-code secret) (fresh-jti)))))
+ (testing "the enrollment row survives the failed attempt"
+ (is (= 1 (t2/count :auth_identity :user_id user-id :provider "totp"))))
+ (testing "disable! deletes without decrypting, so the admin escape hatch works"
+ (is (true? (enrollment/disable! user-id)))
+ (is (zero? (t2/count :auth_identity :user_id user-id :provider "totp")))))))))