Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
Introduce PATCH_SELF so that the policy can differentiate between changing self info VS changing other’s info
天舟9efa07f99e16
diff --git a/backend/server/acl.go b/backend/server/acl.goindex e9f958a364..28cc7a03f9 100644--- a/backend/server/acl.go+++ b/backend/server/acl.go@@ -4,6 +4,7 @@ import ("context""fmt""net/http"+ "strconv""strings""github.com/casbin/casbin/v2"@@ -33,16 +34,28 @@ func AclMiddleware(l *bytebase.Logger, m api.MemberService, ce *casbin.Enforcer,if bytebase.ErrorCode(err) == bytebase.ENOTFOUND {return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("User ID is not a member: %d", principalId))}- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to authorize.").SetInternal(err)+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to process authorize request.").SetInternal(err)}method := c.Request().Method+ // If the requests is trying to PATCH herself, we will change the method signature to+ // PATCH_SELF so that the policy can differentiate between PATCH and PATCH_SELF+ if method == "PATCH" {+ pathPrincipalId := c.Param("principalId")+ if pathPrincipalId != "" {+ if pathPrincipalId == strconv.Itoa(principalId) {+ method = "PATCH_SELF"+ }+ }+ }+path := strings.TrimPrefix(c.Request().URL.Path, "/api")+ // Performs the ACL check.pass, err := ce.Enforce(member.Role.String(), path, method)if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to authorize.").SetInternal(err)+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to process authorize request.").SetInternal(err)}if !pass {diff --git a/backend/server/acl_casbin_policy.csv b/backend/server/acl_casbin_policy.csvindex efb95159f1..a9d798365c 100644--- a/backend/server/acl_casbin_policy.csv+++ b/backend/server/acl_casbin_policy.csv@@ -2,6 +2,7 @@ p, OWNER, /principal, POSTp, OWNER, /principal, GETp, OWNER, /principal/{id}, GETp, OWNER, /principal/{id}, PATCH+p, OWNER, /principal/{id}, PATCH_SELFp, OWNER, /member, POSTp, OWNER, /member, GET@@ -11,6 +12,6 @@ p, OWNER, /member/{id}, DELETEp, DBA, /principal, GETp, DBA, /principal/{id}, GET-p, DBA, /principal/{id}, PATCH+p, DBA, /principal/{id}, PATCH_SELFp, DBA, /member, GET\ No newline at end of filediff --git a/backend/server/principal.go b/backend/server/principal.goindex e66e65b748..e98635b071 100644--- a/backend/server/principal.go+++ b/backend/server/principal.go@@ -56,8 +56,8 @@ func (s *Server) registerPrincipalRoutes(g *echo.Group) {return nil})- g.GET("/principal/:id", func(c echo.Context) error {- id, err := strconv.Atoi(c.Param("id"))+ g.GET("/principal/:principalId", func(c echo.Context) error {+ id, err := strconv.Atoi(c.Param("principalId"))if err != nil {return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("id"))).SetInternal(err)}@@ -82,8 +82,8 @@ func (s *Server) registerPrincipalRoutes(g *echo.Group) {return nil})- g.PATCH("/principal/:id", func(c echo.Context) error {- id, err := strconv.Atoi(c.Param("id"))+ g.PATCH("/principal/:principalId", func(c echo.Context) error {+ id, err := strconv.Atoi(c.Param("principalId"))if err != nil {return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("id"))).SetInternal(err)}