Skip to content

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

Correctly serialize lists in SAML/JWS attributes (#73605)

Nathan Voxland400e9c693b5f
diff --git a/enterprise/backend/src/metabase_enterprise/sandbox/models/params/field_values.clj b/enterprise/backend/src/metabase_enterprise/sandbox/models/params/field_values.clj
index 0178a9feedc..4352f8ebb21 100644
--- a/enterprise/backend/src/metabase_enterprise/sandbox/models/params/field_values.clj
+++ b/enterprise/backend/src/metabase_enterprise/sandbox/models/params/field_values.clj
@@ -19,18 +19,20 @@
(table/only-sandboxed-perms? (or table (field/table field))))
(defn- table-id->sandbox
- "Find the GTAP for current user that apply to table `table-id`."
+ "Find the GTAP for current user that apply to table `table-id`. Returns nil when there is no current user (e.g. when
+ called from a background sync), since sandboxes are scoped to a user's group memberships."
[table-id]
- (let [group-ids (t2/select-fn-set :group_id :model/PermissionsGroupMembership :user_id api/*current-user-id*)
- sandboxes (when (seq group-ids)
- (t2/select :model/Sandbox
- :group_id [:in group-ids]
- :table_id table-id))]
- (when sandboxes
- (sandboxing/assert-one-sandbox-per-table sandboxes)
- ;; there should be only one gtap per table and we only need one table here
- ;; see docs in [[metabase.permissions.models.permissions]] for more info
- (t2/hydrate (first sandboxes) :card))))
+ (when api/*current-user-id*
+ (let [group-ids (t2/select-fn-set :group_id :model/PermissionsGroupMembership :user_id api/*current-user-id*)
+ sandboxes (when (seq group-ids)
+ (t2/select :model/Sandbox
+ :group_id [:in group-ids]
+ :table_id table-id))]
+ (when sandboxes
+ (sandboxing/assert-one-sandbox-per-table sandboxes)
+ ;; there should be only one gtap per table and we only need one table here
+ ;; see docs in [[metabase.permissions.models.permissions]] for more info
+ (t2/hydrate (first sandboxes) :card)))))
(defn- field->sandbox-attributes-for-current-user
"Returns the gtap attributes for current user that applied to `field`.
diff --git a/enterprise/backend/src/metabase_enterprise/sso/integrations/sso_utils.clj b/enterprise/backend/src/metabase_enterprise/sso/integrations/sso_utils.clj
index 5ee47bfaecb..6ebc3858407 100644
--- a/enterprise/backend/src/metabase_enterprise/sso/integrations/sso_utils.clj
+++ b/enterprise/backend/src/metabase_enterprise/sso/integrations/sso_utils.clj
@@ -90,17 +90,22 @@
(-> group-mappings vals flatten set))
(defn stringify-valid-attributes
- "Remove all invalid attributes from passed user attributes, make sure all the remaining keys and values are strings"
+ "Remove all invalid attributes from passed user attributes, make sure all the remaining keys and values are strings.
+ Multi-value attributes (vectors, lists, lazy seqs — produced by SAML/JWT for repeated attributes) are joined into a
+ comma-separated string so that downstream code receives a usable string value."
[attrs]
(->> attrs
(keep (fn [[key value]]
(cond
- (or (vector? value) (map? value) (nil? value))
+ (or (map? value) (nil? value))
(log/warnf "Dropping attribute '%s' with non-stringable value: %s" (name key) value)
(str/starts-with? (name key) "@")
(log/warnf "Dropping attribute '%s', keys beginning with `@` are reserved" (name key))
+ (sequential? value)
+ [(u/qualified-name key) (str/join "," (map str value))]
+
:else
[(u/qualified-name key) (str value)])))
(into {})))
diff --git a/enterprise/backend/test/metabase_enterprise/sso/integrations/jwt_test.clj b/enterprise/backend/test/metabase_enterprise/sso/integrations/jwt_test.clj
index a2cd9c0cfec..372285b8bf2 100644
--- a/enterprise/backend/test/metabase_enterprise/sso/integrations/jwt_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/sso/integrations/jwt_test.clj
@@ -898,7 +898,7 @@
default-jwt-secret))))))))
(deftest non-string-jwt-claims-dropped-test
- (testing "JWT claims with non-string values are dropped and warning is logged"
+ (testing "JWT claims are stringified or joined as appropriate; unstringable values are dropped (UXW-3921)"
(with-jwt-default-setup!
(mt/with-log-messages-for-level [jwt-log-messages [metabase-enterprise :warn]]
(let [response (client/client-full-response :get 302 "/auth/sso"
@@ -919,14 +919,14 @@
default-jwt-secret))]
(is (sso.test-setup/successful-login? response))
- (testing "only string attributes are saved to jwt_attributes"
+ (testing "scalar attributes are stringified, array attributes are joined with commas, unstringable values dropped"
(is (= {"string_attr" "valid-string"
"number_attr" "42"
- "boolean_attr" "false"}
+ "boolean_attr" "false"
+ "array_attr" "item1,item2"}
(t2/select-one-fn :jwt_attributes :model/User :email "[email protected]"))))
(testing "warning messages are logged for non-stringable values"
- (is (some #(re-find #"Dropping attribute 'array_attr' with non-stringable value: \[\"item1\" \"item2\"\]" %) (map :message (jwt-log-messages))))
(is (some #(re-find #"Dropping attribute 'object_attr' with non-stringable value: \{:nested \"value\"\}" %) (map :message (jwt-log-messages))))
(is (some #(re-find #"Dropping attribute 'null_attr' with non-stringable value: null" %) (map :message (jwt-log-messages)))))
(testing "warning messages are logged for `@`-prefixed keys"
diff --git a/enterprise/backend/test/metabase_enterprise/sso/integrations/sso_utils_test.clj b/enterprise/backend/test/metabase_enterprise/sso/integrations/sso_utils_test.clj
index 64ea0003625..a4dccc62950 100644
--- a/enterprise/backend/test/metabase_enterprise/sso/integrations/sso_utils_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/sso/integrations/sso_utils_test.clj
@@ -21,3 +21,33 @@
"not a url"
"localhost:3000" ; URI thinks `localhost` here is scheme
"http://localhost:3000?a=not a param"))))
+
+(deftest ^:parallel stringify-valid-attributes-test
+ (testing "string and number values are stringified"
+ (is (= {"name" "alice" "age" "42"}
+ (sso-utils/stringify-valid-attributes {:name "alice" :age 42}))))
+ (testing "nil values are dropped"
+ (is (= {"name" "alice"}
+ (sso-utils/stringify-valid-attributes {:name "alice" :missing nil}))))
+ (testing "map values are dropped"
+ (is (= {"name" "alice"}
+ (sso-utils/stringify-valid-attributes {:name "alice" :nested {:k "v"}}))))
+ (testing "keys starting with @ are dropped"
+ (is (= {"name" "alice"}
+ (sso-utils/stringify-valid-attributes {:name "alice" (keyword "@reserved") "v"}))))
+ (testing "multi-value sequential attributes are joined with commas (UXW-3921)"
+ (testing "vectors (e.g. JWT JSON arrays)"
+ (is (= {"roles" "admin,user"}
+ (sso-utils/stringify-valid-attributes {:roles ["admin" "user"]}))))
+ (testing "lazy seqs (e.g. SAML multi-value attributes)"
+ (is (= {"dept" "a,b,c"}
+ (sso-utils/stringify-valid-attributes {:dept (map identity ["a" "b" "c"])}))))
+ (testing "lists"
+ (is (= {"groups" "g1,g2"}
+ (sso-utils/stringify-valid-attributes {:groups '("g1" "g2")}))))
+ (testing "single-element collections still join cleanly"
+ (is (= {"tag" "only"}
+ (sso-utils/stringify-valid-attributes {:tag ["only"]}))))
+ (testing "non-string sequential elements are stringified"
+ (is (= {"ids" "1,2,3"}
+ (sso-utils/stringify-valid-attributes {:ids [1 2 3]}))))))