fix(integrations): stop sentry and gitlab from leaking other workspaces' issue watches (#3681)
Co-authored-by: nova28 <17953305+nova28@users.noreply.github.com> Co-authored-by: Carlos Florencio <carlosmflorencio@gmail.com>
This commit is contained in:
@@ -865,6 +865,7 @@ func registerRoutes(p routeParams) {
|
||||
p.services.GitLab.SetWatchDependencyValidator(&gitLabWatchDependencyValidator{
|
||||
tasks: p.taskSvc, workflows: p.services.Workflow, agents: p.agentSettingsRepo,
|
||||
})
|
||||
p.services.GitLab.SetWorkspaceAuthorizer(p.taskSvc.AuthorizeWorkspaceAccess)
|
||||
}
|
||||
if p.services.AzureDevOps != nil {
|
||||
p.services.AzureDevOps.SetWatchRepositoryLookup(repoLookup)
|
||||
@@ -885,6 +886,7 @@ func registerRoutes(p routeParams) {
|
||||
if p.services.Sentry != nil {
|
||||
p.services.Sentry.SetTaskDeleter(handoffSvc)
|
||||
p.services.Sentry.SetRepositoryLookup(repoLookup)
|
||||
p.services.Sentry.SetWorkspaceAuthorizer(p.taskSvc.AuthorizeWorkspaceAccess)
|
||||
}
|
||||
if p.services.Automation != nil {
|
||||
p.services.Automation.Service.SetRepositoryLookup(repoLookup)
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package backendapp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestSentryAndGitLabListAllIssueWatchesScopingIsWired guards the two lines
|
||||
// that make ListAllIssueWatches' per-user workspace filter real in the
|
||||
// shipped binary. internal/sentry and internal/gitlab both default to
|
||||
// unscoped (nil workspaceAuthorizer) so their own package tests stay green
|
||||
// with a hand-injected fake — only this wiring block makes the real
|
||||
// taskSvc.AuthorizeWorkspaceAccess check apply, so a rebase or refactor that
|
||||
// drops one of these lines would leak every workspace's watch config again
|
||||
// (see docs/plans/alert-ingest/task-00-issue-watch-workspace-scoping.md) with
|
||||
// no other failing test.
|
||||
func TestSentryAndGitLabListAllIssueWatchesScopingIsWired(t *testing.T) {
|
||||
fn := findFuncDecl(t, "helpers.go", "registerRoutes")
|
||||
want := map[string]bool{
|
||||
"p.services.GitLab.SetWorkspaceAuthorizer": false,
|
||||
"p.services.Sentry.SetWorkspaceAuthorizer": false,
|
||||
}
|
||||
ast.Inspect(fn, func(n ast.Node) bool {
|
||||
call, ok := n.(*ast.CallExpr)
|
||||
if !ok || len(call.Args) != 1 {
|
||||
return true
|
||||
}
|
||||
callee := dottedExprString(call.Fun)
|
||||
if _, tracked := want[callee]; !tracked {
|
||||
return true
|
||||
}
|
||||
if dottedExprString(call.Args[0]) == "p.taskSvc.AuthorizeWorkspaceAccess" {
|
||||
want[callee] = true
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
for setter, wired := range want {
|
||||
if !wired {
|
||||
t.Errorf("registerRoutes never calls %s(p.taskSvc.AuthorizeWorkspaceAccess); without it "+
|
||||
"ListAllIssueWatches leaks every workspace's watch config to a scoped caller", setter)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// dottedExprString renders a chain of selectors/idents (e.g. p.services.GitLab
|
||||
// or p.taskSvc.AuthorizeWorkspaceAccess) as a dotted string for comparison.
|
||||
func dottedExprString(e ast.Expr) string {
|
||||
switch v := e.(type) {
|
||||
case *ast.SelectorExpr:
|
||||
return dottedExprString(v.X) + "." + v.Sel.Name
|
||||
case *ast.Ident:
|
||||
return v.Name
|
||||
default:
|
||||
return fmt.Sprintf("%T", e)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package gitlab
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/kandev/kandev/internal/task/repository/repoerrors"
|
||||
)
|
||||
|
||||
// denyOnly returns a workspace authorizer that allows every workspace except
|
||||
// the named ones, which it denies with the same ErrWorkspaceNotFound the real
|
||||
// task-service authorizer returns. Mirrors internal/jira/scope_test.go.
|
||||
func denyOnly(denied ...string) func(context.Context, string) error {
|
||||
blocked := make(map[string]bool, len(denied))
|
||||
for _, ws := range denied {
|
||||
blocked[ws] = true
|
||||
}
|
||||
return func(_ context.Context, ws string) error {
|
||||
if blocked[ws] {
|
||||
return repoerrors.ErrWorkspaceNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func newScopeWatchFixture(t *testing.T) (*Service, *Store) {
|
||||
t.Helper()
|
||||
store := newTestStore(t)
|
||||
svc := NewService("", nil, AuthMethodNone, nil, newTestLogger(t))
|
||||
svc.SetStore(store)
|
||||
return svc, store
|
||||
}
|
||||
|
||||
func seedIssueWatch(t *testing.T, store *Store, workspaceID string) *IssueWatch {
|
||||
t.Helper()
|
||||
w := &IssueWatch{
|
||||
WorkspaceID: workspaceID, WorkflowID: "wf", WorkflowStepID: "step",
|
||||
AgentProfileID: "agent", ExecutorProfileID: "exec", Enabled: true,
|
||||
}
|
||||
if err := store.CreateIssueWatch(context.Background(), w); err != nil {
|
||||
t.Fatalf("seed issue watch: %v", err)
|
||||
}
|
||||
return w
|
||||
}
|
||||
|
||||
// TestListAllIssueWatches_FiltersToAccessibleWorkspaces is the regression for
|
||||
// the gap noted in AGENTS.md's "Per-user scoping" section: ListAllIssueWatches
|
||||
// was a bare passthrough to the store with no workspace filter.
|
||||
func TestListAllIssueWatches_FiltersToAccessibleWorkspaces(t *testing.T) {
|
||||
svc, store := newScopeWatchFixture(t)
|
||||
ctx := context.Background()
|
||||
seedIssueWatch(t, store, "ws-allowed")
|
||||
seedIssueWatch(t, store, "ws-allowed")
|
||||
seedIssueWatch(t, store, "ws-denied")
|
||||
svc.SetWorkspaceAuthorizer(denyOnly("ws-denied"))
|
||||
|
||||
watches, err := svc.ListAllIssueWatches(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ListAllIssueWatches: %v", err)
|
||||
}
|
||||
if len(watches) != 2 {
|
||||
t.Fatalf("watches = %+v, want both of ws-allowed's watches", watches)
|
||||
}
|
||||
for _, w := range watches {
|
||||
if w.WorkspaceID != "ws-allowed" {
|
||||
t.Fatalf("watches = %+v, want only ws-allowed's watches", watches)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestListAllIssueWatches_NilAuthorizerReturnsEveryWorkspace preserves the
|
||||
// identity-less internal caller behaviour: poller.go's ListAllIssueWatches
|
||||
// call must keep seeing every watch when no authorizer is wired.
|
||||
func TestListAllIssueWatches_NilAuthorizerReturnsEveryWorkspace(t *testing.T) {
|
||||
svc, store := newScopeWatchFixture(t)
|
||||
ctx := context.Background()
|
||||
seedIssueWatch(t, store, "ws-1")
|
||||
seedIssueWatch(t, store, "ws-2")
|
||||
|
||||
watches, err := svc.ListAllIssueWatches(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ListAllIssueWatches: %v", err)
|
||||
}
|
||||
if len(watches) != 2 {
|
||||
t.Fatalf("watches = %+v, want 2 (nil authorizer must stay unscoped for internal callers like the poller)", watches)
|
||||
}
|
||||
}
|
||||
|
||||
// TestListAllIssueWatches_PropagatesAuthorizerError is the regression for a
|
||||
// non-denial authorizer error (e.g. a transient DB failure): it must surface,
|
||||
// not be treated as a silent per-watch denial that returns a truncated list.
|
||||
func TestListAllIssueWatches_PropagatesAuthorizerError(t *testing.T) {
|
||||
svc, store := newScopeWatchFixture(t)
|
||||
ctx := context.Background()
|
||||
seedIssueWatch(t, store, "ws-1")
|
||||
boom := errors.New("authorizer backend down")
|
||||
svc.SetWorkspaceAuthorizer(func(context.Context, string) error { return boom })
|
||||
|
||||
if _, err := svc.ListAllIssueWatches(ctx); !errors.Is(err, boom) {
|
||||
t.Fatalf("expected the authorizer error to propagate, got %v", err)
|
||||
}
|
||||
}
|
||||
@@ -116,8 +116,12 @@ type Service struct {
|
||||
repositoryLookup RepositoryLookup
|
||||
dependencyValidator WatchDependencyValidator
|
||||
taskAuthorizer TaskAuthorizer
|
||||
promptResolver PromptResolver
|
||||
logger *logger.Logger
|
||||
// workspaceAuthorizer is the per-user workspace access boundary, wired
|
||||
// post-construction via SetWorkspaceAuthorizer. Nil (unit tests, auth
|
||||
// disabled) means unscoped — every workspace is visible, as before auth.
|
||||
workspaceAuthorizer func(context.Context, string) error
|
||||
promptResolver PromptResolver
|
||||
logger *logger.Logger
|
||||
}
|
||||
|
||||
// PromptResolver resolves editable prompt content by name. Mirrors
|
||||
@@ -204,6 +208,29 @@ func (s *Service) authorizeTaskMRAccess(ctx context.Context, taskID string) erro
|
||||
return authorizer.AuthorizeTaskAccess(ctx, taskID)
|
||||
}
|
||||
|
||||
// SetWorkspaceAuthorizer installs the per-user workspace access boundary
|
||||
// applied to ListAllIssueWatches. Wired to taskSvc.AuthorizeWorkspaceAccess so
|
||||
// an unscoped list (workspace_id omitted) returns only the caller's own
|
||||
// workspaces' watches.
|
||||
func (s *Service) SetWorkspaceAuthorizer(authorizer func(context.Context, string) error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.workspaceAuthorizer = authorizer
|
||||
}
|
||||
|
||||
// authorizeWorkspaceAccess denies access to a workspace a scoped caller
|
||||
// cannot reach. A nil authorizer (not wired, e.g. unit tests) or an unscoped
|
||||
// caller (internal callers, auth disabled) is a no-op.
|
||||
func (s *Service) authorizeWorkspaceAccess(ctx context.Context, workspaceID string) error {
|
||||
s.mu.RLock()
|
||||
authorizer := s.workspaceAuthorizer
|
||||
s.mu.RUnlock()
|
||||
if authorizer == nil {
|
||||
return nil
|
||||
}
|
||||
return authorizer(ctx, workspaceID)
|
||||
}
|
||||
|
||||
func (s *Service) validateWatchDependencies(ctx context.Context, workspaceID, workflowID, stepID, agentProfileID, executorProfileID string) error {
|
||||
if strings.TrimSpace(workspaceID) == "" || strings.TrimSpace(workflowID) == "" ||
|
||||
strings.TrimSpace(stepID) == "" || strings.TrimSpace(agentProfileID) == "" ||
|
||||
|
||||
@@ -2,12 +2,15 @@ package gitlab
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/kandev/kandev/internal/task/repository/repoerrors"
|
||||
)
|
||||
|
||||
// CreateIssueWatch persists a new issue watch.
|
||||
@@ -94,13 +97,48 @@ func (s *Service) ListIssueWatches(ctx context.Context, workspaceID string) ([]*
|
||||
return store.ListIssueWatches(ctx, workspaceID)
|
||||
}
|
||||
|
||||
// ListAllIssueWatches returns every issue watch.
|
||||
// ListAllIssueWatches returns every issue watch the caller may see. For a
|
||||
// scoped caller that is only their own workspaces' watches; for an
|
||||
// identity-less internal caller (unscoped) it is every watch, as before auth.
|
||||
func (s *Service) ListAllIssueWatches(ctx context.Context) ([]*IssueWatch, error) {
|
||||
store := s.requireStore()
|
||||
if store == nil {
|
||||
return nil, errStoreUnavailable
|
||||
}
|
||||
return store.ListAllIssueWatches(ctx)
|
||||
watches, err := store.ListAllIssueWatches(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.filterIssueWatchesByAccess(ctx, watches)
|
||||
}
|
||||
|
||||
// filterIssueWatchesByAccess keeps only watches whose workspace the caller may
|
||||
// access. Access decisions are memoized per workspace so a long list costs one
|
||||
// authorize call per distinct workspace, not one per watch. Only an
|
||||
// ErrWorkspaceNotFound denial drops a watch; any other authorizer error (a
|
||||
// transient DB failure, say) is propagated so the caller sees the failure
|
||||
// rather than a silently truncated 200.
|
||||
func (s *Service) filterIssueWatchesByAccess(ctx context.Context, watches []*IssueWatch) ([]*IssueWatch, error) {
|
||||
decision := make(map[string]bool)
|
||||
visible := make([]*IssueWatch, 0, len(watches))
|
||||
for _, w := range watches {
|
||||
allowed, seen := decision[w.WorkspaceID]
|
||||
if !seen {
|
||||
switch err := s.authorizeWorkspaceAccess(ctx, w.WorkspaceID); {
|
||||
case err == nil:
|
||||
allowed = true
|
||||
case errors.Is(err, repoerrors.ErrWorkspaceNotFound):
|
||||
allowed = false
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
decision[w.WorkspaceID] = allowed
|
||||
}
|
||||
if allowed {
|
||||
visible = append(visible, w)
|
||||
}
|
||||
}
|
||||
return visible, nil
|
||||
}
|
||||
|
||||
// UpdateIssueWatch applies a partial update.
|
||||
|
||||
@@ -43,9 +43,8 @@ guard themselves at the service layer:
|
||||
Wire the boundary with `SetWorkspaceAuthorizer(taskSvc.AuthorizeWorkspaceAccess)`
|
||||
in `backendapp/helpers.go`; nil (unit tests, auth disabled) means unscoped.
|
||||
Denials surface `repoerrors.ErrWorkspaceNotFound`, which handlers map to 404 (no
|
||||
existence leak). Jira/Linear/Slack follow this; **Sentry and GitLab's
|
||||
`ListAllIssueWatches` still need the same filter** — the WS gateway backstop does
|
||||
not read `workspace_id`, so GitLab's `workspace_id`-keyed WS list is unscoped too.
|
||||
existence leak). Jira, Linear, Sentry, and GitLab's `ListAllIssueWatches` all
|
||||
filter through this mechanism.
|
||||
|
||||
## Auto-link guarantee (GitHub pull requests / GitLab merge requests)
|
||||
|
||||
|
||||
@@ -21,8 +21,8 @@ func (c *Controller) registerIssueWatchRoutes(api *gin.RouterGroup) {
|
||||
}
|
||||
|
||||
// httpListIssueWatches returns watches scoped to one workspace when
|
||||
// `workspace_id` is supplied, or every watch across all workspaces when it is
|
||||
// absent (used by the install-wide settings page).
|
||||
// `workspace_id` is supplied, or all watches visible to the caller when it is
|
||||
// absent.
|
||||
func (c *Controller) httpListIssueWatches(ctx *gin.Context) {
|
||||
workspaceID := ctx.Query("workspace_id")
|
||||
var (
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
package sentry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
"github.com/kandev/kandev/internal/auth/authn"
|
||||
"github.com/kandev/kandev/internal/common/logger"
|
||||
"github.com/kandev/kandev/internal/db"
|
||||
"github.com/kandev/kandev/internal/events/bus"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
"github.com/kandev/kandev/internal/task/repository"
|
||||
taskservice "github.com/kandev/kandev/internal/task/service"
|
||||
)
|
||||
|
||||
func newTaskServiceWithWorkspaceRepository(t *testing.T) (*taskservice.Service, repository.WorkspaceRepository) {
|
||||
t.Helper()
|
||||
dbConn, err := db.OpenSQLite(filepath.Join(t.TempDir(), "task-service.db"))
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
database := sqlx.NewDb(dbConn, "sqlite3")
|
||||
t.Cleanup(func() { _ = database.Close() })
|
||||
|
||||
repos, cleanup, err := repository.Provide(database, database, logger.Default())
|
||||
if err != nil {
|
||||
t.Fatalf("provide task repository: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = cleanup() })
|
||||
|
||||
log := logger.Default()
|
||||
return taskservice.NewService(
|
||||
taskservice.Repos{Workspaces: repos},
|
||||
bus.NewMemoryEventBus(log),
|
||||
log,
|
||||
taskservice.RepositoryDiscoveryConfig{},
|
||||
), repos
|
||||
}
|
||||
|
||||
func seedAuthorizerWorkspace(t *testing.T, workspaces repository.WorkspaceRepository, id, ownerID string) {
|
||||
t.Helper()
|
||||
if err := workspaces.CreateWorkspace(context.Background(), &models.Workspace{
|
||||
ID: id, Name: id, OwnerID: ownerID,
|
||||
}); err != nil {
|
||||
t.Fatalf("create workspace %q: %v", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHTTP_ListAllIssueWatches_UsesTaskServiceAuthorizer covers the complete
|
||||
// unscoped HTTP path with the production workspace authorizer. It also keeps
|
||||
// identity-less and synthetic callers unscoped when the authorizer is wired.
|
||||
func TestHTTP_ListAllIssueWatches_UsesTaskServiceAuthorizer(t *testing.T) {
|
||||
taskSvc, workspaces := newTaskServiceWithWorkspaceRepository(t)
|
||||
seedAuthorizerWorkspace(t, workspaces, "ws-owned", "user-a")
|
||||
seedAuthorizerWorkspace(t, workspaces, "ws-foreign", "user-b")
|
||||
|
||||
ctrl, router, _ := newTestController(t)
|
||||
ctrl.service.SetWorkspaceAuthorizer(taskSvc.AuthorizeWorkspaceAccess)
|
||||
for _, workspaceID := range []string{"ws-owned", "ws-foreign"} {
|
||||
if err := ctrl.service.store.CreateIssueWatch(context.Background(), newTestIssueWatch(workspaceID)); err != nil {
|
||||
t.Fatalf("seed watch for %q: %v", workspaceID, err)
|
||||
}
|
||||
}
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/sentry/watches/issue", nil)
|
||||
request = request.WithContext(authn.WithIdentity(request.Context(), authn.Identity{
|
||||
UserID: "user-a",
|
||||
Role: authn.RoleMember,
|
||||
}))
|
||||
response := httptest.NewRecorder()
|
||||
router.ServeHTTP(response, request)
|
||||
if response.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, body = %s", response.Code, response.Body.String())
|
||||
}
|
||||
var body struct {
|
||||
Watches []IssueWatch `json:"watches"`
|
||||
}
|
||||
if err := json.Unmarshal(response.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("decode response: %v", err)
|
||||
}
|
||||
if len(body.Watches) != 1 || body.Watches[0].WorkspaceID != "ws-owned" {
|
||||
t.Fatalf("HTTP watches = %+v, want only user-a's workspace", body.Watches)
|
||||
}
|
||||
|
||||
for name, ctx := range map[string]context.Context{
|
||||
"identity-less": context.Background(),
|
||||
"synthetic": authn.WithIdentity(context.Background(), authn.Identity{
|
||||
UserID: "synthetic",
|
||||
Role: authn.RoleAdmin,
|
||||
Synthetic: true,
|
||||
}),
|
||||
} {
|
||||
watches, err := ctrl.service.ListAllIssueWatches(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("%s ListAllIssueWatches: %v", name, err)
|
||||
}
|
||||
if len(watches) != 2 {
|
||||
t.Errorf("%s watches = %+v, want both workspaces", name, watches)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package sentry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/kandev/kandev/internal/task/repository/repoerrors"
|
||||
)
|
||||
|
||||
// denyOnly returns a workspace authorizer that allows every workspace except
|
||||
// the named ones, which it denies with the same ErrWorkspaceNotFound the real
|
||||
// task-service authorizer returns. Mirrors internal/jira/scope_test.go.
|
||||
func denyOnly(denied ...string) func(context.Context, string) error {
|
||||
blocked := make(map[string]bool, len(denied))
|
||||
for _, ws := range denied {
|
||||
blocked[ws] = true
|
||||
}
|
||||
return func(_ context.Context, ws string) error {
|
||||
if blocked[ws] {
|
||||
return repoerrors.ErrWorkspaceNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// TestListAllIssueWatches_FiltersToAccessibleWorkspaces is the regression for
|
||||
// the gap noted in AGENTS.md's "Per-user scoping" section: ListAllIssueWatches
|
||||
// was a bare passthrough to the store with no workspace filter, so
|
||||
// GET /api/v1/sentry/watches/issue with workspace_id omitted returned every
|
||||
// workspace's watch config to any authenticated caller.
|
||||
func TestListAllIssueWatches_FiltersToAccessibleWorkspaces(t *testing.T) {
|
||||
f := newSvcFixture(t)
|
||||
ctx := context.Background()
|
||||
seed := func(ws, projectSlug string) {
|
||||
if err := f.store.CreateIssueWatch(ctx, &IssueWatch{
|
||||
WorkspaceID: ws, WorkflowID: "wf", WorkflowStepID: "step",
|
||||
Filter: SearchFilter{OrgSlug: "org", ProjectSlugs: []string{projectSlug}}, AgentProfileID: "ap", Enabled: true,
|
||||
}); err != nil {
|
||||
t.Fatalf("seed watch: %v", err)
|
||||
}
|
||||
}
|
||||
seed("ws-allowed", "proj1")
|
||||
seed("ws-allowed", "proj2")
|
||||
seed("ws-denied", "proj3")
|
||||
f.svc.SetWorkspaceAuthorizer(denyOnly("ws-denied"))
|
||||
|
||||
watches, err := f.svc.ListAllIssueWatches(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ListAllIssueWatches: %v", err)
|
||||
}
|
||||
if len(watches) != 2 {
|
||||
t.Fatalf("watches = %+v, want both of ws-allowed's watches", watches)
|
||||
}
|
||||
for _, w := range watches {
|
||||
if w.WorkspaceID != "ws-allowed" {
|
||||
t.Fatalf("watches = %+v, want only ws-allowed's watches", watches)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestListAllIssueWatches_NilAuthorizerReturnsEveryWorkspace preserves the
|
||||
// identity-less internal caller behaviour: an unscoped caller of
|
||||
// ListAllIssueWatches must keep seeing every watch when no authorizer is
|
||||
// wired. (Sentry's own poller calls Store().ListEnabledIssueWatches directly
|
||||
// and does not go through this path.) Mirrors
|
||||
// TestIssueAndPRAllWorkspaceListsRetainIdentitylessInternalUse
|
||||
// (internal/github/service_workspace_authorization_test.go).
|
||||
func TestListAllIssueWatches_NilAuthorizerReturnsEveryWorkspace(t *testing.T) {
|
||||
f := newSvcFixture(t)
|
||||
ctx := context.Background()
|
||||
for _, ws := range []string{"ws-1", "ws-2"} {
|
||||
if err := f.store.CreateIssueWatch(ctx, &IssueWatch{
|
||||
WorkspaceID: ws, WorkflowID: "wf", WorkflowStepID: "step",
|
||||
Filter: SearchFilter{OrgSlug: "org", ProjectSlugs: []string{"proj"}}, AgentProfileID: "ap", Enabled: true,
|
||||
}); err != nil {
|
||||
t.Fatalf("seed watch: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
watches, err := f.svc.ListAllIssueWatches(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ListAllIssueWatches: %v", err)
|
||||
}
|
||||
if len(watches) != 2 {
|
||||
t.Fatalf("watches = %+v, want 2 (nil authorizer must stay unscoped for internal callers like the poller)", watches)
|
||||
}
|
||||
}
|
||||
|
||||
// TestListAllIssueWatches_PropagatesAuthorizerError is the regression for a
|
||||
// non-denial authorizer error (e.g. a transient DB failure): it must surface,
|
||||
// not be treated as a silent per-watch denial that returns a truncated 200.
|
||||
// Mirrors internal/jira/scope_watch_test.go.
|
||||
func TestListAllIssueWatches_PropagatesAuthorizerError(t *testing.T) {
|
||||
f := newSvcFixture(t)
|
||||
ctx := context.Background()
|
||||
if err := f.store.CreateIssueWatch(ctx, &IssueWatch{
|
||||
WorkspaceID: "ws-1", WorkflowID: "wf", WorkflowStepID: "step",
|
||||
Filter: SearchFilter{OrgSlug: "org", ProjectSlugs: []string{"proj"}}, AgentProfileID: "ap", Enabled: true,
|
||||
}); err != nil {
|
||||
t.Fatalf("seed watch: %v", err)
|
||||
}
|
||||
boom := errors.New("authorizer backend down")
|
||||
f.svc.SetWorkspaceAuthorizer(func(context.Context, string) error { return boom })
|
||||
|
||||
if _, err := f.svc.ListAllIssueWatches(ctx); !errors.Is(err, boom) {
|
||||
t.Fatalf("expected the authorizer error to propagate, got %v", err)
|
||||
}
|
||||
}
|
||||
@@ -87,6 +87,30 @@ type Service struct {
|
||||
// Wired post-construction via SetRepositoryLookup. When nil (e.g. unit
|
||||
// tests), the binding is accepted as-is and default-branch fill is skipped.
|
||||
repoLookup RepositoryLookup
|
||||
// workspaceAuthorizer is the per-user workspace access boundary, wired
|
||||
// post-construction via SetWorkspaceAuthorizer. Nil (unit tests, auth
|
||||
// disabled) means unscoped — every workspace is visible, as before auth.
|
||||
workspaceAuthorizer func(context.Context, string) error
|
||||
}
|
||||
|
||||
// SetWorkspaceAuthorizer installs the per-user workspace access boundary
|
||||
// applied to ListAllIssueWatches. Wired to taskSvc.AuthorizeWorkspaceAccess so
|
||||
// an unscoped list (workspace_id omitted) returns only the caller's own
|
||||
// workspaces' watches.
|
||||
func (s *Service) SetWorkspaceAuthorizer(authorizer func(context.Context, string) error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.workspaceAuthorizer = authorizer
|
||||
}
|
||||
|
||||
func (s *Service) authorizeWorkspaceAccess(ctx context.Context, workspaceID string) error {
|
||||
s.mu.Lock()
|
||||
authorizer := s.workspaceAuthorizer
|
||||
s.mu.Unlock()
|
||||
if authorizer == nil {
|
||||
return nil
|
||||
}
|
||||
return authorizer(ctx, workspaceID)
|
||||
}
|
||||
|
||||
// SetTaskDeleter wires the cascade-delete dependency used by ResetIssueWatch.
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/kandev/kandev/internal/common/securityutil"
|
||||
"github.com/kandev/kandev/internal/events"
|
||||
"github.com/kandev/kandev/internal/events/bus"
|
||||
"github.com/kandev/kandev/internal/task/repository/repoerrors"
|
||||
"github.com/kandev/kandev/internal/watchreset"
|
||||
)
|
||||
|
||||
@@ -71,9 +72,44 @@ func (s *Service) ListIssueWatches(ctx context.Context, workspaceID string) ([]*
|
||||
return s.store.ListIssueWatches(ctx, workspaceID)
|
||||
}
|
||||
|
||||
// ListAllIssueWatches returns every watch across all workspaces.
|
||||
// ListAllIssueWatches returns every watch the caller may see. For a scoped
|
||||
// caller that is only their own workspaces' watches; for an identity-less
|
||||
// internal caller (unscoped) it is every watch, as before auth.
|
||||
func (s *Service) ListAllIssueWatches(ctx context.Context) ([]*IssueWatch, error) {
|
||||
return s.store.ListAllIssueWatches(ctx)
|
||||
watches, err := s.store.ListAllIssueWatches(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.filterIssueWatchesByAccess(ctx, watches)
|
||||
}
|
||||
|
||||
// filterIssueWatchesByAccess keeps only watches whose workspace the caller may
|
||||
// access. Access decisions are memoized per workspace so a long list costs one
|
||||
// authorize call per distinct workspace, not one per watch. Only an
|
||||
// ErrWorkspaceNotFound denial drops a watch; any other authorizer error (a
|
||||
// transient DB failure, say) is propagated so the caller sees the failure
|
||||
// rather than a silently truncated 200.
|
||||
func (s *Service) filterIssueWatchesByAccess(ctx context.Context, watches []*IssueWatch) ([]*IssueWatch, error) {
|
||||
decision := make(map[string]bool)
|
||||
visible := make([]*IssueWatch, 0, len(watches))
|
||||
for _, w := range watches {
|
||||
allowed, seen := decision[w.WorkspaceID]
|
||||
if !seen {
|
||||
switch err := s.authorizeWorkspaceAccess(ctx, w.WorkspaceID); {
|
||||
case err == nil:
|
||||
allowed = true
|
||||
case errors.Is(err, repoerrors.ErrWorkspaceNotFound):
|
||||
allowed = false
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
decision[w.WorkspaceID] = allowed
|
||||
}
|
||||
if allowed {
|
||||
visible = append(visible, w)
|
||||
}
|
||||
}
|
||||
return visible, nil
|
||||
}
|
||||
|
||||
// GetIssueWatch returns a single watch by ID or ErrIssueWatchNotFound.
|
||||
|
||||
Reference in New Issue
Block a user