Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: tweak casbin _SELF ACL logic (#3192)

Tianzhou (天舟)6b54dd612de7
diff --git a/server/acl.go b/server/acl.go
index c3b4d12125..53fc31bdae 100644
--- a/server/acl.go
+++ b/server/acl.go
@@ -60,16 +60,6 @@ func aclMiddleware(s *Server, ce *casbin.Enforcer, next echo.HandlerFunc, readon
return echo.NewHTTPError(http.StatusUnauthorized, "This user has been deactivated by the admin")
}
- // If the request is trying to GET/PATCH/DELETE itself, we will change the method signature to
- // XXX_SELF so that the policy can differentiate between XXX and XXX_SELF
- if method == "GET" || method == "PATCH" || method == "DELETE" {
- if isSelf, err := isOperatingSelf(ctx, c, s, principalID, method); err != nil {
- return err
- } else if isSelf {
- method += "_SELF"
- }
- }
-
path := strings.TrimPrefix(c.Request().URL.Path, "/api")
role := member.Role
@@ -84,6 +74,26 @@ func aclMiddleware(s *Server, ce *casbin.Enforcer, next echo.HandlerFunc, readon
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to process authorize request.").SetInternal(err)
}
+ if !pass {
+ // If the request is trying to GET/PATCH/DELETE itself, we will change the method signature to
+ // XXX_SELF and try again. Because XXX is a superset of XXX_SELF, thus we only try XXX_SELF after
+ // XXX fails.
+ if method == "GET" || method == "PATCH" || method == "DELETE" {
+ if isSelf, err := isOperatingSelf(ctx, c, s, principalID, method); err != nil {
+ return err
+ } else if isSelf {
+ method += "_SELF"
+ }
+
+ // Performs the ACL check with _SELF.
+ pass, err = ce.Enforce(string(role), path, method)
+
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to process authorize request.").SetInternal(err)
+ }
+ }
+ }
+
if !pass {
return echo.NewHTTPError(http.StatusUnauthorized).SetInternal(
errors.Errorf("rejected by the ACL policy; %s %s u%d/%s", method, path, principalID, role))
diff --git a/server/acl_casbin_policy_dba.csv b/server/acl_casbin_policy_dba.csv
index e5eaaad47a..460fecde55 100644
--- a/server/acl_casbin_policy_dba.csv
+++ b/server/acl_casbin_policy_dba.csv
@@ -103,7 +103,6 @@ p, DBA, /sheet/shared, GET
p, DBA, /sheet/starred, GET
p, DBA, /sheet/{id}, GET
p, DBA, /sheet/{id}, PATCH
-p, DBA, /sheet/{id}, PATCH_SELF
p, DBA, /sheet/{id}, DELETE_SELF
p, DBA, /sheet/{id}/organizer, PATCH
p, DBA, /sheet/project/{projectID}/sync, POST
diff --git a/server/acl_casbin_policy_developer.csv b/server/acl_casbin_policy_developer.csv
index ab00b2d924..ba46b38e87 100644
--- a/server/acl_casbin_policy_developer.csv
+++ b/server/acl_casbin_policy_developer.csv
@@ -88,7 +88,6 @@ p, DEVELOPER, /sheet/shared, GET
p, DEVELOPER, /sheet/starred, GET
p, DEVELOPER, /sheet/{id}, GET
p, DEVELOPER, /sheet/{id}, PATCH
-p, DEVELOPER, /sheet/{id}, PATCH_SELF
p, DEVELOPER, /sheet/{id}, DELETE_SELF
p, DEVELOPER, /sheet/{id}/organizer, PATCH
p, DEVELOPER, /sheet/project/{projectID}/sync, POST
diff --git a/server/acl_casbin_policy_owner.csv b/server/acl_casbin_policy_owner.csv
index 537f363105..c8cd0244a5 100644
--- a/server/acl_casbin_policy_owner.csv
+++ b/server/acl_casbin_policy_owner.csv
@@ -2,7 +2,6 @@ p, OWNER, /principal, POST
p, OWNER, /principal, GET
p, OWNER, /principal/{id}, GET
p, OWNER, /principal/{id}, PATCH
-p, OWNER, /principal/{id}, PATCH_SELF
p, OWNER, /member, POST
p, OWNER, /member, GET
p, OWNER, /member/{id}, PATCH
@@ -109,7 +108,6 @@ p, OWNER, /sheet/shared, GET
p, OWNER, /sheet/starred, GET
p, OWNER, /sheet/{id}, GET
p, OWNER, /sheet/{id}, PATCH
-p, OWNER, /sheet/{id}, PATCH_SELF
p, OWNER, /sheet/{id}, DELETE_SELF
p, OWNER, /sheet/{id}/organizer, PATCH
p, OWNER, /sheet/project/{projectID}/sync, POST