Skip to content

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

feat: enforce the /project route ACL on the backend (#3315)

Tianzhou (天舟)297860fc7d2d
diff --git a/server/acl.go b/server/acl.go
index 5cb884eab8..340b57cd2b 100644
--- a/server/acl.go
+++ b/server/acl.go
@@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"net/url"
+ "regexp"
"strconv"
"strings"
@@ -25,23 +26,52 @@ func getRoleContextKey() string {
return roleContextKey
}
-func enforceWorkspaceDeveloperProjectACL(path string, method string, quaryParams url.Values, principalID int) *echo.HTTPError {
- if strings.HasPrefix(path, "/project") {
+var projectGeneralRouteRegex = regexp.MustCompile(`^/project/(?P<projectID>\d+)`)
+var projectMemberRouteRegex = regexp.MustCompile(`^/project/(?P<projectID>\d+)/member`)
+
+func enforceWorkspaceDeveloperProjectRouteACL(plan api.PlanType, path string, method string, quaryParams url.Values, principalID int, roleFinder func(projectID int, principalID int) (common.ProjectRole, error)) *echo.HTTPError {
+ var projectID int
+ var permission api.ProjectPermissionType
+ if method == "GET" {
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"))
- }
+ 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"))
+ }
+ }
+ // For /project/xxx subroutes, since all projects are public, we don't enforce ACL.
+ } else {
+ if matches := projectMemberRouteRegex.FindStringSubmatch(path); matches != nil {
+ projectID, _ = strconv.Atoi(matches[1])
+ permission = api.ProjectPermissionManageMember
+ } else if matches := projectGeneralRouteRegex.FindStringSubmatch(path); matches != nil {
+ projectID, _ = strconv.Atoi(matches[1])
+ permission = api.ProjectPermissionManageGeneral
}
}
+
+ if projectID > 0 {
+ role, err := roleFinder(projectID, principalID)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to process authorize request.").SetInternal(err)
+ }
+
+ if role == "" {
+ return echo.NewHTTPError(http.StatusUnauthorized).SetInternal(
+ errors.Errorf("rejected by the project ACL policy; %s %s u%d/non-member", method, path, principalID))
+ }
+
+ if !api.ProjectPermission(permission, plan, role) {
+ return echo.NewHTTPError(http.StatusUnauthorized).SetInternal(
+ errors.Errorf("rejected by the project ACL policy; %s %s u%d/%s", method, path, principalID, role))
+ }
+ }
+
return nil
}
@@ -122,9 +152,24 @@ func aclMiddleware(s *Server, ce *casbin.Enforcer, next echo.HandlerFunc, readon
// 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
+ var aclErr *echo.HTTPError
+ roleFinder := func(projectID int, principalID int) (common.ProjectRole, error) {
+ memberList, err := s.store.FindProjectMember(ctx, &api.ProjectMemberFind{ProjectID: &projectID, PrincipalID: &principalID})
+ if err != nil {
+ return "", err
+ }
+ if len(memberList) > 0 {
+ return common.ProjectRole(memberList[0].Role), nil
+ }
+ return "", nil
+ }
+
+ if strings.HasPrefix(path, "/project") {
+ aclErr = enforceWorkspaceDeveloperProjectRouteACL(s.getEffectivePlan(), path, method, c.QueryParams(), principalID, roleFinder)
+ }
+
+ if aclErr != nil {
+ return aclErr
}
}
diff --git a/server/acl_test.go b/server/acl_test.go
index 0a19f97f08..0aa013a77e 100644
--- a/server/acl_test.go
+++ b/server/acl_test.go
@@ -3,11 +3,37 @@ package server
import (
"net/url"
"testing"
+
+ "github.com/bytebase/bytebase/api"
+ "github.com/bytebase/bytebase/common"
)
-func TestEnforceWorkspaceDeveloperProjectACL(t *testing.T) {
+var roleFinder = func(projectID int, principalID int) (common.ProjectRole, error) {
+ switch projectID {
+ case 100:
+ switch principalID {
+ case 200:
+ return common.ProjectOwner, nil
+ case 201:
+ return common.ProjectDeveloper, nil
+ }
+ case 101:
+ switch principalID {
+ case 200:
+ return "", nil
+ case 201:
+ return "", nil
+ case 202:
+ return common.ProjectOwner, nil
+ }
+ }
+ return "", nil
+}
+
+func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {
type test struct {
desc string
+ plan api.PlanType
path string
method string
queryParams url.Values
@@ -16,35 +42,262 @@ func TestEnforceWorkspaceDeveloperProjectACL(t *testing.T) {
}
tests := []test{
+ {
+ desc: "Create a project",
+ plan: api.ENTERPRISE,
+ path: "/project",
+ method: "POST",
+ principalID: 200,
+ errMsg: "",
+ },
{
desc: "Fetch all projects",
+ plan: api.ENTERPRISE,
path: "/project",
method: "GET",
queryParams: url.Values{},
- principalID: 123,
- errMsg: "Not allowed to fetch all project list",
+ principalID: 200,
+ errMsg: "not allowed to fetch all project list",
},
{
- desc: "Fetch projects from other user",
+ desc: "Fetch all projects from other user",
+ plan: api.ENTERPRISE,
path: "/project",
method: "GET",
- queryParams: url.Values{"user": []string{"124"}},
- principalID: 123,
- errMsg: "Not allowed to fetch projects from other user",
+ queryParams: url.Values{"user": []string{"201"}},
+ principalID: 200,
+ errMsg: "not allowed to fetch projects from other user",
},
{
- desc: "Fetch projects from themselves",
+ desc: "Fetch all projects from themselves",
+ plan: api.ENTERPRISE,
path: "/project",
method: "GET",
- queryParams: url.Values{"user": []string{"123"}},
- principalID: 123,
+ queryParams: url.Values{"user": []string{"200"}},
+ principalID: 200,
errMsg: "",
},
+ {
+ desc: "Fetch a single project as a project owner",
+ plan: api.ENTERPRISE,
+ path: "/project/100",
+ method: "GET",
+ principalID: 200,
+ errMsg: "",
+ },
+ {
+ desc: "Fetch a single project as a project developer",
+ plan: api.ENTERPRISE,
+ path: "/project/100",
+ method: "GET",
+ principalID: 201,
+ errMsg: "",
+ },
+ {
+ desc: "Fetch a single project as a non-member",
+ plan: api.ENTERPRISE,
+ path: "/project/100",
+ method: "GET",
+ principalID: 202,
+ errMsg: "",
+ },
+ {
+ desc: "Patch a single project as a project owner",
+ plan: api.ENTERPRISE,
+ path: "/project/100",
+ method: "PATCH",
+ principalID: 200,
+ errMsg: "",
+ },
+ {
+ desc: "Patch a single project as a project developer",
+ plan: api.ENTERPRISE,
+ path: "/project/100",
+ method: "PATCH",
+ principalID: 201,
+ errMsg: "rejected by the project ACL policy; PATCH /project/100 u201/DEVELOPER",
+ },
+ {
+ desc: "Patch a single project as a non-member",
+ plan: api.ENTERPRISE,
+ path: "/project/100",
+ method: "PATCH",
+ principalID: 202,
+ errMsg: "rejected by the project ACL policy; PATCH /project/100 u202/non-member",
+ },
+ {
+ desc: "Fetch subroute from a single project as a project owner",
+ plan: api.ENTERPRISE,
+ path: "/project/100/repository",
+ method: "GET",
+ principalID: 200,
+ errMsg: "",
+ },
+ {
+ desc: "Fetch subroute from a single project as a project developer",
+ plan: api.ENTERPRISE,
+ path: "/project/100/repository",
+ method: "GET",
+ principalID: 201,
+ errMsg: "",
+ },
+ {
+ desc: "Fetch subroute from a single project as a non-member",
+ plan: api.ENTERPRISE,
+ path: "/project/100/repository",
+ method: "GET",
+ principalID: 202,
+ errMsg: "",
+ },
+ {
+ desc: "Create subroute from a single project as a project owner",
+ plan: api.ENTERPRISE,
+ path: "/project/100/repository",
+ method: "POST",
+ principalID: 200,
+ errMsg: "",
+ },
+ {
+ desc: "Create subroute from a single project as a project developer",
+ plan: api.ENTERPRISE,
+ path: "/project/100/repository",
+ method: "POST",
+ principalID: 201,
+ errMsg: "rejected by the project ACL policy; POST /project/100/repository u201/DEVELOPER",
+ },
+ {
+ desc: "Create subroute from a single project as a non member",
+ plan: api.ENTERPRISE,
+ path: "/project/100/repository",
+ method: "POST",
+ principalID: 202,
+ errMsg: "rejected by the project ACL policy; POST /project/100/repository u202/non-member",
+ },
+ {
+ desc: "Patch subroute from a single project as a project owner",
+ plan: api.ENTERPRISE,
+ path: "/project/100/repository",
+ method: "PATCH",
+ principalID: 200,
+ errMsg: "",
+ },
+ {
+ desc: "Patch subroute from a single project as a project developer",
+ plan: api.ENTERPRISE,
+ path: "/project/100/repository",
+ method: "PATCH",
+ principalID: 201,
+ errMsg: "rejected by the project ACL policy; PATCH /project/100/repository u201/DEVELOPER",
+ },
+ {
+ desc: "Patch subroute from a single project as a non member",
+ plan: api.ENTERPRISE,
+ path: "/project/100/repository",
+ method: "PATCH",
+ principalID: 202,
+ errMsg: "rejected by the project ACL policy; PATCH /project/100/repository u202/non-member",
+ },
+ {
+ desc: "Delete subroute from a single project as a project owner",
+ plan: api.ENTERPRISE,
+ path: "/project/100/repository",
+ method: "DELETE",
+ principalID: 200,
+ errMsg: "",
+ },
+ {
+ desc: "Delete subroute from a single project as a project developer",
+ plan: api.ENTERPRISE,
+ path: "/project/100/repository",
+ method: "DELETE",
+ principalID: 201,
+ errMsg: "rejected by the project ACL policy; DELETE /project/100/repository u201/DEVELOPER",
+ },
+ {
+ desc: "Delete subroute from a single project as a non member",
+ plan: api.ENTERPRISE,
+ path: "/project/100/repository",
+ method: "DELETE",
+ principalID: 202,
+ errMsg: "rejected by the project ACL policy; DELETE /project/100/repository u202/non-member",
+ },
+ {
+ desc: "Create member from a single project as a project owner",
+ plan: api.ENTERPRISE,
+ path: "/project/100/member",
+ method: "POST",
+ principalID: 200,
+ errMsg: "",
+ },
+ {
+ desc: "Create member from a single project as a project developer",
+ plan: api.ENTERPRISE,
+ path: "/project/100/member",
+ method: "POST",
+ principalID: 201,
+ errMsg: "rejected by the project ACL policy; POST /project/100/member u201/DEVELOPER",
+ },
+ {
+ desc: "Create member from a single project as a non-member",
+ plan: api.ENTERPRISE,
+ path: "/project/100/member",
+ method: "POST",
+ principalID: 202,
+ errMsg: "rejected by the project ACL policy; POST /project/100/member u202/non-member",
+ },
+ {
+ desc: "PATCH member from a single project as a project owner",
+ plan: api.ENTERPRISE,
+ path: "/project/100/member/567",
+ method: "PATCH",
+ principalID: 200,
+ errMsg: "",
+ },
+ {
+ desc: "PATCH member from a single project as a project developer",
+ plan: api.ENTERPRISE,
+ path: "/project/100/member/567",
+ method: "PATCH",
+ principalID: 201,
+ errMsg: "rejected by the project ACL policy; PATCH /project/100/member/567 u201/DEVELOPER",
+ },
+ {
+ desc: "PATCH member from a single project as a non-member",
+ plan: api.ENTERPRISE,
+ path: "/project/100/member/567",
+ method: "PATCH",
+ principalID: 202,
+ errMsg: "rejected by the project ACL policy; PATCH /project/100/member/567 u202/non-member",
+ },
+ {
+ desc: "DELETE member from a single project as a project owner",
+ plan: api.ENTERPRISE,
+ path: "/project/100/member/567",
+ method: "DELETE",
+ principalID: 200,
+ errMsg: "",
+ },
+ {
+ desc: "DELETE member from a single project as a project developer",
+ plan: api.ENTERPRISE,
+ path: "/project/100/member/567",
+ method: "DELETE",
+ principalID: 201,
+ errMsg: "rejected by the project ACL policy; DELETE /project/100/member/567 u201/DEVELOPER",
+ },
+ {
+ desc: "DELETE member from a single project as a non-member",
+ plan: api.ENTERPRISE,
+ path: "/project/100/member/567",
+ method: "DELETE",
+ principalID: 202,
+ errMsg: "rejected by the project ACL policy; DELETE /project/100/member/567 u202/non-member",
+ },
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
- err := enforceWorkspaceDeveloperProjectACL(tc.path, tc.method, tc.queryParams, tc.principalID)
+ err := enforceWorkspaceDeveloperProjectRouteACL(tc.plan, tc.path, tc.method, tc.queryParams, tc.principalID, roleFinder)
if err != nil {
if tc.errMsg == "" {
t.Errorf("expect no error, got %s", err.Internal.Error())