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
GHY-4587: Bind values in public_sharing/db.clj (8 non-id) (#83142)
Eric Normandf765a1d341ae
diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.ednindex 487bc20ebe6..20104c75a65 100644--- a/.clj-kondo/config.edn+++ b/.clj-kondo/config.edn@@ -990,6 +990,9 @@metabase.usage-metadata.db{:linters {:metabase/unsafe-app-db-query {:level :warning}}}+ metabase.public-sharing.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-testdiff --git a/src/metabase/public_sharing/db.clj b/src/metabase/public_sharing/db.cljindex 8c55dbcd892..1439259ffc5 100644--- a/src/metabase/public_sharing/db.clj+++ b/src/metabase/public_sharing/db.clj@@ -8,39 +8,39 @@(mu/defn unarchived-card-ids-and-public-uuids-by-prefix"The `:id` and `:public_uuid` of the unarchived Cards whose public uuid prefix is `prefix`."[prefix :- :string]- (t2/select [:model/Card :id :public_uuid] :public_uuid_prefix prefix :archived false))+ (t2/select [:model/Card :id :public_uuid] :public_uuid_prefix [:auto/param prefix] :archived false))(mu/defn unarchived-dashboard-ids-and-public-uuids-by-prefix"The `:id` and `:public_uuid` of the unarchived Dashboards whose public uuid prefix is `prefix`."[prefix :- :string]- (t2/select [:model/Dashboard :id :public_uuid] :public_uuid_prefix prefix :archived false))+ (t2/select [:model/Dashboard :id :public_uuid] :public_uuid_prefix [:auto/param prefix] :archived false))(mu/defn unarchived-action-ids-and-public-uuids-by-prefix"The `:id` and `:public_uuid` of the unarchived Actions whose public uuid prefix is `prefix`."[prefix :- :string]- (t2/select [:model/Action :id :public_uuid] :public_uuid_prefix prefix :archived false))+ (t2/select [:model/Action :id :public_uuid] :public_uuid_prefix [:auto/param prefix] :archived false))(mu/defn unarchived-document-ids-and-public-uuids-by-prefix"The `:id` and `:public_uuid` of the unarchived Documents whose public uuid prefix is `prefix`."[prefix :- :string]- (t2/select [:model/Document :id :public_uuid] :public_uuid_prefix prefix :archived false))+ (t2/select [:model/Document :id :public_uuid] :public_uuid_prefix [:auto/param prefix] :archived false))(mu/defn unarchived-cards-by-public-uuid-prefix"The unarchived Cards whose public uuid prefix is `prefix`."[prefix :- :string]- (t2/select :model/Card :public_uuid_prefix prefix :archived false))+ (t2/select :model/Card :public_uuid_prefix [:auto/param prefix] :archived false))(mu/defn unarchived-dashboards-by-public-uuid-prefix"The unarchived Dashboards whose public uuid prefix is `prefix`."[prefix :- :string]- (t2/select :model/Dashboard :public_uuid_prefix prefix :archived false))+ (t2/select :model/Dashboard :public_uuid_prefix [:auto/param prefix] :archived false))(mu/defn unarchived-actions-by-public-uuid-prefix"The unarchived Actions whose public uuid prefix is `prefix`."[prefix :- :string]- (t2/select :model/Action :public_uuid_prefix prefix :archived false))+ (t2/select :model/Action :public_uuid_prefix [:auto/param prefix] :archived false))(mu/defn unarchived-documents-by-public-uuid-prefix"The unarchived Documents whose public uuid prefix is `prefix`."[prefix :- :string]- (t2/select :model/Document :public_uuid_prefix prefix :archived false))+ (t2/select :model/Document :public_uuid_prefix [:auto/param prefix] :archived false))diff --git a/test/metabase/public_sharing/db_test.clj b/test/metabase/public_sharing/db_test.cljnew file mode 100644index 00000000000..ec02fa4d221--- /dev/null+++ b/test/metabase/public_sharing/db_test.clj@@ -0,0 +1,53 @@+(ns metabase.public-sharing.db-test+ (:require+ [clojure.test :refer :all]+ [metabase.public-sharing.core :as public-sharing]+ [metabase.public-sharing.db :as public-sharing.db]+ [metabase.test :as mt]+ [metabase.util.malli :as mu]))++(deftest prefix-lookups-bind-the-prefix-test+ (testing "GHY-4587: each prefix lookup binds `prefix` as a parameter"+ (let [uuid (str (random-uuid))+ prefix (public-sharing/public-uuid-prefix uuid)]+ (mt/with-temp [:model/Card {card-id :id} {:public_uuid uuid}+ :model/Dashboard {dashboard-id :id} {:public_uuid uuid}+ :model/Document {document-id :id} {:name "Shared Doc" :public_uuid uuid}]+ (testing "a matching prefix finds the row"+ (is (= [card-id] (map :id (public-sharing.db/unarchived-card-ids-and-public-uuids-by-prefix prefix))))+ (is (= [dashboard-id] (map :id (public-sharing.db/unarchived-dashboard-ids-and-public-uuids-by-prefix prefix))))+ (is (= [document-id] (map :id (public-sharing.db/unarchived-document-ids-and-public-uuids-by-prefix prefix))))+ (is (= [card-id] (map :id (public-sharing.db/unarchived-cards-by-public-uuid-prefix prefix))))+ (is (= [dashboard-id] (map :id (public-sharing.db/unarchived-dashboards-by-public-uuid-prefix prefix))))+ (is (= [document-id] (map :id (public-sharing.db/unarchived-documents-by-public-uuid-prefix prefix)))))+ (testing "a prefix that looks like SQL is compared as a string, matching nothing"+ (let [hostile (str prefix "' OR '1'='1")]+ (is (empty? (public-sharing.db/unarchived-card-ids-and-public-uuids-by-prefix hostile)))+ (is (empty? (public-sharing.db/unarchived-cards-by-public-uuid-prefix hostile)))))))+ (mt/with-actions-enabled+ (let [uuid (str (random-uuid))]+ (mt/with-actions [{action-id :action-id} {:public_uuid uuid :made_public_by_id (mt/user->id :crowberto)}]+ (let [prefix (public-sharing/public-uuid-prefix uuid)]+ (is (= [action-id] (map :id (public-sharing.db/unarchived-action-ids-and-public-uuids-by-prefix prefix))))+ (is (= [action-id] (map :id (public-sharing.db/unarchived-actions-by-public-uuid-prefix prefix))))))))+ (testing "a non-string prefix is rejected before it reaches the query"+ (is (thrown? clojure.lang.ExceptionInfo+ (public-sharing.db/unarchived-cards-by-public-uuid-prefix {:raw "(SELECT 1)"}))))))++(deftest prefix-lookups-bind-the-prefix-without-the-schema-test+ (testing "GHY-4587: with the schema off, the marker rejects an operator-shaped prefix instead of compiling it into the WHERE"+ ;; An unmarked `[:not= "x"]` in the kv-arg slot compiles to `public_uuid_prefix <> 'x'` and matches every shared row.+ (mt/with-temp [:model/Card _ {:public_uuid (str (random-uuid))}+ :model/Dashboard _ {:public_uuid (str (random-uuid))}+ :model/Document _ {:name "Shared Doc" :public_uuid (str (random-uuid))}]+ (mu/disable-enforcement+ (doseq [lookup [public-sharing.db/unarchived-card-ids-and-public-uuids-by-prefix+ public-sharing.db/unarchived-dashboard-ids-and-public-uuids-by-prefix+ public-sharing.db/unarchived-action-ids-and-public-uuids-by-prefix+ public-sharing.db/unarchived-document-ids-and-public-uuids-by-prefix+ public-sharing.db/unarchived-cards-by-public-uuid-prefix+ public-sharing.db/unarchived-dashboards-by-public-uuid-prefix+ public-sharing.db/unarchived-actions-by-public-uuid-prefix+ public-sharing.db/unarchived-documents-by-public-uuid-prefix]]+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"Marked a whole operator form"+ (doall (lookup [:not= "zzzzzzzz"])))))))))diff --git a/test/metabase/public_sharing_rest/api_test.clj b/test/metabase/public_sharing_rest/api_test.cljindex 03ef250cc45..713ddd17810 100644--- a/test/metabase/public_sharing_rest/api_test.clj+++ b/test/metabase/public_sharing_rest/api_test.clj@@ -214,6 +214,25 @@(with-temp-public-dashboard [{uuid :public_uuid, dashboard-id :id}](is (= dashboard-id (:id (mt/client :get 200 (str "public/dashboard/" uuid))))))))))+(deftest ^:synchronized public-uuid-prefix-lookup-is-bound-test+ (testing "GHY-4587: every shared model resolves by its uuid through the bound prefix lookup"+ (encryption-tu/with-encrypted-app-db+ (mt/with-actions-enabled+ (mt/with-temporary-setting-values [enable-public-sharing true]+ (with-temp-public-card [{uuid :public_uuid, card-id :id}]+ (is (= card-id (:id (mt/client :get 200 (str "public/card/" uuid)))))+ (is (= card-id (:id (public-sharing/public-uuid->model :model/Card uuid)))))+ (with-temp-public-dashboard [{uuid :public_uuid, dashboard-id :id}]+ (is (= dashboard-id (:id (mt/client :get 200 (str "public/dashboard/" uuid)))))+ (is (= dashboard-id (:id (public-sharing/public-uuid->model :model/Dashboard uuid)))))+ (let [{uuid :public_uuid, :as action-opts} (shared-obj)]+ (mt/with-actions [{action-id :action-id} action-opts]+ (is (= action-id (:id (mt/client :get 200 (str "public/action/" uuid)))))+ (is (= action-id (:id (public-sharing/public-uuid->model :model/Action uuid))))))+ (mt/with-temp [:model/Document {uuid :public_uuid, document-id :id} (merge {:name "Shared Doc"} (shared-obj))]+ (is (= document-id (:id (mt/client :get 200 (str "public/document/" uuid)))))+ (is (= document-id (:id (public-sharing/public-uuid->model :model/Document uuid))))))))))+(defn- assert-forged-plaintext-does-not-resolve)]