Skip to content

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

GHY-4590: Bind values in auth_identity/db.clj (7 non-id) (#83141)

Eric Normandd90b5c6b1393
diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.edn
index 31b9e15d6e7..35fd1e162bf 100644
--- a/.clj-kondo/config.edn
+++ b/.clj-kondo/config.edn
@@ -984,6 +984,9 @@
metabase.settings.db
{:linters {:metabase/unsafe-app-db-query {:level :warning}}}
+ metabase.auth-identity.db
+ {:linters {:metabase/unsafe-app-db-query {:level :warning}}}
+
;; .cljc tests run in both Clojure and ClojureScript — `with-dynamic-fn-redefs` is JVM-only,
;; so leave these alone.
metabase.lib.aggregation-test
diff --git a/src/metabase/auth_identity/db.clj b/src/metabase/auth_identity/db.clj
index efb6cb24b1e..a50db1ed101 100644
--- a/src/metabase/auth_identity/db.clj
+++ b/src/metabase/auth_identity/db.clj
@@ -26,25 +26,25 @@
"The AuthIdentity of the User with `user-id` at `provider`, or nil."
[user-id :- ::lib.schema.id/user
provider :- :string]
- (t2/select-one :model/AuthIdentity :user_id user-id :provider provider))
+ (t2/select-one :model/AuthIdentity :user_id (long user-id) :provider [:auto/param provider]))
(mu/defn auth-identity-id
"The id of the AuthIdentity of the User with `user-id` at `provider`, or nil."
[user-id :- ::lib.schema.id/user
provider :- :string]
- (t2/select-one-pk :model/AuthIdentity :user_id user-id :provider provider))
+ (t2/select-one-pk :model/AuthIdentity :user_id (long user-id) :provider [:auto/param provider]))
(mu/defn auth-identity-expiry
"The id and expiry of the AuthIdentity of the User with `user-id` at `provider`, or nil."
[user-id :- ::lib.schema.id/user
provider :- :string]
- (t2/select-one [:model/AuthIdentity :id :expires_at] :user_id user-id :provider provider))
+ (t2/select-one [:model/AuthIdentity :id :expires_at] :user_id (long user-id) :provider [:auto/param provider]))
(mu/defn auth-identity-exists?
"Whether the User with `user-id` has an AuthIdentity at `provider`."
[user-id :- ::lib.schema.id/user
provider :- :string]
- (t2/exists? :model/AuthIdentity :user_id user-id :provider provider))
+ (t2/exists? :model/AuthIdentity :user_id (long user-id) :provider [:auto/param provider]))
(mu/defn insert-auth-identity!
"Insert the AuthIdentity `row`, returning the number inserted."
@@ -67,14 +67,14 @@
"Delete the AuthIdentities of the User with `user-id` at `provider`, returning the number deleted."
[user-id :- ::lib.schema.id/user
provider :- :string]
- (t2/delete! :model/AuthIdentity :user_id user-id :provider provider))
+ (t2/delete! :model/AuthIdentity :user_id (long user-id) :provider [:auto/param provider]))
(mu/defn delete-sessions-for-user!
"Delete every Session of the User with `user-id`, returning the number deleted. Duplicates
`metabase.session.db/delete-sessions-for-user!`; can't delegate to it because the `session` module already depends
on `auth-identity`, so the reverse dependency would be a module cycle."
[user-id :- ::lib.schema.id/user]
- (t2/delete! :model/Session :user_id user-id))
+ (t2/delete! :model/Session :user_id (long user-id)))
(mu/defn user
"The User with `user-id`, or nil."
@@ -89,7 +89,7 @@
(mu/defn user-login-columns
"The id, active flag, last login, and tenant id of the User with `user-id`, or nil."
[user-id :- ::lib.schema.id/user]
- (t2/select-one [:model/User :id :is_active :last_login :tenant_id] :id user-id))
+ (t2/select-one [:model/User :id :is_active :last_login :tenant_id] :id (long user-id)))
(mu/defn user-login-columns-by-email
"The id, active flag, last login, and tenant id of the User whose email matches `email` case-insensitively, or
@@ -105,7 +105,7 @@
(mu/defn user-active?
"Whether the User with `user-id` is active."
[user-id :- ::lib.schema.id/user]
- (t2/select-one-fn :is_active :model/User :id user-id))
+ (t2/select-one-fn :is_active :model/User :id (long user-id)))
(mu/defn update-user!
"Apply `changes` to the User with `user-id`, returning the number updated."
@@ -133,14 +133,16 @@
[:saml-session-index {:optional true} [:maybe :string]]
[:saml-name-id {:optional true} [:maybe :string]]
[:saml-name-id-format {:optional true} [:maybe :string]]]]
+ ;; `:id` and `:session_key` stay unmarked: the before-insert hook checks the id is not a UUID and hashes the key,
+ ;; and both need the plain string. The key itself never reaches SQL, only its hash.
(t2/insert-returning-instance! :model/Session
;; Without setting the ID here we can't return an instance on MySQL
:id session-id
- :user_id user-id
- :auth_identity_id auth-identity-id
+ :user_id (long user-id)
+ :auth_identity_id (some-> auth-identity-id long)
:session_key session-key
- :expires_at expires-at
- :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)))
+ :expires_at [:auto/param expires-at]
+ :mfa_auth_identity_id (some-> mfa-auth-identity-id long)
+ :saml_session_index [:auto/param (:saml-session-index opts)]
+ :saml_name_id [:auto/param (:saml-name-id opts)]
+ :saml_name_id_format [:auto/param (:saml-name-id-format opts)]))
diff --git a/test/metabase/auth_identity/db_test.clj b/test/metabase/auth_identity/db_test.clj
new file mode 100644
index 00000000000..5bcba450bcc
--- /dev/null
+++ b/test/metabase/auth_identity/db_test.clj
@@ -0,0 +1,87 @@
+(ns metabase.auth-identity.db-test
+ (:require
+ [clojure.test :refer :all]
+ [java-time.api :as t]
+ [metabase.auth-identity.db :as auth-identity.db]
+ [metabase.session.core :as session]
+ [metabase.test :as mt]
+ [metabase.util.malli :as mu]
+ [toucan2.core :as t2]))
+
+(deftest operator-shaped-provider-is-not-structure-test
+ (testing "GHY-4590: with schema enforcement off, as in prod, an operator form passed as `provider` must not
+ become SQL structure. Unbound, `[:like \"%\"]` compiled to `provider LIKE '%'` and matched every
+ provider."
+ (mt/with-temp [:model/User {user-id :id} {}
+ :model/AuthIdentity _ {:user_id user-id :provider "google"}]
+ (mu/disable-enforcement
+ (doseq [[fn-name f] {"auth-identity" auth-identity.db/auth-identity
+ "auth-identity-id" auth-identity.db/auth-identity-id
+ "auth-identity-expiry" auth-identity.db/auth-identity-expiry
+ "auth-identity-exists?" auth-identity.db/auth-identity-exists?
+ "delete-auth-identities!" auth-identity.db/delete-auth-identities!}]
+ (testing fn-name
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Marked a whole operator form"
+ (f user-id [:like "%"])))))
+ (is (t2/exists? :model/AuthIdentity :user_id user-id :provider "google"))))))
+
+(deftest insert-session-binds-written-values-test
+ (testing "GHY-4590: with schema enforcement off, as in prod, an operator form passed as a SAML value or an
+ expiry must not become SQL. Unbound, `[:upper \"abc\"]` compiled to `UPPER(?)` and stored `ABC`."
+ (mt/with-temp [:model/User {user-id :id} {}]
+ (mu/disable-enforcement
+ (doseq [[label expires-at opts] [["saml-name-id" nil {:saml-name-id [:upper "abc"]}]
+ ["saml-session-index" nil {:saml-session-index [:upper "abc"]}]
+ ["saml-name-id-format" nil {:saml-name-id-format [:upper "abc"]}]
+ ["expires-at" [:now] {}]]]
+ (testing label
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Marked a whole operator form"
+ (auth-identity.db/insert-session! (session/generate-session-id) user-id nil
+ (session/generate-session-key) expires-at nil
+ opts))))))
+ (is (not (t2/exists? :model/Session :user_id user-id))))))
+
+(deftest insert-session-stores-values-test
+ (mt/with-temp [:model/User {user-id :id} {}
+ :model/AuthIdentity {ai-id :id} {:user_id user-id :provider "google"}]
+ (let [session-key (session/generate-session-key)
+ expires-at (t/offset-date-time 2030 1 1 0 0 0 0 (t/zone-offset 0))
+ inserted (auth-identity.db/insert-session! (session/generate-session-id) user-id ai-id session-key
+ expires-at nil
+ {:saml-session-index "idx-1"
+ :saml-name-id "[email protected]"})
+ row (t2/select-one :model/Session :id (:id inserted))]
+ (testing "the key is stored hashed, and every other value is stored as given"
+ (is (= (session/hash-session-key session-key) (:key_hashed row)))
+ (is (=? {:user_id user-id
+ :auth_identity_id ai-id
+ :mfa_auth_identity_id nil
+ :expires_at #(t/= expires-at (t/offset-date-time %))
+ :saml_session_index "idx-1"
+ :saml_name_id "[email protected]"
+ :saml_name_id_format nil}
+ row))))))
+
+(deftest lookups-filter-by-user-and-provider-test
+ (mt/with-temp [:model/User {user-id :id} {}
+ :model/User {other-id :id} {}
+ :model/AuthIdentity {ai-id :id} {:user_id user-id :provider "google"}
+ :model/AuthIdentity _ {:user_id other-id :provider "google"}]
+ (testing "a matching user and provider finds the identity"
+ (is (= ai-id (:id (auth-identity.db/auth-identity user-id "google"))))
+ (is (= ai-id (auth-identity.db/auth-identity-id user-id "google")))
+ (is (= ai-id (:id (auth-identity.db/auth-identity-expiry user-id "google"))))
+ (is (true? (auth-identity.db/auth-identity-exists? user-id "google"))))
+ (testing "another provider matches nothing"
+ (is (nil? (auth-identity.db/auth-identity user-id "password")))
+ (is (nil? (auth-identity.db/auth-identity-id user-id "password")))
+ (is (nil? (auth-identity.db/auth-identity-expiry user-id "password")))
+ (is (false? (auth-identity.db/auth-identity-exists? user-id "password"))))
+ (testing "a SQL-shaped provider is compared as a string, matching nothing"
+ (is (false? (auth-identity.db/auth-identity-exists? user-id "google' OR '1'='1"))))
+ (testing "delete removes only the given user's identity at the given provider"
+ (is (= 0 (auth-identity.db/delete-auth-identities! user-id "password")))
+ (is (= 1 (auth-identity.db/delete-auth-identities! user-id "google")))
+ (is (t2/exists? :model/AuthIdentity :user_id other-id :provider "google")))
+ (testing "an id-only lookup still finds the user"
+ (is (true? (auth-identity.db/user-active? user-id))))))