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
fix: validate ldap server email before save (#62616)
Edward Paget2219786d93dc
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 5438d7b3c31..4af7ab72418 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -1361,9 +1361,11 @@users{:team "UX West":api #{metabase.users.api+ metabase.users.coremetabase.users.initmetabase.users.models.user- metabase.users.models.user-parameter-value}+ metabase.users.models.user-parameter-value+ metabase.users.schema}:uses #{analyticsapiappearancediff --git a/enterprise/backend/src/metabase_enterprise/scim/v2/api.clj b/enterprise/backend/src/metabase_enterprise/scim/v2/api.cljindex 265794503ef..c931dc206fe 100644--- a/enterprise/backend/src/metabase_enterprise/scim/v2/api.clj+++ b/enterprise/backend/src/metabase_enterprise/scim/v2/api.clj@@ -10,6 +10,7 @@[metabase.models.interface :as mi][metabase.permissions.core :as perms][metabase.users.models.user :as user]+ [metabase.users.schema :as users.schema][metabase.util :as u][metabase.util.i18n :as i18n][metabase.util.malli :as mu]@@ -171,7 +172,7 @@:active (:is_active user):meta {:resourceType "User"}})-(mu/defn ^:private scim-user->mb :- user/NewUser+(mu/defn ^:private scim-user->mb :- users.schema/NewUser"Given a SCIM user, returns a Metabase user."[user](let [{email :userName name-obj :name locale :locale is-active? :active} userdiff --git a/enterprise/backend/src/metabase_enterprise/sso/integrations/ldap.clj b/enterprise/backend/src/metabase_enterprise/sso/integrations/ldap.cljindex 1aa4e340683..fdc9040f7b0 100644--- a/enterprise/backend/src/metabase_enterprise/sso/integrations/ldap.clj+++ b/enterprise/backend/src/metabase_enterprise/sso/integrations/ldap.clj@@ -5,7 +5,6 @@[metabase-enterprise.sso.settings :as ee.sso.settings][metabase.premium-features.core :refer [defenterprise-schema]][metabase.sso.core :as sso]- [metabase.users.models.user :as user][metabase.util :as u][metabase.util.malli.schema :as ms][toucan2.core :as t2])@@ -63,10 +62,10 @@{:keys [sync-groups?], :as settings} :- sso/LDAPSettings](let [user (or (attribute-synced-user user-info)(sso-utils/check-user-provisioning :ldap)- (-> (user/create-new-ldap-auth-user! {:first_name first-name- :last_name last-name- :email email- :login_attributes attributes})+ (-> (sso/create-new-ldap-auth-user! {:first_name first-name+ :last_name last-name+ :email email+ :login_attributes attributes})(assoc :is_active true)))](u/prog1 user(when sync-groups?diff --git a/src/metabase/sso/core.clj b/src/metabase/sso/core.cljindex 23f0c709b0c..477bde2394e 100644--- a/src/metabase/sso/core.clj+++ b/src/metabase/sso/core.clj@@ -14,6 +14,7 @@do-google-authgoogle-auth-create-new-user!][metabase.sso.ldap.default-implementation+ create-new-ldap-auth-user!LDAPSettingsldap-groups->mb-group-idsldap-search-result->user-info]diff --git a/src/metabase/sso/google.clj b/src/metabase/sso/google.cljindex 64f9c853d87..be7920d2115 100644--- a/src/metabase/sso/google.clj+++ b/src/metabase/sso/google.clj@@ -5,6 +5,7 @@[metabase.api.common :as api][metabase.sso.settings :as sso.settings][metabase.users.models.user :as user]+ [metabase.users.schema :as users.schema][metabase.util :as u][metabase.util.i18n :refer [deferred-tru tru]][metabase.util.json :as json]@@ -54,7 +55,7 @@(mu/defn google-auth-create-new-user!"Create a new Google Auth user."- [{:keys [email] :as new-user} :- user/NewUser]+ [{:keys [email] :as new-user} :- users.schema/NewUser](check-autocreate-user-allowed-for-email email);; this will just give the user a random password; they can go reset it if they ever change their mind and want to;; log in without Google Auth; this lets us keep the NOT NULL constraints on password / salt without having to makediff --git a/src/metabase/sso/ldap/default_implementation.clj b/src/metabase/sso/ldap/default_implementation.cljindex 0ea217093cf..ec90623d3d4 100644--- a/src/metabase/sso/ldap/default_implementation.clj+++ b/src/metabase/sso/ldap/default_implementation.clj@@ -5,7 +5,8 @@[clojure.string :as str][metabase.premium-features.core :refer [defenterprise-schema]][metabase.sso.common :as sso.common]- [metabase.users.models.user :as user]+ [metabase.users.core :as users.core]+ [metabase.users.schema :as users.schema][metabase.util :as u][metabase.util.log :as log][metabase.util.malli :as mu]@@ -127,6 +128,18 @@;;; --------------------------------------------- fetch-or-create-user! ----------------------------------------------+(mu/defn create-new-ldap-auth-user!+ "Convenience for creating a new user via LDAP. This account is considered active immediately; thus all active admins+ will receive an email right away."+ [{:keys [email] :as new-user} :- users.schema/NewUser]+ (when-not (u/email? email)+ (throw (ex-info "Invalid email supplied by LDAP server" {})))+ (users.core/insert-new-user!+ (-> new-user+ ;; We should not store LDAP passwords+ (dissoc :password)+ (assoc :sso_source "ldap"))))+(mu/defn ldap-groups->mb-group-ids :- [:set ms/PositiveInt]"Translate a set of a user's group DNs to a set of MB group IDs using the configured mappings."[ldap-groups :- [:maybe [:sequential ms/NonBlankString]]@@ -150,6 +163,7 @@metabase-enterprise.sso.integrations.ldap[{:keys [first-name last-name email groups]} :- UserInfo{:keys [sync-groups?], :as settings} :- LDAPSettings]+(let [user (t2/select-one [:model/User :id :last_login :first_name :last_name :is_active]:%lower.email (u/lower-case-en email))new-user (if user@@ -163,9 +177,9 @@(t2/update! :model/User (:id user) user-changes)(t2/select-one [:model/User :id :last_login :is_active] :id (:id user))) ; Reload updated useruser))- (-> (user/create-new-ldap-auth-user! {:first_name first-name- :last_name last-name- :email email})+ (-> (create-new-ldap-auth-user! {:first_name first-name+ :last_name last-name+ :email email})(assoc :is_active true)))](u/prog1 new-user(when sync-groups?diff --git a/src/metabase/users/api.clj b/src/metabase/users/api.cljindex c8dc5abe012..9e3b805b523 100644--- a/src/metabase/users/api.clj+++ b/src/metabase/users/api.clj@@ -17,6 +17,7 @@[metabase.session.models.session :as session][metabase.sso.core :as sso][metabase.users.models.user :as user]+ [metabase.users.schema :as users.schema][metabase.users.settings :as users.settings][metabase.util :as u][metabase.util.i18n :refer [tru]]@@ -392,7 +393,7 @@[:last_name {:optional true} [:maybe ms/NonBlankString]][:email ms/Email][:user_group_memberships {:optional true} [:maybe [:sequential ::user-group-membership]]]- [:login_attributes {:optional true} [:maybe user/LoginAttributes]]+ [:login_attributes {:optional true} [:maybe users.schema/LoginAttributes]][:source {:optional true, :default "admin"} [:maybe ms/NonBlankString]]]](invite-user body))@@ -436,7 +437,7 @@[:user_group_memberships {:optional true} [:maybe [:sequential ::user-group-membership]]][:is_superuser {:optional true} [:maybe :boolean]][:is_group_manager {:optional true} [:maybe :boolean]]- [:login_attributes {:optional true} [:maybe user/LoginAttributes]]+ [:login_attributes {:optional true} [:maybe users.schema/LoginAttributes]][:locale {:optional true} [:maybe ms/ValidLocale]]]](try(check-self-or-superuser id)diff --git a/src/metabase/users/core.clj b/src/metabase/users/core.cljnew file mode 100644index 00000000000..ae9c8e57cc2--- /dev/null+++ b/src/metabase/users/core.clj@@ -0,0 +1,11 @@+(ns metabase.users.core+ (:require+ [metabase.users.models.user]+ [potemkin :as p]))++(comment+ (metabase.users.models.user/keep-me))++(p/import-vars+ [metabase.users.models.user+ insert-new-user!])diff --git a/src/metabase/users/models/user.clj b/src/metabase/users/models/user.cljindex fae08e77b30..b68def1ac91 100644--- a/src/metabase/users/models/user.clj+++ b/src/metabase/users/models/user.clj@@ -13,9 +13,10 @@[metabase.settings.core :as setting][metabase.setup.core :as setup][metabase.system.core :as system]+ [metabase.users.schema :as users.schema][metabase.util :as u][metabase.util.honey-sql-2 :as h2x]- [metabase.util.i18n :as i18n :refer [deferred-tru trs tru]]+ [metabase.util.i18n :as i18n :refer [trs tru]][metabase.util.log :as log][metabase.util.malli :as mu][metabase.util.malli.schema :as ms]@@ -278,25 +279,6 @@(declare form-password-reset-url set-password-reset-token!)-(def LoginAttributes- "Login attributes, currently not collected for LDAP or Google Auth. Will ultimately be stored as JSON."- (mu/with-api-error-message- [:map-of ms/KeywordOrString :any]- (deferred-tru "login attribute keys must be a keyword or string")))--(def NewUser- "Required/optionals parameters needed to create a new user (for any backend)"- [:map- [:first_name {:optional true} [:maybe ms/NonBlankString]]- [:last_name {:optional true} [:maybe ms/NonBlankString]]- [:email ms/Email]- [:password {:optional true} [:maybe ms/NonBlankString]]- [:login_attributes {:optional true} [:maybe LoginAttributes]]- [:jwt_attributes {:optional true} [:maybe LoginAttributes]]- [:sso_source {:optional true} [:maybe ms/NonBlankString]]- [:locale {:optional true} [:maybe ms/KeywordOrString]]- [:type {:optional true} [:maybe ms/KeywordOrString]]])-(def ^:private Invitor"Map with info about the admin creating the user, used in the new user notification code"[:map@@ -305,7 +287,7 @@(mu/defn insert-new-user!"Creates a new user, defaulting the password when not provided"- [new-user :- NewUser]+ [new-user :- users.schema/NewUser](t2/insert-returning-instance! :model/User (update new-user :password #(or % (str (random-uuid))))))(defn serdes-synthesize-user!@@ -318,7 +300,7 @@"Convenience function for inviting a new `User` and sending them a welcome email.This function will create the user, which will trigger the built-in system eventnotification to send an invite via email."- [new-user :- NewUser invitor :- Invitor setup? :- :boolean]+ [new-user :- users.schema/NewUser invitor :- Invitor setup? :- :boolean];; create the new user(u/prog1 (insert-new-user! new-user);; TODO make sure the email being sent synchronously.@@ -334,23 +316,12 @@(mu/defn create-new-google-auth-user!"Convenience for creating a new user via Google Auth. This account is considered active immediately; thus all activeadmins will receive an email right away."- [new-user :- NewUser]+ [new-user :- users.schema/NewUser](u/prog1 (insert-new-user! (assoc new-user :sso_source "google"));; send an email to everyone including the site admin if that's set(when (setting/get :send-new-sso-user-admin-email?)((requiring-resolve 'metabase.channel.email.messages/send-user-joined-admin-notification-email!) <>, :google-auth? true))))-;;; TODO -- this should probably be moved into [[metabase.sso.ldap]]-(mu/defn create-new-ldap-auth-user!- "Convenience for creating a new user via LDAP. This account is considered active immediately; thus all active admins- will receive an email right away."- [new-user :- NewUser]- (insert-new-user!- (-> new-user- ;; We should not store LDAP passwords- (dissoc :password)- (assoc :sso_source "ldap"))))-;;; TODO -- it seems like maybe this should just be part of the [[pre-update]] logic whenever `:password` changes; then;;; we can remove this function altogether.(defn set-password!diff --git a/src/metabase/users/schema.clj b/src/metabase/users/schema.cljnew file mode 100644index 00000000000..4c44b1b081e--- /dev/null+++ b/src/metabase/users/schema.clj@@ -0,0 +1,24 @@+(ns metabase.users.schema+ (:require+ [metabase.util.i18n :refer [deferred-tru]]+ [metabase.util.malli :as mu]+ [metabase.util.malli.schema :as ms]))++(def LoginAttributes+ "Login attributes, currently not collected for LDAP or Google Auth. Will ultimately be stored as JSON."+ (mu/with-api-error-message+ [:map-of ms/KeywordOrString :any]+ (deferred-tru "login attribute keys must be a keyword or string")))++(def NewUser+ "Required/optionals parameters needed to create a new user (for any backend)"+ [:map+ [:first_name {:optional true} [:maybe ms/NonBlankString]]+ [:last_name {:optional true} [:maybe ms/NonBlankString]]+ [:email ms/Email]+ [:password {:optional true} [:maybe ms/NonBlankString]]+ [:login_attributes {:optional true} [:maybe LoginAttributes]]+ [:jwt_attributes {:optional true} [:maybe LoginAttributes]]+ [:sso_source {:optional true} [:maybe ms/NonBlankString]]+ [:locale {:optional true} [:maybe ms/KeywordOrString]]+ [:type {:optional true} [:maybe ms/KeywordOrString]]])diff --git a/test/metabase/sso/ldap/default_implementation_test.clj b/test/metabase/sso/ldap/default_implementation_test.cljnew file mode 100644index 00000000000..a7ebb5b7330--- /dev/null+++ b/test/metabase/sso/ldap/default_implementation_test.clj@@ -0,0 +1,41 @@+(ns metabase.sso.ldap.default-implementation-test+ (:require+ [clojure.test :refer :all]+ [metabase.sso.ldap.default-implementation :as sut]+ [metabase.test :as mt]+ [metabase.util.malli :as mu]+ [metabase.util.password :as u.password]+ [toucan2.core :as t2]))++(deftest ldap-user-passwords-test+ (testing (str "LDAP users should not persist their passwords. Check that if somehow we get passed an LDAP user "+ "password, it gets swapped with something random")+ (mt/with-model-cleanup [:model/User]+ (sut/create-new-ldap-auth-user! {:email "[email protected]"+ :first_name "Test"+ :last_name "SomeLdapStuff"+ :password "should be removed"})+ (let [{:keys [password password_salt]} (t2/select-one [:model/User :password :password_salt] :email "[email protected]")]+ (is (= false+ (u.password/verify-password "should be removed" password_salt password)))))))++(deftest ldap-sequential-login-attributes-test+ (testing "You should be able to create a new LDAP user if some `login_attributes` are vectors (#10291)"+ (mt/with-model-cleanup [:model/User]+ (sut/create-new-ldap-auth-user! {:email "[email protected]"+ :first_name "Test"+ :last_name "SomeLdapStuff"+ :login_attributes {:local_birds ["Steller's Jay" "Mountain Chickadee"]}})+ (is (= {"local_birds" ["Steller's Jay" "Mountain Chickadee"]}+ (t2/select-one-fn :login_attributes :model/User :email "[email protected]"))))))++(deftest ldap-bad-email-test+ (testing "If the ldap server supplies an invalid email it should throw"+ (mt/with-model-cleanup [:model/User]+ ;; disable the schema enforcement since it doesn't happen in the real world+ (mu/disable-enforcement+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Invalid email supplied by LDAP server"+ (sut/create-new-ldap-auth-user! {:email "ldaptest@metabase"+ :first_name "Test"+ :last_name "SomeLdapStuff"+ :login_attributes {:local_birds ["Steller's Jay" "Mountain Chickadee"]}})))))))diff --git a/test/metabase/users/models/user_test.clj b/test/metabase/users/models/user_test.cljindex 6bd80851377..4863991b4ea 100644--- a/test/metabase/users/models/user_test.clj+++ b/test/metabase/users/models/user_test.clj@@ -145,20 +145,6 @@(invite-user-accept-and-check-inboxes! :invitor default-invitor , :accept-invite? false)(is (seq (mt/regex-email-bodies #"/auth/login")))))))))-(deftest ldap-user-passwords-test- (testing (str "LDAP users should not persist their passwords. Check that if somehow we get passed an LDAP user "- "password, it gets swapped with something random")- (try- (user/create-new-ldap-auth-user! {:email "[email protected]"- :first_name "Test"- :last_name "SomeLdapStuff"- :password "should be removed"})- (let [{:keys [password password_salt]} (t2/select-one [:model/User :password :password_salt] :email "[email protected]")]- (is (= false- (u.password/verify-password "should be removed" password_salt password))))- (finally- (t2/delete! :model/User :email "[email protected]")))))-(deftest new-admin-user-test(testing (str "when you create a new user with `is_superuser` set to `true`, it should create a ""PermissionsGroupMembership object")@@ -166,18 +152,6 @@(is (true?(t2/exists? :model/PermissionsGroupMembership :user_id (u/the-id user), :group_id (u/the-id (perms-group/admin))))))))-(deftest ldap-sequential-login-attributes-test- (testing "You should be able to create a new LDAP user if some `login_attributes` are vectors (#10291)"- (try- (user/create-new-ldap-auth-user! {:email "[email protected]"- :first_name "Test"- :last_name "SomeLdapStuff"- :login_attributes {:local_birds ["Steller's Jay" "Mountain Chickadee"]}})- (is (= {"local_birds" ["Steller's Jay" "Mountain Chickadee"]}- (t2/select-one-fn :login_attributes :model/User :email "[email protected]")))- (finally- (t2/delete! :model/User :email "[email protected]")))))-;;; +----------------------------------------------------------------------------------------------------------------+;;; | New Group IDs Functions |;;; +----------------------------------------------------------------------------------------------------------------+