Skip to content

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

refactor: move the GET /project ACL check to acl.go (#3270)

Tianzhou (天舟)672de30a00bb
diff --git a/server/acl.go b/server/acl.go
index d8112008ca..5cb884eab8 100644
--- a/server/acl.go
+++ b/server/acl.go
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
+ "net/url"
"strconv"
"strings"
@@ -24,6 +25,26 @@ func getRoleContextKey() string {
return roleContextKey
}
+func enforceWorkspaceDeveloperProjectACL(path string, method string, quaryParams url.Values, principalID int) *echo.HTTPError {
+ if strings.HasPrefix(path, "/project") {
+ if path == "/project" {
+ // Developer can only fetch projects from themseleves.
+ if method == "GET" {
+ userIDStr := quaryParams.Get("user")
+ if userIDStr == "" {
+ return echo.NewHTTPError(http.StatusUnauthorized).SetInternal(
+ errors.Errorf("Not allowed to fetch all project list"))
+ }
+ if strconv.Itoa(principalID) != userIDStr {
+ return echo.NewHTTPError(http.StatusUnauthorized).SetInternal(
+ errors.Errorf("Not allowed to fetch projects from other user"))
+ }
+ }
+ }
+ }
+ return nil
+}
+
func aclMiddleware(s *Server, ce *casbin.Enforcer, next echo.HandlerFunc, readonly bool) echo.HandlerFunc {
return func(c echo.Context) error {
ctx := c.Request().Context()
@@ -67,9 +88,9 @@ func aclMiddleware(s *Server, ce *casbin.Enforcer, next echo.HandlerFunc, readon
if !s.feature(api.FeatureRBAC) {
role = api.Owner
}
+
// Performs the ACL check.
pass, err := ce.Enforce(string(role), path, method)
-
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to process authorize request.").SetInternal(err)
}
@@ -98,6 +119,15 @@ func aclMiddleware(s *Server, ce *casbin.Enforcer, next echo.HandlerFunc, readon
errors.Errorf("rejected by the ACL policy; %s %s u%d/%s", method, path, principalID, role))
}
+ // Workspace Owner or DBA assumes project Owner role for all projects, so will
+ // pass any project ACL.
+ if role != api.Owner && role != api.DBA {
+ projectACLErr := enforceWorkspaceDeveloperProjectACL(path, method, c.QueryParams(), principalID)
+ if projectACLErr != nil {
+ return projectACLErr
+ }
+ }
+
// Stores role into context.
c.Set(getRoleContextKey(), role)
diff --git a/server/acl_test.go b/server/acl_test.go
new file mode 100644
index 0000000000..0a19f97f08
--- /dev/null
+++ b/server/acl_test.go
@@ -0,0 +1,59 @@
+package server
+
+import (
+ "net/url"
+ "testing"
+)
+
+func TestEnforceWorkspaceDeveloperProjectACL(t *testing.T) {
+ type test struct {
+ desc string
+ path string
+ method string
+ queryParams url.Values
+ principalID int
+ errMsg string
+ }
+
+ tests := []test{
+ {
+ desc: "Fetch all projects",
+ path: "/project",
+ method: "GET",
+ queryParams: url.Values{},
+ principalID: 123,
+ errMsg: "Not allowed to fetch all project list",
+ },
+ {
+ desc: "Fetch projects from other user",
+ path: "/project",
+ method: "GET",
+ queryParams: url.Values{"user": []string{"124"}},
+ principalID: 123,
+ errMsg: "Not allowed to fetch projects from other user",
+ },
+ {
+ desc: "Fetch projects from themselves",
+ path: "/project",
+ method: "GET",
+ queryParams: url.Values{"user": []string{"123"}},
+ principalID: 123,
+ errMsg: "",
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.desc, func(t *testing.T) {
+ err := enforceWorkspaceDeveloperProjectACL(tc.path, tc.method, tc.queryParams, tc.principalID)
+ if err != nil {
+ if tc.errMsg == "" {
+ t.Errorf("expect no error, got %s", err.Internal.Error())
+ } else if tc.errMsg != err.Internal.Error() {
+ t.Errorf("expect error %s, got %s", tc.errMsg, err.Internal.Error())
+ }
+ } else if tc.errMsg != "" {
+ t.Errorf("expect error %s, got no error", tc.errMsg)
+ }
+ })
+ }
+}
diff --git a/server/project.go b/server/project.go
index 484a616430..72ed522291 100644
--- a/server/project.go
+++ b/server/project.go
@@ -89,15 +89,6 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
projectFind.PrincipalID = &userID
}
- // Only Owner and DBA can fetch all projects from all users.
- // Developer can only fetch all projects from herself.
- if projectFind.PrincipalID == nil {
- role := c.Get(getRoleContextKey()).(api.Role)
- if role != api.Owner && role != api.DBA {
- return echo.NewHTTPError(http.StatusForbidden, "Not allowed to fetch all project list")
- }
- }
-
if rowStatusStr := c.QueryParam("rowstatus"); rowStatusStr != "" {
rowStatus := api.RowStatus(rowStatusStr)
projectFind.RowStatus = &rowStatus
@@ -108,11 +99,11 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
}
var activeProjectList []*api.Project
- // if principalID is passed, we will enable the filter logic
+ // If principalID is passed, We will filter those projects with the current principal having the role provider
+ // different from the project's current role provider.
if projectFind.PrincipalID != nil {
principalID := *projectFind.PrincipalID
for _, project := range projectList {
- // We will filter those project with the current principal as an inactive member (the role provider differs from that of the project)
roleProvider := project.RoleProvider
for _, projectMember := range project.ProjectMemberList {
if projectMember.PrincipalID == principalID && projectMember.RoleProvider == roleProvider {