Skip to content

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

Make the permission check a hash lookup instead of a scan of every granted path (#79076)

Alexander Polyankin860f2beff2b0
diff --git a/src/metabase/permissions/models/permissions.clj b/src/metabase/permissions/models/permissions.clj
index f4895ff4be0..78097b3bff2 100644
--- a/src/metabase/permissions/models/permissions.clj
+++ b/src/metabase/permissions/models/permissions.clj
@@ -177,7 +177,6 @@
[metabase.util.log :as log]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
- [metabase.util.performance :as perf]
[methodical.core :as methodical]
[toucan2.core :as t2]))
@@ -229,22 +228,37 @@
;;; -------------------------------------------- Permissions Checking Fns --------------------------------------------
-(defn is-permissions-for-object?
- "Does `permissions-path` grant *full* access for `path`?"
- [permissions-path path]
- (str/starts-with? path permissions-path))
-
-(defn set-has-full-permissions?
- "Does `permissions-set` grant *full* access to object with `path`?"
- ^Boolean [permissions-set path]
- (boolean (perf/some #(is-permissions-for-object? % path) permissions-set)))
+(defn- granting-paths
+ "The permissions paths that grant full access to `path` — `path` itself and each of its ancestors, most specific
+ first:
+
+ (granting-paths \"/a/b/c/\") ; => [\"/a/b/c/\" \"/a/b/\" \"/a/\" \"/\"]
+
+ Every permissions path ends in a slash (see this namespace's docstring), so a granted path is a prefix of `path`
+ exactly when it is one of these. There is only one per segment of `path`, which is what lets
+ [[set-has-full-permissions?]] answer with a handful of hash lookups instead of a scan of every path the User was
+ granted. A `path` that does not end in a slash can have no descendants, so it only ever matches itself."
+ [^String path]
+ (loop [acc (cond-> (transient [])
+ (not (str/ends-with? path "/")) (conj! path))
+ i (.lastIndexOf path (int \/))]
+ (if (neg? i)
+ (persistent! acc)
+ (recur (conj! acc (.substring path 0 (inc i)))
+ (.lastIndexOf path (int \/) (dec i))))))
+
+(mu/defn set-has-full-permissions? :- :boolean
+ "Does `permissions-set` grant *full* access to object with `path`? A `nil` `permissions-set` grants nothing."
+ [permissions-set :- [:maybe [:set :string]]
+ path :- :string]
+ (boolean (and permissions-set
+ (some permissions-set (granting-paths path)))))
(mu/defn set-has-full-permissions-for-set? :- :boolean
"Do the permissions paths in `permissions-set` grant *full* access to all the object paths in `paths-set`?"
- [permissions-set paths-set]
- (let [permissions (or (:as-vec (meta permissions-set))
- permissions-set)]
- (every? (partial set-has-full-permissions? permissions) paths-set)))
+ [permissions-set :- [:maybe [:set :string]]
+ paths-set :- [:set :string]]
+ (every? #(set-has-full-permissions? permissions-set %) paths-set))
(mu/defn set-has-application-permission-of-type? :- :boolean
"Does `permissions-set` grant *full* access to a application permission of type `perm-type`?"
diff --git a/src/metabase/permissions/user.clj b/src/metabase/permissions/user.clj
index c3f873c3e67..9cdfdf9243a 100644
--- a/src/metabase/permissions/user.clj
+++ b/src/metabase/permissions/user.clj
@@ -19,30 +19,27 @@
"Return a set of all permissions object paths that `user-or-id` has been granted access to. (2 DB Calls)"
[user-or-id]
(tracing/with-span :db-app "db-app.permissions-load" {}
- (let [s
- (set (when-let [user-id (u/the-id user-or-id)]
- (concat
- ;; Current User always gets readwrite perms for their Personal Collection and for its descendants! (1 DB
- ;; Call)
- (map permissions.path/collection-readwrite-path
- ((requiring-resolve 'metabase.collections.models.collection/user->personal-collection-and-descendant-ids)
- user-or-id))
- ;; Current User always gets readwrite perms for their Tenant Collection and for its descendants! (3 DB Calls)
- (map permissions.path/collection-readwrite-path
- (user->tenant-collection-and-descendant-ids user-or-id))
- ;; Current User always gets read perms for Transforms if they are an analyst (1 DB Call)
- (when (or (data-perms/is-data-analyst? user-id) (data-perms/is-superuser? user-id))
- (concat ["/collection/namespace/transforms/root/"]
- (map permissions.path/collection-readwrite-path ((requiring-resolve 'metabase.collections.models.collection/collections-in-namespace)
- :transforms))))
- ;; include the other Perms entries for any Group this User is in (1 DB Call)
- (map :object (app-db/query {:select [:p.object]
- :from [[:permissions_group_membership :pgm]]
- :join [[:permissions_group :pg] [:= :pgm.group_id :pg.id]
- [:permissions :p] [:= :p.group_id :pg.id]]
- :where [:= :pgm.user_id user-id]})))))]
- ;; Append permissions as a vector for more efficient iteration in checks that go over each permission linearly.
- (with-meta s {:as-vec (vec s)}))))
+ (set (when-let [user-id (u/the-id user-or-id)]
+ (concat
+ ;; Current User always gets readwrite perms for their Personal Collection and for its descendants! (1 DB
+ ;; Call)
+ (map permissions.path/collection-readwrite-path
+ ((requiring-resolve 'metabase.collections.models.collection/user->personal-collection-and-descendant-ids)
+ user-or-id))
+ ;; Current User always gets readwrite perms for their Tenant Collection and for its descendants! (3 DB Calls)
+ (map permissions.path/collection-readwrite-path
+ (user->tenant-collection-and-descendant-ids user-or-id))
+ ;; Current User always gets read perms for Transforms if they are an analyst (1 DB Call)
+ (when (or (data-perms/is-data-analyst? user-id) (data-perms/is-superuser? user-id))
+ (concat ["/collection/namespace/transforms/root/"]
+ (map permissions.path/collection-readwrite-path ((requiring-resolve 'metabase.collections.models.collection/collections-in-namespace)
+ :transforms))))
+ ;; include the other Perms entries for any Group this User is in (1 DB Call)
+ (map :object (app-db/query {:select [:p.object]
+ :from [[:permissions_group_membership :pgm]]
+ :join [[:permissions_group :pg] [:= :pgm.group_id :pg.id]
+ [:permissions :p] [:= :p.group_id :pg.id]]
+ :where [:= :pgm.user_id user-id]})))))))
(defn query-creation-capabilities
"Returns a map with `:can-create-queries` and `:can-create-native-queries` for the given user,
diff --git a/test/metabase/permissions/models/permissions_test.clj b/test/metabase/permissions/models/permissions_test.clj
index bb1dd273e3f..6d1b34c71f9 100644
--- a/test/metabase/permissions/models/permissions_test.clj
+++ b/test/metabase/permissions/models/permissions_test.clj
@@ -61,6 +61,29 @@
#{"/db/3/" "/db/2/"} "/db/1/schema/public/table/2/"
#{"/db/3/schema/public/" "/db/2/schema/public/"} "/db/1/schema/public/table/2/"))
+(deftest ^:parallel set-has-full-permissions?-path-boundary-test
+ (testing "a granted path only grants paths below it, never one that merely shares a textual prefix"
+ (are [perms path] (perms/set-has-full-permissions? perms path)
+ ;; a schema whose name contains a slash is escaped as `\/`, and still grants its tables
+ #{"/db/1/schema/a\\/b/"} "/db/1/schema/a\\/b/table/2/"
+ ;; an empty schema name is a real (empty) path segment
+ #{"/db/1/schema//"} "/db/1/schema//table/2/"
+ #{"/collection/1/"} "/collection/1/read/")
+ (are [perms path] (not (perms/set-has-full-permissions? perms path))
+ ;; IDs must match whole segments: collection 1 is not an ancestor of collection 12
+ #{"/collection/1/"} "/collection/12/"
+ #{"/db/1/"} "/db/12/"
+ #{"/db/1/schema/a/"} "/db/1/schema/ab/"
+ ;; `a` is not the escaped-slash schema `a/b`
+ #{"/db/1/schema/a/"} "/db/1/schema/a\\/b/table/2/"
+ ;; an empty schema name is not an ancestor of a named one
+ #{"/db/1/schema//"} "/db/1/schema/public/table/2/"))
+ (testing "paths that do not end in a slash are matched exactly"
+ ;; `perms-objects-set` returns this sentinel for instances that remote sync makes read-only, so it must
+ ;; not be granted by any real permission
+ (is (perms/set-has-full-permissions? #{"___no-remote-sync-access"} "___no-remote-sync-access"))
+ (is (not (perms/set-has-full-permissions? #{"/"} "___no-remote-sync-access")))))
+
(deftest ^:parallel set-has-application-permission-of-type?-test
(are [perms perms-type] (perms/set-has-application-permission-of-type? perms perms-type)
#{"/"} :subscription