Skip to content

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

Better error messages for actions that violate privileges (#62282)

Ngoc Khuat92e6d0c760f8
diff --git a/src/metabase/actions/core.clj b/src/metabase/actions/core.clj
index 4712fbc87bc..279518131fd 100644
--- a/src/metabase/actions/core.clj
+++ b/src/metabase/actions/core.clj
@@ -43,6 +43,7 @@
violate-check-constraint
violate-foreign-key-constraint
violate-not-null-constraint
+ violate-permission-constraint
violate-unique-constraint]
[metabase.actions.execution
execute-action!
diff --git a/src/metabase/actions/error.clj b/src/metabase/actions/error.clj
index 1f01e0317a9..5219b0a0e9a 100644
--- a/src/metabase/actions/error.clj
+++ b/src/metabase/actions/error.clj
@@ -16,6 +16,10 @@
"Error type for SQL incorrect value type."
::incorrect-value-type)
+(def violate-permission-constraint
+ "Error type for SQL permission denied violations."
+ ::violate-permission-constraint)
+
(def violate-check-constraint
"Error type for SQL check constraint violation."
::violate-check-constraint)
diff --git a/src/metabase/driver/h2/actions.clj b/src/metabase/driver/h2/actions.clj
index eed90e48240..43fb5ff5a25 100644
--- a/src/metabase/driver/h2/actions.clj
+++ b/src/metabase/driver/h2/actions.clj
@@ -110,6 +110,24 @@
:message (tru "Some of your values aren’t of the correct type for the database.")
:errors {}}))
+(defmethod sql-jdbc.actions/maybe-parse-sql-error [:h2 driver-api/violate-permission-constraint]
+ [_driver error-type _database action-type error-message]
+ (when-let [[_match _table-name]
+ (re-find #"Not enough rights for object \"(.+)\"" error-message)]
+ (merge {:type error-type}
+ (case action-type
+ (:table.row/create :model.row/create)
+ {:message (tru "You don''t have permission to add data to this table.")
+ :errors {}}
+
+ (:table.row/update :model.row/update)
+ {:message (tru "You don''t have permission to update data in this table.")
+ :errors {}}
+
+ (:table.row/delete :model.row/delete)
+ {:message (tru "You don''t have permission to delete data from this table.")
+ :errors {}}))))
+
(defmethod sql-jdbc.actions/maybe-parse-sql-error [:h2 driver-api/violate-check-constraint]
[_driver error-type _database _action-type error-message]
(when-let [[_match constraint-name]
diff --git a/src/metabase/driver/mysql/actions.clj b/src/metabase/driver/mysql/actions.clj
index 1927b81d45f..cf4edc73044 100644
--- a/src/metabase/driver/mysql/actions.clj
+++ b/src/metabase/driver/mysql/actions.clj
@@ -111,6 +111,39 @@
:message (tru "Some of your values aren’t of the correct type for the database.")
:errors {column (tru "This value should be of type {0}." (str/capitalize expected-type))}})))
+(defmethod sql-jdbc.actions/maybe-parse-sql-error [:mysql driver-api/violate-permission-constraint]
+ [_driver error-type _database action-type error-message]
+ (or (when-let [[_match _command _table-name]
+ (re-find #"(INSERT|UPDATE|DELETE) command denied to user .* for table '(.+)'" error-message)]
+ (merge {:type error-type}
+ (case action-type
+ (:table.row/create :model.row/create)
+ {:message (tru "You don''t have permission to add data to this table.")
+ :errors {}}
+
+ (:table.row/update :model.row/update)
+ {:message (tru "You don''t have permission to update data in this table.")
+ :errors {}}
+
+ (:table.row/delete :model.row/delete)
+ {:message (tru "You don''t have permission to delete data from this table.")
+ :errors {}})))
+ (when-let [[_match _table-name]
+ (re-find #"Access denied for user .* to database '(.+)'" error-message)]
+ (merge {:type error-type}
+ (case action-type
+ (:table.row/create :model.row/create)
+ {:message (tru "You don''t have permission to add data to this database.")
+ :errors {}}
+
+ (:table.row/update :model.row/update)
+ {:message (tru "You don''t have permission to update data in this database.")
+ :errors {}}
+
+ (:table.row/delete :model.row/delete)
+ {:message (tru "You don''t have permission to delete data from this database.")
+ :errors {}})))))
+
(defmethod sql-jdbc.actions/maybe-parse-sql-error [:mysql driver-api/violate-check-constraint]
[_driver error-type _database _action-type error-message]
(or (when-let [[_match constraint-name]
diff --git a/src/metabase/driver/postgres/actions.clj b/src/metabase/driver/postgres/actions.clj
index 279a719f17c..f36b89f5052 100644
--- a/src/metabase/driver/postgres/actions.clj
+++ b/src/metabase/driver/postgres/actions.clj
@@ -74,6 +74,23 @@
:message (tru "Some of your values aren’t of the correct type for the database.")
:errors {}}))
+(defmethod sql-jdbc.actions/maybe-parse-sql-error [:postgres driver-api/violate-permission-constraint]
+ [_driver error-type _database action-type error-message]
+ (when (re-find #"permission denied for" error-message)
+ (merge {:type error-type}
+ (case action-type
+ (:table.row/create :model.row/create)
+ {:message (tru "You don''t have permission to add data to this table.")
+ :errors {}}
+
+ (:table.row/update :model.row/update)
+ {:message (tru "You don''t have permission to update data in this table.")
+ :errors {}}
+
+ (:table.row/delete :model.row/delete)
+ {:message (tru "You don''t have permission to delete data from this table.")
+ :errors {}}))))
+
(defmethod sql-jdbc.actions/maybe-parse-sql-error [:postgres driver-api/violate-check-constraint]
[_driver error-type _database _action-type error-message]
(when-let [[_match constraint-name]
diff --git a/src/metabase/driver_api/core.clj b/src/metabase/driver_api/core.clj
index 4404f0b5f10..d597ccba311 100644
--- a/src/metabase/driver_api/core.clj
+++ b/src/metabase/driver_api/core.clj
@@ -71,6 +71,7 @@
actions/violate-check-constraint
actions/violate-foreign-key-constraint
actions/violate-not-null-constraint
+ actions/violate-permission-constraint
actions/violate-unique-constraint
add/add-alias-info
add/field-reference-mlv2
diff --git a/test/metabase/actions/actions_test.clj b/test/metabase/actions/actions_test.clj
index eceb89765c5..af36093865c 100644
--- a/test/metabase/actions/actions_test.clj
+++ b/test/metabase/actions/actions_test.clj
@@ -1,14 +1,19 @@
(ns ^:mb/driver-tests metabase.actions.actions-test
(:require
+ [clojure.java.jdbc :as jdbc]
[clojure.test :refer :all]
[clojure.walk :as walk]
[metabase.actions.actions :as actions]
+ [metabase.actions.error :as actions.error]
[metabase.actions.execution :as actions.execution]
[metabase.actions.models :as action]
[metabase.api.common :refer [*current-user-permissions-set*]]
[metabase.driver :as driver]
+ [metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]
[metabase.query-processor :as qp]
+ [metabase.sync.core :as sync]
[metabase.test :as mt]
+ [metabase.test.data.interface :as tx]
[metabase.util :as u]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
@@ -664,3 +669,94 @@
(is (= 2 (first (mt/first-row (mt/run-mbql-query ants {:aggregation [[:count]], :limit 1})))))
(is (= [] (mt/rows (mt/run-mbql-query ants {:filter [:= $id "d6b02fa2-bf7b-4b32-80d5-060b649c9859"]})))
"Selecting for deleted rows should return an empty result")))))
+
+(deftest test-permission-denied-errors
+ (testing "Permission denied errors should return nice error messages"
+ ;; can't create users with restricted permissions in h2
+ (mt/with-actions-enabled
+ (mt/test-drivers (disj (mt/normal-drivers-with-feature :actions) :h2)
+ (let [test-db-name "permission_test"
+ test-user-name "test_user"]
+ ;; Create a test database with test data
+ (tx/drop-if-exists-and-create-db! driver/*driver* test-db-name)
+ (let [details (mt/dbdef->connection-details driver/*driver* :db {:database-name test-db-name})
+ admin-spec (sql-jdbc.conn/connection-details->spec driver/*driver* details)]
+ ;; Set up test data and user with limited permissions
+ (doseq [stmt (concat
+ ;; Create the categories table with test data
+ ["CREATE TABLE categories (id INTEGER PRIMARY KEY, name VARCHAR(255) NOT NULL);"
+ "INSERT INTO categories (id, name) VALUES (1, 'Test Category');"
+ "INSERT INTO categories (id, name) VALUES (2, 'Another Category');"]
+ ;; Create test user with no password and only SELECT permissions
+ [(format "DROP USER IF EXISTS %s" test-user-name)
+ (case driver/*driver*
+ :postgres (format "CREATE USER %s WITH PASSWORD '123456';" test-user-name)
+ :mysql (format "CREATE USER '%s'" test-user-name))
+ (case driver/*driver*
+ :postgres (format "GRANT SELECT ON TABLE categories TO %s" test-user-name)
+ :mysql (format "GRANT SELECT ON %s.categories TO '%s'" test-db-name test-user-name))])]
+ (when stmt
+ (jdbc/execute! admin-spec [stmt])))
+
+ ;; Create connection details for test user (no password)
+ (let [test-user-details (cond-> details
+ (= driver/*driver* :mysql)
+ (assoc :user test-user-name
+ :additional-options "useSSL=false&allowPublicKeyRetrieval=true")
+
+ (= driver/*driver* :postgres)
+ (assoc :user test-user-name
+ :password "123456"))]
+ ;; Create temporary database with test user credentials
+ (mt/with-temp [:model/Database test-database {:engine driver/*driver*, :details test-user-details :settings {:database-enable-actions true
+ :database-enable-table-editing true}}]
+ ;; Sync the database to get table metadata
+ (sync/sync-database! test-database)
+ (let [table-id (t2/select-one-pk :model/Table :db_id (:id test-database) :name "categories")]
+ (testing (str "INSERT permission denied for " driver/*driver*)
+ (let [result (try
+ (actions/perform-action-v2!
+ :table.row/create
+ test-scope
+ [{:database (:id test-database)
+ :table-id table-id
+ :row {"name" "New Test Category"}}])
+ nil
+ (catch Exception e
+ (ex-data e)))
+ first-error (first (:errors result))]
+ (is (= 400 (:status-code result)))
+ (is (= actions.error/violate-permission-constraint (:type first-error)))
+ (is (= "You don't have permission to add data to this table." (:message first-error)))))
+
+ (testing (str "UPDATE permission denied for " driver/*driver*)
+ (let [result (try
+ (actions/perform-action-v2!
+ :table.row/update
+ test-scope
+ [{:database (:id test-database)
+ :table-id table-id
+ :row {"id" 1 "name" "Updated Category"}}])
+ nil
+ (catch Exception e
+ (ex-data e)))
+ first-error (first (:errors result))]
+ (is (= 400 (:status-code result)))
+ (is (= actions.error/violate-permission-constraint (:type first-error)))
+ (is (= "You don't have permission to update data in this table." (:message first-error)))))
+
+ (testing (str "DELETE permission denied for " driver/*driver*)
+ (let [result (try
+ (actions/perform-action-v2!
+ :table.row/delete
+ test-scope
+ [{:database (:id test-database)
+ :table-id table-id
+ :row {"id" 1}}])
+ nil
+ (catch Exception e
+ (ex-data e)))
+ first-error (first (:errors result))]
+ (is (= 400 (:status-code result)))
+ (is (= actions.error/violate-permission-constraint (:type first-error)))
+ (is (= "You don't have permission to delete data from this table." (:message first-error))))))))))))))