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
Correctly handle non-string `@tenants` in JWTs (#66944)
John Swansonb7c18ff30d65
diff --git a/enterprise/backend/src/metabase_enterprise/sso/providers/jwt.clj b/enterprise/backend/src/metabase_enterprise/sso/providers/jwt.cljindex 3555c442ef9..32c04e3dfd4 100644--- a/enterprise/backend/src/metabase_enterprise/sso/providers/jwt.clj+++ b/enterprise/backend/src/metabase_enterprise/sso/providers/jwt.clj@@ -85,15 +85,20 @@email (get jwt-data (jwt-attribute-email))first-name (get jwt-data (jwt-attribute-firstname))last-name (get jwt-data (jwt-attribute-lastname))- tenant-slug (get jwt-data (jwt-attribute-tenant))+ tenant-slug (u/prog1 (get jwt-data (jwt-attribute-tenant))+ (when-not (or (nil? <>)+ (string? <>)+ (integer? <>))+ (throw (ex-info "Value of `@tenant` must be a string" {:status-code 400+ :error :invalid-tenant}))))user-attributes (jwt-data->user-attributes jwt-data)](when-not email- (throw (ex-info (str (tru "JWT token missing email claim"))+ (throw (ex-info (tru "JWT token missing email claim"){:status-code 400:error :missing-email})))(log/infof "Successfully authenticated JWT token for: %s %s" first-name last-name){:success? true- :tenant-slug tenant-slug+ :tenant-slug (some-> tenant-slug str):user-data (->> {:email email:first_name first-name:last_name last-namediff --git a/enterprise/backend/src/metabase_enterprise/tenants/auth_provider.clj b/enterprise/backend/src/metabase_enterprise/tenants/auth_provider.cljindex 1cb7b654a71..2860d158e15 100644--- a/enterprise/backend/src/metabase_enterprise/tenants/auth_provider.clj+++ b/enterprise/backend/src/metabase_enterprise/tenants/auth_provider.clj@@ -67,7 +67,7 @@(methodical/defmethod auth-identity/login! ::create-tenant-if-not-exists[provider {:keys [tenant-slug]:as request}]- (let [existing-tenant (t2/select-one :model/Tenant :slug tenant-slug)]+ (let [existing-tenant (when tenant-slug (t2/select-one :model/Tenant :slug tenant-slug))](next-method provider (create-tenant-if-not-exists! request existing-tenant))))(methodical/prefer-method! #'auth-identity/login! :metabase.auth-identity.provider/provider ::create-tenant-if-not-exists)diff --git a/enterprise/backend/test/metabase_enterprise/sso/integrations/jwt_test.clj b/enterprise/backend/test/metabase_enterprise/sso/integrations/jwt_test.cljindex 457ac80d84a..c8b19c6c03e 100644--- a/enterprise/backend/test/metabase_enterprise/sso/integrations/jwt_test.clj+++ b/enterprise/backend/test/metabase_enterprise/sso/integrations/jwt_test.clj@@ -1107,6 +1107,51 @@(testing "user is not assigned to groups not mentioned in JWT claims"(is (not (contains? (group-memberships (u/the-id user)) "tenant-admins"))))))))))))))+(deftest non-string-tenant-slugs-are-handled-correctly+ (with-jwt-default-setup!+ (mt/with-additional-premium-features #{:tenants}+ (mt/with-temporary-setting-values [use-tenants true]+ (mt/with-model-cleanup [:model/User :model/Tenant]+ (testing "Integers are allowed, converted to strings"+ (let [response (client/client-real-response :get 302 "/auth/sso"+ {:request-options {:redirect-strategy :none}}+ :return_to default-redirect-uri+ :jwt+ (jwt/sign+ {:email "[email protected]"+ :first_name "Tenant"+ :last_name "User"+ "@tenant" 123}+ default-jwt-secret))]+ (is (saml-test/successful-login? response))+ (is (t2/exists? :model/Tenant :slug "123"))))+ (testing "Other non-string values are rejected"+ (let [response (client/client-real-response :get 401 "/auth/sso"+ {:request-options {:redirect-strategy :none}}+ :return_to default-redirect-uri+ :jwt+ (jwt/sign+ {:email "[email protected]"+ :first_name "Tenant"+ :last_name "User"+ "@tenant" false}+ default-jwt-secret))]+ (is (= "Value of `@tenant` must be a string" (:body response)))+ (is (not (saml-test/successful-login? response)))))+ (testing "Other non-string values are rejected"+ (let [response (client/client-real-response :get 401 "/auth/sso"+ {:request-options {:redirect-strategy :none}}+ :return_to default-redirect-uri+ :jwt+ (jwt/sign+ {:email "[email protected]"+ :first_name "Tenant"+ :last_name "User"+ "@tenant" {"foo" "bar"}}+ default-jwt-secret))]+ (is (= "Value of `@tenant` must be a string" (:body response)))+ (is (not (saml-test/successful-login? response))))))))))+(deftest tenant-user-group-sync-on-subsequent-login-test(testing "JWT tenant user group memberships are synced correctly on subsequent logins"(with-jwt-default-setup!