Skip to content

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

fix: run issue approval filters in SQL, and four edge cases in the issue store queries (#21282)

Danny Xua747c31f8174
diff --git a/backend/api/v1/issue_service.go b/backend/api/v1/issue_service.go
index 92994456d1..1e11c46bb2 100644
--- a/backend/api/v1/issue_service.go
+++ b/backend/api/v1/issue_service.go
@@ -4,6 +4,8 @@ import (
"context"
"fmt"
"log/slog"
+ "maps"
+ "slices"
"strings"
"time"
@@ -39,12 +41,6 @@ type IssueService struct {
reviewWorkflow *review.Workflow
}
-type filterIssueMessage struct {
- ApprovalStatus *v1pb.ApprovalStatus
- // Approver is the user who can approve the issue.
- Approver *store.UserMessage
-}
-
// NewIssueService creates a new IssueService.
func NewIssueService(
store *store.Store,
@@ -76,13 +72,16 @@ func (s *IssueService) GetIssue(ctx context.Context, req *connect.Request[v1pb.G
return connect.NewResponse(issueV1), nil
}
+// getIssueFind translates the CEL filter into a store query. It returns the user
+// named by `current_approver`, if any: their roles depend on the projects being
+// searched, which the caller resolves afterwards via setNextApproverRoles.
func (s *IssueService) getIssueFind(
ctx context.Context,
filter string,
query string,
limit,
offset *int,
-) (*store.FindIssueMessage, *filterIssueMessage, error) {
+) (*store.FindIssueMessage, *store.UserMessage, error) {
issueFind := &store.FindIssueMessage{
Workspace: common.GetWorkspaceIDFromContext(ctx),
Limit: limit,
@@ -104,7 +103,7 @@ func (s *IssueService) getIssueFind(
return nil, nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("failed to parse filter %v, error: %v", filter, iss.String()))
}
- filterIssue := &filterIssueMessage{}
+ var approver *store.UserMessage
var parseFilter func(expr celast.Expr) (string, error)
parseFilter = func(expr celast.Expr) (string, error) {
@@ -136,14 +135,14 @@ func (s *IssueService) getIssueFind(
if !ok {
return "", connect.NewError(connect.CodeInvalidArgument, errors.Errorf(`invalid approval_status %q`, value))
}
- filterIssue.ApprovalStatus = new(v1pb.ApprovalStatus(approvalStatusValue))
+ issueFind.ApprovalStatus = new(v1pb.ApprovalStatus(approvalStatusValue).String())
case "current_approver", "creator":
user, err := s.getUserByIdentifier(ctx, value.(string))
if err != nil {
return "", connect.NewError(connect.CodeInternal, errors.Errorf("failed to get user %v with error %v", value, err.Error()))
}
if variable == "current_approver" {
- filterIssue.Approver = user
+ approver = user
} else {
issueFind.CreatorID = &user.Email
}
@@ -228,7 +227,48 @@ func (s *IssueService) getIssueFind(
if _, err := parseFilter(ast.NativeRep().Expr()); err != nil {
return nil, nil, connect.NewError(connect.CodeInvalidArgument, errors.Wrapf(err, "failed to parse filter"))
}
- return issueFind, filterIssue, nil
+ return issueFind, approver, nil
+}
+
+// setNextApproverRoles resolves the roles the approver holds in each project
+// being searched, so `current_approver` can be matched in SQL.
+func (s *IssueService) setNextApproverRoles(ctx context.Context, issueFind *store.FindIssueMessage, approver *store.UserMessage) error {
+ if approver == nil {
+ return nil
+ }
+ workspaceID := common.GetWorkspaceIDFromContext(ctx)
+ workspacePolicy, err := s.store.GetWorkspaceIamPolicy(ctx, workspaceID)
+ if err != nil {
+ return errors.Wrapf(err, "failed to get workspace iam policy")
+ }
+ // One query rather than one per project: the wildcard search authorizes
+ // every project the caller can read issues in, and a workspace-level
+ // grant reaches this loop without having read any project policy.
+ projectPolicies, err := s.store.ListProjectIamPolicies(ctx, workspaceID, issueFind.ProjectIDs)
+ if err != nil {
+ return errors.Wrapf(err, "failed to list project iam policies")
+ }
+ // Resolved once rather than per project. GetUserRolesInIamPolicy unions the
+ // policies it is handed, so roles(workspace ∪ project) is roles(workspace)
+ // ∪ roles(project) and splitting the two changes nothing — but expanding
+ // the workspace policy inside the loop re-runs its group lookups for every
+ // project, and HA runs with the store cache off (server.go passes
+ // !profile.HA), so each of those is a real query.
+ workspaceRoles := utils.GetUserFormattedRolesMap(ctx, s.store, workspaceID, approver, workspacePolicy.Policy)
+
+ projectRoles := []store.ProjectRole{}
+ for _, projectID := range issueFind.ProjectIDs {
+ roles := map[string]bool{}
+ maps.Copy(roles, workspaceRoles)
+ if projectPolicy, ok := projectPolicies[projectID]; ok {
+ maps.Copy(roles, utils.GetUserFormattedRolesMap(ctx, s.store, workspaceID, approver, projectPolicy))
+ }
+ for role := range roles {
+ projectRoles = append(projectRoles, store.ProjectRole{ProjectID: projectID, Role: role})
+ }
+ }
+ issueFind.NextApproverRoles = &projectRoles
+ return nil
}
func (s *IssueService) ListIssues(ctx context.Context, req *connect.Request[v1pb.ListIssuesRequest]) (*connect.Response[v1pb.ListIssuesResponse], error) {
@@ -251,12 +291,15 @@ func (s *IssueService) ListIssues(ctx context.Context, req *connect.Request[v1pb
}
limitPlusOne := offset.limit + 1
- issueFind, issueFilter, err := s.getIssueFind(ctx, req.Msg.Filter, req.Msg.Query, &limitPlusOne, &offset.offset)
+ issueFind, approver, err := s.getIssueFind(ctx, req.Msg.Filter, req.Msg.Query, &limitPlusOne, &offset.offset)
if err != nil {
return nil, err
}
issueFind.ProjectIDs = []string{projectID}
issueFind.ExcludeDraft = true
+ if err := s.setNextApproverRoles(ctx, issueFind, approver); err != nil {
+ return nil, connect.NewError(connect.CodeInternal, err)
+ }
orderByKeys, err := store.GetIssueOrders(req.Msg.OrderBy)
if err != nil {
@@ -277,7 +320,7 @@ func (s *IssueService) ListIssues(ctx context.Context, req *connect.Request[v1pb
issues = issues[:offset.limit]
}
- converted, err := s.convertToIssues(ctx, issues, issueFilter)
+ converted, err := s.convertToIssues(issues)
if err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to convert to issue"))
}
@@ -307,7 +350,7 @@ func (s *IssueService) SearchIssues(ctx context.Context, req *connect.Request[v1
}
limitPlusOne := offset.limit + 1
- issueFind, issueFilter, err := s.getIssueFind(ctx, req.Msg.Filter, req.Msg.Query, &limitPlusOne, &offset.offset)
+ issueFind, approver, err := s.getIssueFind(ctx, req.Msg.Filter, req.Msg.Query, &limitPlusOne, &offset.offset)
if err != nil {
return nil, err
}
@@ -361,6 +404,9 @@ func (s *IssueService) SearchIssues(ctx context.Context, req *connect.Request[v1
}
issueFind.ProjectIDs = projectIDs
issueFind.ExcludeDraft = true
+ if err := s.setNextApproverRoles(ctx, issueFind, approver); err != nil {
+ return nil, connect.NewError(connect.CodeInternal, err)
+ }
issues, err := s.store.ListIssues(ctx, issueFind)
if err != nil {
@@ -375,7 +421,7 @@ func (s *IssueService) SearchIssues(ctx context.Context, req *connect.Request[v1
issues = issues[:offset.limit]
}
- converted, err := s.convertToIssues(ctx, issues, issueFilter)
+ converted, err := s.convertToIssues(issues)
if err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to convert to issue"))
}
@@ -1120,6 +1166,9 @@ func (s *IssueService) BatchUpdateIssuesStatus(ctx context.Context, req *connect
issueUIDs = append(issueUIDs, issueUID)
}
+ // One comment per issue, and the store rejects a batch whose ids do not all
+ // resolve — so naming the same issue twice must collapse, not fail.
+ issueUIDs = slices.Compact(slices.Sorted(slices.Values(issueUIDs)))
// Get project early for webhooks.
project, err := s.store.GetProject(ctx, &store.FindProjectMessage{Workspace: common.GetWorkspaceIDFromContext(ctx), ResourceID: &projectID})
diff --git a/backend/api/v1/issue_service_converter.go b/backend/api/v1/issue_service_converter.go
index dc1defa309..7b812eea11 100644
--- a/backend/api/v1/issue_service_converter.go
+++ b/backend/api/v1/issue_service_converter.go
@@ -1,83 +1,31 @@
package v1
import (
- "context"
"fmt"
- "log/slog"
"github.com/pkg/errors"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/bytebase/bytebase/backend/common"
- "github.com/bytebase/bytebase/backend/common/log"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
"github.com/bytebase/bytebase/backend/store"
- "github.com/bytebase/bytebase/backend/utils"
)
-func (s *IssueService) convertToIssues(ctx context.Context, issues []*store.IssueMessage, issueFilter *filterIssueMessage) ([]*v1pb.Issue, error) {
+// convertToIssues converts a page the store has already filtered. Nothing may be
+// dropped here: the page token is minted from the row count the store returned.
+func (s *IssueService) convertToIssues(issues []*store.IssueMessage) ([]*v1pb.Issue, error) {
var converted []*v1pb.Issue
for _, issue := range issues {
v1Issue, err := s.convertToIssue(issue)
if err != nil {
return nil, errors.Wrapf(err, "failed to convert to issue")
}
- if v1Issue == nil {
- continue
- }
- if issueFilter != nil {
- if v := issueFilter.ApprovalStatus; v != nil && v1Issue.ApprovalStatus != *v {
- continue
- }
- projectID, _, err := common.GetProjectIDIssueUID(v1Issue.Name)
- if err != nil {
- slog.Error("failed to parse the issue name", log.BBError(err), slog.String("issue", v1Issue.Name))
- continue
- }
- if v := issueFilter.Approver; v != nil && !s.isIssueNextApprover(ctx, v1Issue, projectID, v) {
- continue
- }
- }
converted = append(converted, v1Issue)
}
return converted, nil
}
-func (s *IssueService) getUserRoleMap(ctx context.Context, projectResourceID string, user *store.UserMessage) map[string]bool {
- if user == nil {
- return map[string]bool{}
- }
-
- policy, err := s.store.GetProjectIamPolicy(ctx, common.GetWorkspaceIDFromContext(ctx), projectResourceID)
- if err != nil {
- slog.Error("failed to get project iam policy", log.BBError(err), slog.String("project", projectResourceID))
- return map[string]bool{}
- }
- workspacePolicy, err := s.store.GetWorkspaceIamPolicy(ctx, common.GetWorkspaceIDFromContext(ctx))
- if err != nil {
- slog.Error("failed to get workspace iam policy", log.BBError(err))
- return map[string]bool{}
- }
-
- return utils.GetUserFormattedRolesMap(ctx, s.store, common.GetWorkspaceIDFromContext(ctx), user, policy.Policy, workspacePolicy.Policy)
-}
-
-func (s *IssueService) isIssueNextApprover(ctx context.Context, issue *v1pb.Issue, projectResourceID string, user *store.UserMessage) bool {
- if user == nil {
- return false
- }
-
- roles := s.getUserRoleMap(ctx, projectResourceID, user)
- approvalRoles := issue.GetApprovalTemplate().GetFlow().GetRoles()
- index := len(issue.Approvers)
- if index >= len(approvalRoles) {
- return false
- }
-
- return roles[approvalRoles[index]]
-}
-
// nolint:unparam
func (*IssueService) convertToIssue(issue *store.IssueMessage) (*v1pb.Issue, error) {
issuePayload := issue.Payload
diff --git a/backend/api/v1/issue_service_test.go b/backend/api/v1/issue_service_test.go
index a337eed9c4..40bc5637fb 100644
--- a/backend/api/v1/issue_service_test.go
+++ b/backend/api/v1/issue_service_test.go
@@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
+ "slices"
"sync"
"testing"
"time"
@@ -1640,3 +1641,150 @@ func waitForTransactionBlock(ctx context.Context, t *testing.T, db *sql.DB, tx *
time.Sleep(10 * time.Millisecond)
}
}
+
+func createIssueWithApproval(ctx context.Context, t *testing.T, stores *store.Store, title string, approval *storepb.IssuePayloadApproval) *store.IssueMessage {
+ t.Helper()
+
+ plan, err := stores.CreatePlan(ctx, &store.PlanMessage{
+ ProjectID: "project-a",
+ Name: title + " plan",
+ Config: &storepb.PlanConfig{},
+ require.NoError(t, err)
+
+ issue, err := stores.CreateIssue(ctx, &store.IssueMessage{
+ ProjectID: "project-a",
+ CreatorEmail: "[email protected]",
+ Title: title,
+ Type: storepb.Issue_DATABASE_CHANGE,
+ Payload: &storepb.Issue{Approval: approval},
+ PlanUID: &plan.UID,
+ })
+ require.NoError(t, err)
+ return issue
+}
+
+func approvalTemplateWithRoles(roles ...string) *storepb.ApprovalTemplate {
+ return &storepb.ApprovalTemplate{Flow: &storepb.ApprovalFlow{Roles: roles}}
+}
+
+func issueApprover(status storepb.IssuePayloadApproval_Approver_Status) *storepb.IssuePayloadApproval_Approver {
+ return &storepb.IssuePayloadApproval_Approver{
+ Status: status,
+ Principal: common.FormatUserEmail("[email protected]"),
+ }
+}
+
+// TestIssueApprovalFiltersRunBeforePaging is the regression lock for audit
+// finding T13: approval_status and current_approver used to be applied to the
+// page after the store had already cut it and minted the page token, so
+// filtering the default My Issues view answered with an empty page and a live
+// next page token underneath it.
+func TestIssueApprovalFiltersRunBeforePaging(t *testing.T) {
+ ctx := issueServiceTestContext()
+ stores := setupIssueServiceTestStore(ctx, t)
+ service := newIssueServiceForTest(t, stores)
+
+ // Created first, so the default id DESC ordering puts it behind both
+ // non-matching issues. With page size 1 the pre-fix code read the two
+ // non-matching rows, minted a token, and then dropped every row it had.
+ waiting := createIssueWithApproval(ctx, t, stores, "waiting on the caller", &storepb.IssuePayloadApproval{
+ ApprovalTemplate: approvalTemplateWithRoles("roles/workspaceAdmin"),
+ ApprovalFindingDone: true,
+ })
+ createIssueWithApproval(ctx, t, stores, "already approved", &storepb.IssuePayloadApproval{
+ ApprovalTemplate: approvalTemplateWithRoles("roles/workspaceAdmin"),
+ Approvers: []*storepb.IssuePayloadApproval_Approver{issueApprover(storepb.IssuePayloadApproval_Approver_APPROVED)},
+ ApprovalFindingDone: true,
+ })
+ // The caller holds roles/workspaceAdmin, not roles/projectOwner, so this one
+ // is pending but waiting on somebody else.
+ createIssueWithApproval(ctx, t, stores, "waiting on another role", &storepb.IssuePayloadApproval{
+ ApprovalTemplate: approvalTemplateWithRoles("roles/projectOwner"),
+ ApprovalFindingDone: true,
+ })
+
+ const filter = `approval_status == "PENDING" && current_approver == "users/[email protected]"`
+ want := []string{common.FormatIssue("project-a", waiting.UID)}
+
+ list, err := service.ListIssues(ctx, connect.NewRequest(&v1pb.ListIssuesRequest{
+ Parent: "projects/project-a",
+ Filter: filter,
+ PageSize: 1,
+ }))
+ require.NoError(t, err)
+ require.Equal(t, want, issueNames(list.Msg.Issues))
+ require.Empty(t, list.Msg.GetNextPageToken())
+
+ search, err := service.SearchIssues(ctx, connect.NewRequest(&v1pb.SearchIssuesRequest{
+ Parent: "projects/project-a",
+ Filter: filter,
+ PageSize: 1,
+ }))
+ require.NoError(t, err)
+ require.Equal(t, want, issueNames(search.Msg.Issues))
+ require.Empty(t, search.Msg.GetNextPageToken())
+}
+
+// TestIssueApprovalStatusFilterMatchesConverter pins the SQL derivation of
+// approval_status to computeApprovalStatus. They are two implementations of one
+// rule, and the SQL side has to read an absent JSON key as a zero value because
+// payloads are written with a bare protojson.Marshal.
+func TestIssueApprovalStatusFilterMatchesConverter(t *testing.T) {
+ ctx := issueServiceTestContext()
+ stores := setupIssueServiceTestStore(ctx, t)
+ service := newIssueServiceForTest(t, stores)
+
+ cases := []struct {
+ title string
+ approval *storepb.IssuePayloadApproval
+ }{
+ {"no approval payload at all", nil},
+ {"approval finding not done", &storepb.IssuePayloadApproval{ApprovalTemplate: approvalTemplateWithRoles("roles/workspaceAdmin")}},
+ {"no template", &storepb.IssuePayloadApproval{ApprovalFindingDone: true}},
+ {"template with an empty flow", &storepb.IssuePayloadApproval{ApprovalFindingDone: true, ApprovalTemplate: approvalTemplateWithRoles()}},
+ {"no approvers yet", &storepb.IssuePayloadApproval{ApprovalFindingDone: true, ApprovalTemplate: approvalTemplateWithRoles("roles/workspaceAdmin")}},
+ {"one of two steps approved", &storepb.IssuePayloadApproval{
+ ApprovalFindingDone: true,
+ ApprovalTemplate: approvalTemplateWithRoles("roles/workspaceAdmin", "roles/projectOwner"),
+ Approvers: []*storepb.IssuePayloadApproval_Approver{issueApprover(storepb.IssuePayloadApproval_Approver_APPROVED)},
+ }},
+ {"rejected at the second step", &storepb.IssuePayloadApproval{
+ ApprovalFindingDone: true,
+ ApprovalTemplate: approvalTemplateWithRoles("roles/workspaceAdmin", "roles/projectOwner"),
+ Approvers: []*storepb.IssuePayloadApproval_Approver{
+ issueApprover(storepb.IssuePayloadApproval_Approver_APPROVED),
+ issueApprover(storepb.IssuePayloadApproval_Approver_REJECTED),
+ },
+ }},
+ {"every step approved", &storepb.IssuePayloadApproval{
+ ApprovalFindingDone: true,
+ ApprovalTemplate: approvalTemplateWithRoles("roles/workspaceAdmin"),
+ Approvers: []*storepb.IssuePayloadApproval_Approver{issueApprover(storepb.IssuePayloadApproval_Approver_APPROVED)},
+ }},
+ }
+
+ want := map[v1pb.ApprovalStatus][]string{}
+ for _, tc := range cases {
+ issue := createIssueWithApproval(ctx, t, stores, tc.title, tc.approval)
+ status := computeApprovalStatus(tc.approval)
+ want[status] = append(want[status], common.FormatIssue("project-a", issue.UID))
+ }
+ // Every branch of computeApprovalStatus should be represented, otherwise the
+ // comparison below passes on empty sets.
+ require.Len(t, want, 5)
+
+ for status, wantNames := range want {
+ resp, err := service.ListIssues(ctx, connect.NewRequest(&v1pb.ListIssuesRequest{
+ Parent: "projects/project-a",
+ Filter: fmt.Sprintf("approval_status == %q", status),
+ PageSize: 100,
+ }))
+ require.NoError(t, err, "approval_status == %q", status)
+
+ gotNames := issueNames(resp.Msg.Issues)
+ slices.Sort(gotNames)
+ slices.Sort(wantNames)
+ require.Equal(t, wantNames, gotNames, "approval_status == %q", status)
+ }
+}
diff --git a/backend/store/issue.go b/backend/store/issue.go
index 624c326e1f..c7a0d6b223 100644
--- a/backend/store/issue.go
+++ b/backend/store/issue.go
@@ -108,8 +108,46 @@ type FindIssueMessage struct {
LabelList []string
RiskLevelList []storepb.RiskLevel
OrderByKeys []*OrderByKey
+
+ // ApprovalStatus is a v1 ApprovalStatus enum name, matched by approvalStatusExpr.
+ ApprovalStatus *string
+ // NextApproverRoles restricts the list to issues whose approval flow is
+ // waiting on one of these (project, role) pairs. Non-nil and empty matches
+ // no issue.
+ NextApproverRoles *[]ProjectRole
+}
+
+// ProjectRole is a role held in one project.
+type ProjectRole struct {
+ ProjectID string
+ Role string
}
+// approvalStatusExpr mirrors computeApprovalStatus in
+// backend/api/v1/issue_service_converter.go branch for branch — keep the two in
+// step. Payloads are marshalled with a bare protojson.Marshal, so an absent key
+// is a zero value.
+const approvalStatusExpr = `
+ CASE
+ WHEN NOT COALESCE((issue.payload->'approval'->>'approvalFindingDone')::BOOLEAN, FALSE) THEN 'CHECKING'
+ WHEN issue.payload->'approval'->'approvalTemplate' IS NULL THEN 'SKIPPED'
+ WHEN COALESCE(jsonb_array_length(issue.payload->'approval'->'approvers'), 0) = 0 THEN 'PENDING'
+ WHEN issue.payload->'approval'->'approvers' @> '[{"status": "REJECTED"}]'::JSONB THEN 'REJECTED'
+ WHEN COALESCE(jsonb_array_length(issue.payload->'approval'->'approvers'), 0)
+ >= COALESCE(jsonb_array_length(issue.payload->'approval'->'approvalTemplate'->'flow'->'roles'), 0)
+ AND NOT EXISTS (
+ SELECT 1
+ FROM jsonb_array_elements(issue.payload->'approval'->'approvers') AS approver
+ WHERE COALESCE(approver->>'status', '') <> 'APPROVED'
+ ) THEN 'APPROVED'
+ ELSE 'PENDING'
+ END`
+
+// nextApprovalRoleExpr is the flow role at the first step nobody has acted on.
+// `->>` past the end of the array yields NULL, so a finished flow drops out.
+const nextApprovalRoleExpr = `issue.payload->'approval'->'approvalTemplate'->'flow'->'roles'
+ ->> COALESCE(jsonb_array_length(issue.payload->'approval'->'approvers'), 0)`
+
// GetIssueOrders parses the order_by string and returns the corresponding OrderByKeys.
func GetIssueOrders(orderBy string) ([]*OrderByKey, error) {
keys, err := parseOrderBy(orderBy)
@@ -356,11 +394,12 @@ func (s *Store) ListIssues(ctx context.Context, find *FindIssueMessage) ([]*Issu
if v := find.CreatorID; v != nil {
where.And("issue.creator = ?", *v)
}
+ // The filter grammar documents ">=" and "<=" for create_time.
if v := find.CreatedAtBefore; v != nil {
- where.And("issue.created_at < ?", *v)
+ where.And("issue.created_at <= ?", *v)
}
if v := find.CreatedAtAfter; v != nil {
- where.And("issue.created_at > ?", *v)
+ where.And("issue.created_at >= ?", *v)
}
if v := find.Types; v != nil {
typeStrings := make([]string, 0, len(*v))
@@ -376,7 +415,7 @@ func (s *Store) ListIssues(ctx context.Context, find *FindIssueMessage) ([]*Issu
searchCondition.Or("issue.ts_vector @@ query")
rankOrder = "ts_rank(issue.ts_vector, query) DESC"
}
- searchCondition.Or("issue.name ILIKE ?", "%"+*v+"%")
+ searchCondition.Or("issue.name ILIKE ?", "%"+escapeLikePattern(*v)+"%")
where.And("(?)", searchCondition)
}
if len(find.StatusList) != 0 {
@@ -399,6 +438,26 @@ func (s *Store) ListIssues(ctx context.Context, find *FindIssueMessage) ([]*Issu
if find.ExcludeDraft {
where.And("COALESCE(issue.payload->>'draft', 'false') = 'false'")
}
+ // Matched in SQL, not over the returned page: filtering after LIMIT mints a
+ // page token for rows that are then dropped, so the list answers with an
+ // empty page and a live next page token.
+ if v := find.ApprovalStatus; v != nil {
+ where.And("("+approvalStatusExpr+") = ?", *v)
+ }
+ if v := find.NextApproverRoles; v != nil {
+ projects := make([]string, 0, len(*v))
+ roles := make([]string, 0, len(*v))
+ for _, projectRole := range *v {
+ projects = append(projects, projectRole.ProjectID)
+ roles = append(roles, projectRole.Role)
+ }
+ where.And(`EXISTS (
+ SELECT 1
+ FROM unnest(?::TEXT[], ?::TEXT[]) AS approver_role(project, role)
+ WHERE approver_role.project = issue.project
+ AND approver_role.role = `+nextApprovalRoleExpr+`
+ )`, projects, roles)
+ }
// issue.id alone is not unique across projects — nextProjectID floors every
// project's first issue at 101 — so issue.project completes the
@@ -604,8 +663,12 @@ func getTSQuery(text string) string {
if len(parts) == 0 {
parts = seg.CutTrim(text)
}
+ // Text the segmenter reduces to nothing is punctuation only. It has no
+ // lexeme to search for, and interpolating it raw builds an invalid tsquery
+ // that fails the whole query at cast time — `(`, `:` and `'` are tsquery
+ // syntax. Callers fall back to ILIKE when this is empty.
if len(parts) == 0 {
- return fmt.Sprintf("%s:*", text)
+ return ""
}
var tsQuery strings.Builder
for i, part := range parts {
diff --git a/backend/store/issue_list_filter_integration_test.go b/backend/store/issue_list_filter_integration_test.go
new file mode 100644
index 0000000000..4a0c3596ae
--- /dev/null
+++ b/backend/store/issue_list_filter_integration_test.go
@@ -0,0 +1,167 @@
+package store_test
+
+import (
+ "context"
+ "fmt"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/migrator"
+ "github.com/bytebase/bytebase/backend/store"
+
+ _ "github.com/bytebase/bytebase/backend/plugin/db/pg"
+)
+
+// TestIssueListFilterEdgeCases covers three defects in the issue list filters,
+// all of which reproduce against the pre-fix code:
+// - search text that is punctuation only used to build an invalid tsquery and
+// fail the whole query with SQLSTATE 42601;
+// - the ILIKE fallback interpolated the search text unescaped, so `%` and `_`
+// matched as wildcards;
+// - create_time is documented as ">=" and "<=" but was implemented as ">" and
+// "<", excluding an issue at the exact boundary.
+func TestIssueListFilterEdgeCases(t *testing.T) {
+ ctx := context.Background()
+ container := testcontainer.GetTestPgContainer(ctx, t)
+ t.Cleanup(func() { container.Close(ctx) })
+ db := container.GetDB()
+ require.NoError(t, migrator.MigrateSchema(ctx, db))
+ _, err := db.ExecContext(ctx, `
+ INSERT INTO workspace (resource_id) VALUES ('default');
+ INSERT INTO principal (name, email, password_hash) VALUES ('c', '[email protected]', 'x');
+ INSERT INTO project (resource_id, workspace, name) VALUES ('p1', 'default', 'P1'), ('p2', 'default', 'P2');
+ `)
+ require.NoError(t, err)
+
+ pgURL := fmt.Sprintf(
+ "host=%s port=%s user=postgres password=root-password database=postgres",
+ container.GetHost(), container.GetPort(),
+ )
+ stores, err := store.New(ctx, pgURL, false)
+ require.NoError(t, err)
+ t.Cleanup(func() { require.NoError(t, stores.Close()) })
+
+ createIn := func(projectID, title string, approval *storepb.IssuePayloadApproval) *store.IssueMessage {
+ plan, err := stores.CreatePlan(ctx, &store.PlanMessage{
+ ProjectID: projectID, Name: title, Config: &storepb.PlanConfig{},
+ require.NoError(t, err)
+ issue, err := stores.CreateIssue(ctx, &store.IssueMessage{
+ ProjectID: projectID, CreatorEmail: "[email protected]", Title: title,
+ Type: storepb.Issue_DATABASE_CHANGE, Payload: &storepb.Issue{Approval: approval}, PlanUID: &plan.UID,
+ })
+ require.NoError(t, err)
+ return issue
+ }
+ create := func(title string) *store.IssueMessage { return createIn("p1", title, nil) }
+ titles := func(find *store.FindIssueMessage) []string {
+ find.Workspace, find.ProjectIDs = "default", []string{"p1"}
+ issues, err := stores.ListIssues(ctx, find)
+ require.NoError(t, err)
+ out := []string{}
+ for _, issue := range issues {
+ out = append(out, issue.Title)
+ }
+ return out
+ }
+
+ create("fix the 100% CPU spike")
+ create("cleanup")
+ create("release (v2)")
+
+ // Punctuation-only search returns results instead of failing, via ILIKE.
+ for _, text := range []string{"&", "!", ":", "'", "|", "<->", "!!!"} {
+ query := text
+ require.Empty(t, titles(&store.FindIssueMessage{Query: &query}), "query %q", text)
+ }
+ paren := "("
+ require.Equal(t, []string{"release (v2)"}, titles(&store.FindIssueMessage{Query: &paren}))
+
+ // A real word still goes through full text search.
+ word := "cleanup"
+ require.Equal(t, []string{"cleanup"}, titles(&store.FindIssueMessage{Query: &word}))
+
+ // LIKE metacharacters are matched literally, not as wildcards.
+ wildcard := "%"
+ require.Equal(t, []string{"fix the 100% CPU spike"}, titles(&store.FindIssueMessage{Query: &wildcard}))
+
+ // create_time bounds include an issue sitting exactly on the boundary.
+ boundary := create("boundary")
+ stored, err := stores.GetIssue(ctx, &store.FindIssueMessage{ProjectIDs: []string{"p1"}, UID: &boundary.UID})
+ require.NoError(t, err)
+ at := stored.CreatedAt
+ require.Contains(t, titles(&store.FindIssueMessage{CreatedAtAfter: &at}), "boundary")
+ require.Contains(t, titles(&store.FindIssueMessage{CreatedAtBefore: &at}), "boundary")
+}
+
+// TestIssueListNextApproverIsProjectScoped locks the cross-project half of the
+// current_approver predicate. Two projects run the identical approval flow; a
+// role the caller holds in only one of them must match only that project's
+// issue. A flat role list without the project pairing passes every other
+// assertion in this file and fails this one.
+func TestIssueListNextApproverIsProjectScoped(t *testing.T) {
+ ctx := context.Background()
+ container := testcontainer.GetTestPgContainer(ctx, t)
+ t.Cleanup(func() { container.Close(ctx) })
+ db := container.GetDB()
+ require.NoError(t, migrator.MigrateSchema(ctx, db))
+ _, err := db.ExecContext(ctx, `
+ INSERT INTO workspace (resource_id) VALUES ('default');
+ INSERT INTO principal (name, email, password_hash) VALUES ('c', '[email protected]', 'x');
+ INSERT INTO project (resource_id, workspace, name) VALUES ('p1', 'default', 'P1'), ('p2', 'default', 'P2');
+ `)
+ require.NoError(t, err)
+
+ pgURL := fmt.Sprintf(
+ "host=%s port=%s user=postgres password=root-password database=postgres",
+ container.GetHost(), container.GetPort(),
+ )
+ stores, err := store.New(ctx, pgURL, false)
+ require.NoError(t, err)
+ t.Cleanup(func() { require.NoError(t, stores.Close()) })
+
+ waiting := &storepb.IssuePayloadApproval{
+ ApprovalTemplate: &storepb.ApprovalTemplate{Flow: &storepb.ApprovalFlow{Roles: []string{"roles/projectOwner"}}},
+ ApprovalFindingDone: true,
+ }
+ create := func(projectID, title string) *store.IssueMessage {
+ plan, err := stores.CreatePlan(ctx, &store.PlanMessage{
+ ProjectID: projectID, Name: title, Config: &storepb.PlanConfig{},
+ require.NoError(t, err)
+ issue, err := stores.CreateIssue(ctx, &store.IssueMessage{
+ ProjectID: projectID, CreatorEmail: "[email protected]", Title: title,
+ Type: storepb.Issue_DATABASE_CHANGE, Payload: &storepb.Issue{Approval: waiting}, PlanUID: &plan.UID,
+ })
+ require.NoError(t, err)
+ return issue
+ }
+ inP1 := create("p1", "waiting in p1")
+ create("p2", "waiting in p2")
+
+ list := func(roles []store.ProjectRole) []*store.IssueMessage {
+ issues, err := stores.ListIssues(ctx, &store.FindIssueMessage{
+ Workspace: "default", ProjectIDs: []string{"p1", "p2"}, NextApproverRoles: &roles,
+ })
+ require.NoError(t, err)
+ return issues
+ }
+
+ // Holding the role in p1 only must not surface p2's identical issue.
+ got := list([]store.ProjectRole{{ProjectID: "p1", Role: "roles/projectOwner"}})
+ require.Len(t, got, 1)
+ require.Equal(t, "p1", got[0].ProjectID)
+ require.Equal(t, inP1.UID, got[0].UID)
+
+ // Holding it in both surfaces both.
+ require.Len(t, list([]store.ProjectRole{
+ {ProjectID: "p1", Role: "roles/projectOwner"},
+ {ProjectID: "p2", Role: "roles/projectOwner"},
+ }), 2)
+
+ // A non-nil empty set matches nothing rather than everything.
+ require.Empty(t, list([]store.ProjectRole{}))
+}
diff --git a/backend/store/issue_search_test.go b/backend/store/issue_search_test.go
new file mode 100644
index 0000000000..a7ef7da18c
--- /dev/null
+++ b/backend/store/issue_search_test.go
@@ -0,0 +1,19 @@
+package store
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+// Text with no lexeme in it must produce no tsquery at all. Interpolating it
+// raw builds an invalid tsquery — `(`, `:`, `'`, `&`, `|` and `!` are tsquery
+// syntax — which fails the whole issue list at cast time.
+func TestGetTSQueryRejectsNonLexemeText(t *testing.T) {
+ for _, text := range []string{"(", ")", ":", "'", "&", "|", "!", "!!!", "<->", " ", `\`} {
+ require.Empty(t, getTSQuery(text), "getTSQuery(%q)", text)
+ }
+ for _, text := range []string{"cleanup", "fix login bug", "100%", "foo-bar", "重构订单表"} {
+ require.NotEmpty(t, getTSQuery(text), "getTSQuery(%q)", text)
+ }
+}
diff --git a/backend/store/policy.go b/backend/store/policy.go
index 032dc69c3b..77d4ac5d9c 100644
--- a/backend/store/policy.go
+++ b/backend/store/policy.go
@@ -116,6 +116,45 @@ func (s *Store) GetProjectIamPolicy(ctx context.Context, workspaceID string, pro
})
}
+// ListProjectIamPolicies returns the IAM policy of each named project, keyed by
+// project resource ID, in one query. Callers needing several projects' policies
+// use this instead of GetProjectIamPolicy in a loop, which costs a round trip
+// per project whenever the cache is cold. A project with no stored policy is
+// absent from the map; treat that as an empty policy.
+func (s *Store) ListProjectIamPolicies(ctx context.Context, workspaceID string, projectIDs []string) (map[string]*storepb.IamPolicy, error) {
+ if len(projectIDs) == 0 {
+ return map[string]*storepb.IamPolicy{}, nil
+ }
+ resources := make([]string, 0, len(projectIDs))
+ for _, projectID := range projectIDs {
+ resources = append(resources, common.FormatProject(projectID))
+ }
+ policies, err := s.ListPolicies(ctx, &FindPolicyMessage{
+ Workspace: workspaceID,
+ ResourceType: new(storepb.Policy_PROJECT),
+ Resources: resources,
+ Type: new(storepb.Policy_IAM),
+ ShowAll: true,
+ })
+ if err != nil {
+ return nil, err
+ }
+
+ policyMap := make(map[string]*storepb.IamPolicy, len(policies))
+ for _, policy := range policies {
+ projectID, err := common.GetProjectID(policy.Resource)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to parse project from policy resource %q", policy.Resource)
+ }
+ p := &storepb.IamPolicy{}
+ if err := common.ProtojsonUnmarshaler.Unmarshal([]byte(policy.Payload), p); err != nil {
+ return nil, errors.Wrapf(err, "failed to unmarshal iam policy for %v", policy.Resource)
+ }
+ policyMap[projectID] = p
+ }
+ return policyMap, nil
+}
+
func (s *Store) GetWorkspaceIamPolicySnapshot(ctx context.Context, workspaceID string) (*IamPolicyMessage, error) {
workspaceResource := common.FormatWorkspace(workspaceID)
key := getIamPolicyCacheKey(workspaceID, storepb.Policy_WORKSPACE, workspaceResource)
@@ -419,7 +458,10 @@ type FindPolicyMessage struct {
Workspace string
ResourceType *storepb.Policy_Resource
Resource *string
- Type *storepb.Policy_Type
+ // Resources matches any of several resources. Unlike Resource it is never
+ // cached, so it is for batch reads rather than single lookups.
+ Resources []string
+ Type *storepb.Policy_Type
// ShowAll will show all policies regardless of the enforce status.
ShowAll bool
}
@@ -488,6 +530,9 @@ func (s *Store) ListPolicies(ctx context.Context, find *FindPolicyMessage) ([]*P
if v := find.Resource; v != nil {
q.And("resource = ?", *v)
}
+ if v := find.Resources; v != nil {
+ q.And("resource = ANY(?)", v)
+ }
if v := find.Type; v != nil {
q.And("type = ?", v.String())
}
diff --git a/backend/store/policy_project_iam_test.go b/backend/store/policy_project_iam_test.go
new file mode 100644
index 0000000000..05875769ae
--- /dev/null
+++ b/backend/store/policy_project_iam_test.go
@@ -0,0 +1,101 @@
+package store_test
+
+import (
+ "context"
+ "fmt"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/migrator"
+ "github.com/bytebase/bytebase/backend/store"
+
+ _ "github.com/bytebase/bytebase/backend/plugin/db/pg"
+)
+
+// TestListProjectIamPoliciesIsScoped pins the predicate of the batch read that
+// replaced a GetProjectIamPolicy loop. The policy primary key is
+// (workspace, resource_type, resource, type) and the query names all four; this
+// asserts each one actually filters, so a dropped clause cannot silently widen
+// the read into another workspace, another resource type, or — worst — another
+// policy type, whose payload would then be unmarshalled as an IamPolicy.
+func TestListProjectIamPoliciesIsScoped(t *testing.T) {
+ ctx := context.Background()
+ container := testcontainer.GetTestPgContainer(ctx, t)
+ t.Cleanup(func() { container.Close(ctx) })
+ db := container.GetDB()
+ require.NoError(t, migrator.MigrateSchema(ctx, db))
+ _, err := db.ExecContext(ctx, `
+ INSERT INTO workspace (resource_id) VALUES ('ws-a'), ('ws-b');
+ INSERT INTO project (resource_id, workspace, name) VALUES
+ ('mine', 'ws-a', 'Mine'),
+ ('rollout-only', 'ws-a', 'Rollout only'),
+ ('no-policy', 'ws-a', 'No policy'),
+ ('other-workspace', 'ws-b', 'Other workspace');
+ `)
+ require.NoError(t, err)
+
+ pgURL := fmt.Sprintf(
+ "host=%s port=%s user=postgres password=root-password database=postgres",
+ container.GetHost(), container.GetPort(),
+ )
+ stores, err := store.New(ctx, pgURL, false)
+ require.NoError(t, err)
+ t.Cleanup(func() { require.NoError(t, stores.Close()) })
+
+ iamPayload := func(role string) string {
+ return fmt.Sprintf(`{"bindings":[{"role":%q,"members":["users/[email protected]"]}]}`, role)
+ }
+ create := func(workspace, projectID string, policyType storepb.Policy_Type, payload string) {
+ _, err := stores.CreatePolicy(ctx, &store.PolicyMessage{
+ Workspace: workspace,
+ ResourceType: storepb.Policy_PROJECT,
+ Resource: common.FormatProject(projectID),
+ Type: policyType,
+ Payload: payload,
+ Enforce: true,
+ })
+ require.NoError(t, err)
+ }
+ create("ws-a", "mine", storepb.Policy_IAM, iamPayload("roles/projectOwner"))
+ // Same project, a different policy type: must not be read as an IAM policy.
+ create("ws-a", "mine", storepb.Policy_ROLLOUT, `{"automatic":true}`)
+ create("ws-a", "rollout-only", storepb.Policy_ROLLOUT, `{"automatic":true}`)
+ create("ws-b", "other-workspace", storepb.Policy_IAM, iamPayload("roles/projectOwner"))
+
+ got, err := stores.ListProjectIamPolicies(ctx, "ws-a",
+ []string{"mine", "rollout-only", "no-policy", "other-workspace"})
+ require.NoError(t, err)
+
+ // Only the one project in this workspace with an IAM policy.
+ require.Len(t, got, 1)
+ require.Contains(t, got, "mine")
+ require.Len(t, got["mine"].GetBindings(), 1)
+ require.Equal(t, "roles/projectOwner", got["mine"].GetBindings()[0].GetRole())
+
+ // A project whose only policy is another type is absent, not misparsed.
+ require.NotContains(t, got, "rollout-only")
+ // A project with no policy at all is absent; callers treat that as empty.
+ require.NotContains(t, got, "no-policy")
+ // Another workspace's project is never reachable, even when named.
+ require.NotContains(t, got, "other-workspace")
+
+ // Naming only the foreign project returns nothing rather than its policy.
+ foreign, err := stores.ListProjectIamPolicies(ctx, "ws-a", []string{"other-workspace"})
+ require.NoError(t, err)
+ require.Empty(t, foreign)
+
+ // Reading it from its own workspace does work, so the empty result above is
+ // the workspace filter and not a missing row.
+ own, err := stores.ListProjectIamPolicies(ctx, "ws-b", []string{"other-workspace"})
+ require.NoError(t, err)
+ require.Len(t, own, 1)
+
+ // No projects named is not "all projects".
+ none, err := stores.ListProjectIamPolicies(ctx, "ws-a", nil)
+ require.NoError(t, err)
+ require.Empty(t, none)
+}
diff --git a/docs/design/v1-api-audit-2026-08.md b/docs/design/v1-api-audit-2026-08.md
index c147962ac3..e67d1ed9c7 100644
--- a/docs/design/v1-api-audit-2026-08.md
+++ b/docs/design/v1-api-audit-2026-08.md
@@ -10,7 +10,6 @@ noted. Fixed findings are removed and listed at the end.
| | Problem | Severity |
|---|---|---|
-| T13 | "Waiting for my approval" can show an empty page with a live Load More | HIGH |
| T12 | Anonymous callers can enumerate emails, relay mail, and read your LDAP config | MED |
| T16 | Four `BatchGet` RPCs behave four different ways when an item is missing | MED |
| T18b | Concurrent permission edits silently lose one side, including a revoke | MED |
@@ -77,17 +76,6 @@ irreversibly purged, and the error names only the one that failed — not the on
## Lists that return the wrong rows
-### T13 · Issue filters run after the page is cut — HIGH
-
-`approval_status` and `current_approver` never reach SQL. The database returns a full page, the page
-token is minted, and *then* non-matching issues are dropped in Go. So filtering by "waiting for my
-approval" in a busy workspace shows an empty list with a live "Load more" button underneath.
-
-This is the default My Issues view, and there's no empty-page workaround on the frontend.
-`issue_service_converter.go:29-41`, `issue_service.go:134-148`
-
-(Same shape as the `SearchProjects` bug that parked patch `03` fixes — also still unmerged.)
-
### T16 · Four `BatchGet` RPCs, four behaviors — MED
Ask for five databases and get three back, with no way to tell which two are missing. Ask for five
@@ -194,6 +182,7 @@ constraints.
| T8 `CreateWorksheet` | [#21169](https://github.com/bytebase/bytebase/pull/21169) — `CreateSavedQuery` is now IAM-enforced; Workspace Member holds no saved-query permission, and new queries start creator-private |
| T12 `GetActuatorInfo` | [#21184](https://github.com/bytebase/bytebase/pull/21184) — anonymous access and the `name` field removed; workspace now comes from the token (the narrower surface that replaced it is T12) |
| T13 `SearchWorksheets` | [#21160](https://github.com/bytebase/bytebase/pull/21160) + [#21178](https://github.com/bytebase/bytebase/pull/21178) — visibility predicate pushed into SQL before `LIMIT` |
+| T13 issue filters | `approval_status` and `current_approver` are matched in SQL before `LIMIT`, so a filtered page can no longer come back empty under a live page token. The status is a `CASE` over `payload->'approval'` mirroring `computeApprovalStatus`; `current_approver` cannot be resolved in SQL alone (binding conditions and group expansion are evaluated in Go), so the caller passes down the `(project, role)` pairs the named user holds, the same shape as the saved-query `AccessMembers` predicate. Accepted with it: the predicates are not indexable, so a filtered list scans until it has a full page of matches — measured on 50,000 issues across 40 projects, that is ~9 ms against ~14 ms for the same unfiltered query, and ~3.5 ms in the zero-match case, because PostgreSQL drives the join from the small `(project, role)` set, so no expression index is warranted yet. Deliberately unchanged: the filter still ignores rejection and self-approval, so a rejected issue names the holder of the *next* role as its current approver — masked in the default view, and worth its own fix |
| T18 worksheet write/delete | [#21169](https://github.com/bytebase/bytebase/pull/21169) + [#21181](https://github.com/bytebase/bytebase/pull/21181) — per-verb permissions; SQL Editor Read User can no longer rewrite or delete |
| T18a | [#21265](https://github.com/bytebase/bytebase/pull/21265) — `BatchCancelTaskRuns` confines its lookup to the plan and environment just authorized; the stage in a task run name is caller-supplied, so cancel rights in one environment reached a RUNNING row in another. An out-of-scope ID is rejected, not dropped — the cancel is driven by the requested IDs |
| T9 | [#21189](https://github.com/bytebase/bytebase/pull/21189) (self-hosted audit rows) + [#21234](https://github.com/bytebase/bytebase/pull/21234) — one `login_attempt` table bounds password, email-code, and MFA guessing per identity on both deployments, replacing the audit-log counter and the per-code attempt column (which bypassed the resend cooldown). Accepted with it: no per-tenant failed-login record on Cloud, and lockout-as-denial-of-service — see [`login-attempt-lockout.md`](login-attempt-lockout.md) |