Skip to content

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

fix(BAC): avoid workspace developer operating the issue in some cases (#5209)

h3n4l182592991b1b
diff --git a/backend/server/acl.go b/backend/server/acl.go
index 9c36d09a31..dcc711cf4c 100644
--- a/backend/server/acl.go
+++ b/backend/server/acl.go
@@ -3,6 +3,7 @@ package server
import (
"context"
"fmt"
+ "io"
"net/http"
"net/url"
"regexp"
@@ -10,6 +11,7 @@ import (
"strings"
"github.com/casbin/casbin/v2"
+ "github.com/google/jsonapi"
"github.com/pkg/errors"
"github.com/labstack/echo/v4"
@@ -188,6 +190,85 @@ func enforceWorkspaceDeveloperSheetRouteACL(plan api.PlanType, path string, meth
return nil
}
+var issueStatusRegex = regexp.MustCompile(`^/issue/(?P<issueID>\d+)/status`)
+var issueRouteRegex = regexp.MustCompile(`^/issue/(?P<issueID>\d+)$`)
+
+func enforceWorkspaceDeveloperIssueRouteACL(path string, method string, body string, queryParams url.Values, principalID int, getIssueProjectID func(issueID int) (int, error), getProjectMemberIDs func(projectID int) ([]int, error)) *echo.HTTPError {
+ switch method {
+ case http.MethodGet:
+ // For /issue route, require the caller principal to be the same as the user in the query.
+ if userStr := queryParams.Get("user"); userStr != "" {
+ userID, err := strconv.Atoi(userStr)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Invalid user ID").SetInternal(err)
+ }
+ if principalID != userID {
+ return echo.NewHTTPError(http.StatusUnauthorized, "not allowed to list other users' issues")
+ }
+ } else if matches := issueRouteRegex.FindStringSubmatch(path); len(matches) > 0 {
+ issueIDStr := matches[1]
+ issueID, err := strconv.Atoi(issueIDStr)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Invalid issue ID").SetInternal(err)
+ }
+ projectID, err := getIssueProjectID(issueID)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to process authorize request.").SetInternal(err)
+ }
+ memberIDs, err := getProjectMemberIDs(projectID)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to process authorize request.").SetInternal(err)
+ }
+ for _, memberID := range memberIDs {
+ if memberID == principalID {
+ return nil
+ }
+ }
+ return echo.NewHTTPError(http.StatusUnauthorized, "not allowed to retrieve issue that is in the project that the user is not a member of")
+ }
+ case http.MethodPatch:
+ // Workspace developer can only operating the issues if the user is the member of the project that the issue belongs to.
+ if matches := issueStatusRegex.FindStringSubmatch(path); len(matches) > 0 {
+ issueIDStr := matches[1]
+ issueID, err := strconv.Atoi(issueIDStr)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Invalid issue ID").SetInternal(err)
+ }
+ projectID, err := getIssueProjectID(issueID)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to process authorize request.").SetInternal(err)
+ }
+ memberIDs, err := getProjectMemberIDs(projectID)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to process authorize request.").SetInternal(err)
+ }
+ for _, memberID := range memberIDs {
+ if memberID == principalID {
+ return nil
+ }
+ }
+ return echo.NewHTTPError(http.StatusUnauthorized, "not allowed to operate the issue")
+ }
+ case http.MethodPost:
+ // Workspace developer can only create issue under the project that the user is the member of.
+ var issueCreate api.IssueCreate
+ if err := jsonapi.UnmarshalPayload(strings.NewReader(body), &issueCreate); err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Malformed create issue request").SetInternal(err)
+ }
+ memberIDs, err := getProjectMemberIDs(issueCreate.ProjectID)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to process authorize request.").SetInternal(err)
+ }
+ for _, memberID := range memberIDs {
+ if memberID == principalID {
+ return nil
+ }
+ }
+ return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("not allowed to create issue under the project %d", issueCreate.ProjectID))
+ }
+ return nil
+}
+
func aclMiddleware(s *Server, pathPrefix string, ce *casbin.Enforcer, next echo.HandlerFunc, readonly bool) echo.HandlerFunc {
return func(c echo.Context) error {
ctx := c.Request().Context()
@@ -334,6 +415,15 @@ func aclMiddleware(s *Server, pathPrefix string, ce *casbin.Enforcer, next echo.
return false, nil
}
aclErr = enforceWorkspaceDeveloperDatabaseRouteACL(path, method, principalID, isMemberOfAnyProjectOwnsDatabase)
+ } else if strings.HasPrefix(path, "/issue") {
+ var bodyCopy strings.Builder
+ if _, err := io.Copy(&bodyCopy, c.Request().Body); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to process authorize request.").SetInternal(err)
+ }
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to process authorize request.").SetInternal(err)
+ }
+ aclErr = enforceWorkspaceDeveloperIssueRouteACL(path, method, bodyCopy.String(), c.QueryParams(), principalID, getRetrieveIssueProjectID(ctx, s.store), getRetrieveProjectMemberIDs(ctx, s.store))
}
if aclErr != nil {
return aclErr
@@ -347,6 +437,42 @@ func aclMiddleware(s *Server, pathPrefix string, ce *casbin.Enforcer, next echo.
}
}
+func getRetrieveIssueProjectID(ctx context.Context, s *store.Store) func(issueID int) (int, error) {
+ return func(issueID int) (int, error) {
+ issue, err := s.GetIssueV2(ctx, &store.FindIssueMessage{
+ UID: &issueID,
+ })
+ if err != nil {
+ return 0, err
+ }
+ if issue == nil {
+ return 0, errors.Errorf("cannot find issue %d", issueID)
+ }
+ return issue.Project.UID, nil
+ }
+}
+
+func getRetrieveProjectMemberIDs(ctx context.Context, s *store.Store) func(projectID int) ([]int, error) {
+ return func(projectID int) ([]int, error) {
+ policy, err := s.GetProjectPolicy(ctx, &store.GetProjectPolicyMessage{
+ UID: &projectID,
+ })
+ if err != nil {
+ return nil, err
+ }
+ if policy == nil {
+ return nil, errors.Errorf("cannot find policy for project %d", projectID)
+ }
+ var memberIDs []int
+ for _, binding := range policy.Bindings {
+ for _, member := range binding.Members {
+ memberIDs = append(memberIDs, member.ID)
+ }
+ }
+ return memberIDs, nil
+ }
+}
+
func isOperatingSelf(ctx context.Context, c echo.Context, s *Server, curPrincipalID int, method string, path string) (bool, error) {
switch method {
case http.MethodGet:
diff --git a/backend/server/acl_issue_test.go b/backend/server/acl_issue_test.go
new file mode 100644
index 0000000000..7f8f2b4a68
--- /dev/null
+++ b/backend/server/acl_issue_test.go
@@ -0,0 +1,157 @@
+package server
+
+import (
+ "net/url"
+ "testing"
+)
+
+func TestWorkspaceDeveloperIssueRouteACL_RetrieveIssue(t *testing.T) {
+ type test struct {
+ desc string
+ path string
+ method string
+ queryParams url.Values
+ principalID int
+ body string
+ errMsg string
+ }
+
+ tests := []test{
+ {
+ desc: "Retrieve other users' issue",
+ path: "/issue?user=201",
+ queryParams: url.Values{"user": []string{"201"}},
+ method: "GET",
+ principalID: 202,
+ errMsg: "not allowed to list other users' issues",
+ },
+ {
+ desc: "Retrieve my issue",
+ path: "/issue?user=202",
+ queryParams: url.Values{"user": []string{"202"}},
+ method: "GET",
+ principalID: 202,
+ errMsg: "",
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.desc, func(t *testing.T) {
+ err := enforceWorkspaceDeveloperIssueRouteACL(tc.path, tc.method, tc.body, tc.queryParams, tc.principalID, mockGetIssueProjectID, mockGetProjectMembers)
+ if err != nil {
+ if tc.errMsg == "" {
+ t.Errorf("expect no error, got %s", err.Message)
+ } else if tc.errMsg != err.Message {
+ t.Errorf("expect error %s, got %s", tc.errMsg, err.Message)
+ }
+ } else if tc.errMsg != "" {
+ t.Errorf("expect error %s, got no error", tc.errMsg)
+ }
+ })
+ }
+}
+
+func TestWorkspaceDeveloperIssueRouteACL_OperateIssue(t *testing.T) {
+ type test struct {
+ desc string
+ path string
+ method string
+ body string
+ queryParams url.Values
+ principalID int
+ errMsg string
+ }
+
+ tests := []test{
+ {
+ desc: "Operating the issue created by other user in other projects",
+ path: "/issue/403/status",
+ body: "",
+ queryParams: url.Values{},
+ method: "PATCH",
+ principalID: 202,
+ errMsg: "not allowed to operate the issue",
+ },
+ {
+ desc: "Operating the issue I created",
+ path: "/issue/401/status",
+ body: "",
+ queryParams: url.Values{},
+ method: "PATCH",
+ principalID: 202,
+ errMsg: "",
+ },
+ {
+ desc: "Operating the issue created by other user in projects I am a member of",
+ path: "/issue/402/status",
+ body: "",
+ queryParams: url.Values{},
+ method: "PATCH",
+ principalID: 202,
+ errMsg: "",
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.desc, func(t *testing.T) {
+ err := enforceWorkspaceDeveloperIssueRouteACL(tc.path, tc.method, tc.body, tc.queryParams, tc.principalID, mockGetIssueProjectID, mockGetProjectMembers)
+ if err != nil {
+ if tc.errMsg == "" {
+ t.Errorf("expect no error, got %s", err.Message)
+ } else if tc.errMsg != err.Message {
+ t.Errorf("expect error %s, got %s", tc.errMsg, err.Message)
+ }
+ } else if tc.errMsg != "" {
+ t.Errorf("expect error %s, got no error", tc.errMsg)
+ }
+ })
+ }
+}
+
+func TestWorkspaceDeveloperIssueRouteACL_CreateIssue(t *testing.T) {
+ type test struct {
+ desc string
+ path string
+ method string
+ body string
+ queryParams url.Values
+ principalID int
+ errMsg string
+ }
+
+ tests := []test{
+ {
+ desc: "Create issue under project I am not a member of",
+ path: "/issue",
+ body: `{"data":{"type":"issue","attributes":{"title":"test","description":"test","projectId":103}}}`,
+ queryParams: url.Values{},
+ method: "POST",
+ principalID: 202,
+ errMsg: "not allowed to create issue under the project 103",
+ },
+ {
+ desc: "Create issue under project I am a member of",
+ path: "/issue",
+ body: `{"data":{"type":"issue","attributes":{"title":"test","description":"test","projectId":102}}}`,
+ queryParams: url.Values{},
+ method: "POST",
+ principalID: 202,
+ errMsg: "",
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.desc, func(t *testing.T) {
+ err := enforceWorkspaceDeveloperIssueRouteACL(tc.path, tc.method, tc.body, tc.queryParams, tc.principalID, mockGetIssueProjectID, mockGetProjectMembers)
+ if err != nil {
+ if tc.errMsg == "" {
+ t.Errorf("expect no error, got %s", err.Message)
+ } else if tc.errMsg != err.Message {
+ t.Errorf("expect error %s, got %s", tc.errMsg, err.Message)
+ }
+ } else if tc.errMsg != "" {
+ t.Errorf("expect error %s, got no error", tc.errMsg)
+ }
+ })
+ }
+}
diff --git a/backend/server/acl_test_helper.go b/backend/server/acl_test_helper.go
index 93545104bc..5f4e85d975 100644
--- a/backend/server/acl_test_helper.go
+++ b/backend/server/acl_test_helper.go
@@ -1,6 +1,8 @@
package server
import (
+ "github.com/pkg/errors"
+
"github.com/bytebase/bytebase/backend/common"
api "github.com/bytebase/bytebase/backend/legacyapi"
)
@@ -169,3 +171,46 @@ var isMemberOfAnyProjectOwnsDatabase = func(principalID int, databaseID int) (bo
}
return false, nil
}
+
+var testMemberIssueHelper = struct {
+ // principalIDToProjectID is a map from principal ID to the project ID.
+ // We assume that a principal can only be a member of one project (actually it can be a member of multiple projects).
+ principalIDToProjectID map[int]int
+ // issueIDToProjectID is a map from issue ID to the project ID.
+ issueIDToProjectID map[int]int
+}{
+ principalIDToProjectID: map[int]int{
+ // User 202 is a member of project 102.
+ 202: 102,
+ // User 203 is a member of project 102.
+ 203: 102,
+ // User 203 is a member of project 103.
+ 204: 103,
+ },
+ issueIDToProjectID: map[int]int{
+ // Issue 401 belongs to project 102.
+ 401: 102,
+ // Issue 402 belongs to project 102.
+ 402: 102,
+ // Issue 403 belongs to project 103.
+ 403: 103,
+ },
+}
+
+func mockGetIssueProjectID(issueID int) (int, error) {
+ projectID, ok := testMemberIssueHelper.issueIDToProjectID[issueID]
+ if !ok {
+ return 0, errors.Errorf("issue %d does not belong to any project", issueID)
+ }
+ return projectID, nil
+}
+
+func mockGetProjectMembers(projectID int) ([]int, error) {
+ var memberIDs []int
+ for principalID, id := range testMemberIssueHelper.principalIDToProjectID {
+ if id == projectID {
+ memberIDs = append(memberIDs, principalID)
+ }
+ }
+ return memberIDs, nil
+}