Skip to content

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

Check actions-enabled and native perms against the query's database (#82775)

appleby8855cdfa85ba
diff --git a/test/metabase/actions_rest/api_test.clj b/test/metabase/actions_rest/api_test.clj
index e8f74659319..fc0cb43d8c3 100644
--- a/test/metabase/actions_rest/api_test.clj
+++ b/test/metabase/actions_rest/api_test.clj
@@ -225,6 +225,53 @@
(mt/user-http-request :rasta :post 403 url params)))
(is (= {:rows-affected 1} (mt/user-http-request :crowberto :post 200 url params)))))))))))))))
+(defn- write-query
+ "A native write query targeting `db-id`."
+ [db-id]
+ {:type "native"
+ :database db-id
+ :native {:query "update people set source = 'pwned' where id = 1"}})
+
+(defn- cross-db-native-action
+ "A native query action whose declared `:database_id` and whose query's own `:database` disagree."
+ [model-id declared-db-id query-db-id]
+ {:name "cross db native action"
+ :type "query"
+ :model_id model-id
+ :database_id declared-db-id
+ :dataset_query (write-query query-db-id)
+ :parameters []})
+
+(defn- model-card-def
+ "A model Card on the current test database, in `user`'s personal collection."
+ [user]
+ {:type :model
+ :dataset_query (mt/native-query {:query "select * from checkins limit 1"})
+ :collection_id (:id (collection/user->personal-collection (mt/user->id user)))})
+
+(defn- do-with-two-actions-enabled-dbs!
+ "Runs `f` with two test databases that both have actions enabled, passing it `[model-db-id query-db-id]`."
+ [f]
+ (mt/dataset test-data
+ (let [query-db-id (mt/id)]
+ (mt/with-actions-enabled
+ (mt/dataset time-test-data
+ (mt/with-actions-enabled
+ (let [model-db-id (mt/id)]
+ (is (not= model-db-id query-db-id))
+ (f model-db-id query-db-id))))))))
+
+(defn- do-with-actions-enabled-on-model-db-only!
+ "Runs `f` with two test databases, passing it `[model-db-id other-db-id]`."
+ [f]
+ (mt/dataset test-data
+ (let [other-db-id (mt/id)] ;; actions stay DISABLED here
+ (mt/dataset time-test-data
+ (mt/with-actions-enabled ;; enabled only on the model's DB
+ (let [model-db-id (mt/id)]
+ (is (not= model-db-id other-db-id))
+ (f model-db-id other-db-id)))))))
+
(deftest action-query-db-differs-from-declared-db-test
(testing "a query action cannot be created against a database other than the one its query targets"
(mt/dataset test-data
@@ -249,6 +296,82 @@
(is (= "Actions are not enabled."
(mt/user-http-request :crowberto :post 400 "action" action)))))))))))))
+(deftest action-query-db-differs-from-declared-db-execution-test
+ (testing "a query action cannot execute against a database other than the one its query targets"
+ (mt/dataset test-data
+ (let [target-db-id (mt/id)] ;; the DB the query really targets; actions OFF here
+ (mt/dataset time-test-data
+ (mt/with-actions-enabled ;; actions enabled only on the model's DB (time-test-data)
+ (let [model-db-id (mt/id)]
+ (is (not= model-db-id target-db-id))
+ ;; persisted through the model layer rather than the API, because the API now refuses this, see
+ ;; action-query-db-differs-from-declared-db-test. This test ensures that an old action created before
+ ;; still cannot execute against the query's real DB.
+ (mt/with-actions [_model {:type :model
+ :dataset_query (mt/native-query {:query "select * from checkins limit 1"})}
+ ;; declare :database_id as the model's (enabled) DB while the query points at
+ ;; target-db-id, whose actions are off
+ {action-id :action-id} {:type :query
+ :database_id model-db-id
+ :name "sneaky cross db action"
+ :dataset_query (write-query target-db-id)
+ :parameters []}]
+ (testing "the declared database_id is overwritten with the query's real database on save"
+ (is (=? {:database_id target-db-id}
+ (mt/user-http-request :crowberto :get 200 (format "action/%s" action-id)))))
+ (testing "execution is blocked because actions are disabled on the query's real DB"
+ (is (= "Actions are not enabled."
+ (mt/user-http-request :crowberto :post 400
+ (format "action/%s/execute" action-id)
+ {:parameters {}}))))))))))))
+
+(deftest action-update-cannot-repoint-database-id-without-the-query-test
+ (testing "PUT /api/action/:id with only :database_id leaves a query action pointed at its query's database"
+ (do-with-two-actions-enabled-dbs!
+ (fn [model-db-id query-db-id]
+ (mt/with-model-cleanup [:model/Action]
+ (mt/with-temp [:model/Card {model-id :id} (model-card-def :crowberto)]
+ (let [created (mt/user-http-request :crowberto :post 200 "action"
+ (cross-db-native-action model-id model-db-id model-db-id))]
+ (is (=? {:database_id model-db-id}
+ created))
+ (testing "the declared database_id is re-derived from the existing query, not written through"
+ (is (=? {:database_id model-db-id}
+ (mt/user-http-request :crowberto :put 200 (str "action/" (:id created))
+ {:database_id query-db-id})))
+ (is (=? {:database_id model-db-id}
+ (mt/user-http-request :crowberto :get 200 (str "action/" (:id created)))))))))))))
+
+(deftest action-update-checks-actions-enabled-on-query-database-test
+ (testing "PUT /api/action/:id checks actions-enabled on the database the updated query targets"
+ (do-with-actions-enabled-on-model-db-only!
+ (fn [model-db-id query-db-id]
+ (mt/with-model-cleanup [:model/Action]
+ (mt/with-temp [:model/Card {model-id :id} (model-card-def :crowberto)]
+ (let [created (mt/user-http-request :crowberto :post 200 "action"
+ (cross-db-native-action model-id model-db-id model-db-id))]
+ (is (= "Actions are not enabled."
+ (mt/user-http-request :crowberto :put 400 (str "action/" (:id created))
+ {:dataset_query (write-query query-db-id)}))))))))))
+
+(deftest action-update-cannot-repoint-a-query-action-with-a-blanked-query-test
+ (testing "PUT /api/action/:id checks actions-enabled on a database_id that no query overrides"
+ (do-with-actions-enabled-on-model-db-only!
+ (fn [model-db-id other-db-id]
+ (mt/with-model-cleanup [:model/Action]
+ (mt/with-temp [:model/Card {model-id :id} (model-card-def :crowberto)]
+ (let [created (mt/user-http-request :crowberto :post 200 "action"
+ (cross-db-native-action model-id model-db-id model-db-id))
+ url (str "action/" (:id created))]
+ (testing "blanking the query leaves the action on the database it was created against"
+ (is (=? {:database_id model-db-id}
+ (mt/user-http-request :crowberto :put 200 url {:dataset_query {}}))))
+ (testing "the database_id that would now be written through is checked"
+ (is (= "Actions are not enabled."
+ (mt/user-http-request :crowberto :put 400 url {:database_id other-db-id}))))
+ (is (=? {:database_id model-db-id}
+ (mt/user-http-request :crowberto :get 200 url))))))))))
+
(deftest unified-action-create-test
(mt/test-helpers-set-global-values!
(mt/with-actions-enabled