From e3dc7fc2e9839f2fbf08b0dfdc5d1440c9d60f50 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Mon, 9 Nov 2026 12:43:44 +1100 Subject: [PATCH] fix(issuerpc): list only issues opened by users with write access (#1830) Adds Issue.Author to the forge contract; the daemon drops issues from authors without write access and fails the listing closed when the forge cannot answer. --- internal/contracts/forge/v1/forge.pb.go | 13 +++- internal/forge/forge.go | 2 + internal/infrastructure/forgeext/forgeext.go | 2 +- internal/infrastructure/issuerpc/issuerpc.go | 32 ++++++++++ .../infrastructure/issuerpc/issuerpc_test.go | 61 +++++++++++++++++++ proto/forge/v1/forge.proto | 1 + 6 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 internal/infrastructure/issuerpc/issuerpc_test.go diff --git a/internal/contracts/forge/v1/forge.pb.go b/internal/contracts/forge/v1/forge.pb.go index a399c5941..d73928d65 100644 --- a/internal/contracts/forge/v1/forge.pb.go +++ b/internal/contracts/forge/v1/forge.pb.go @@ -228,6 +228,7 @@ type Issue struct { Title string `protobuf:"bytes,2,opt,name=title,proto3" json:"title,omitempty"` Body string `protobuf:"bytes,3,opt,name=body,proto3" json:"body,omitempty"` Labels []string `protobuf:"bytes,4,rep,name=labels,proto3" json:"labels,omitempty"` + Author string `protobuf:"bytes,5,opt,name=author,proto3" json:"author,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } @@ -290,6 +291,13 @@ func (x *Issue) GetLabels() []string { return nil } +func (x *Issue) GetAuthor() string { + if x != nil { + return x.Author + } + return "" +} + type AssignedIssuesResponse struct { state protoimpl.MessageState `protogen:"open.v1"` Issues []*Issue `protobuf:"bytes,1,rep,name=issues,proto3" json:"issues,omitempty"` @@ -2918,12 +2926,13 @@ const file_forge_v1_forge_proto_rawDesc = "" + "\x04repo\x18\x02 \x01(\tR\x04repo\"O\n" + "\x0ePullRequestRef\x12%\n" + "\x04repo\x18\x01 \x01(\v2\x11.forge.v1.RepoRefR\x04repo\x12\x16\n" + - "\x06number\x18\x02 \x01(\x05R\x06number\"a\n" + + "\x06number\x18\x02 \x01(\x05R\x06number\"y\n" + "\x05Issue\x12\x16\n" + "\x06number\x18\x01 \x01(\x05R\x06number\x12\x14\n" + "\x05title\x18\x02 \x01(\tR\x05title\x12\x12\n" + "\x04body\x18\x03 \x01(\tR\x04body\x12\x16\n" + - "\x06labels\x18\x04 \x03(\tR\x06labels\"A\n" + + "\x06labels\x18\x04 \x03(\tR\x06labels\x12\x16\n" + + "\x06author\x18\x05 \x01(\tR\x06author\"A\n" + "\x16AssignedIssuesResponse\x12'\n" + "\x06issues\x18\x01 \x03(\v2\x0f.forge.v1.IssueR\x06issues\"B\n" + "\x17IssuesWithLabelResponse\x12'\n" + diff --git a/internal/forge/forge.go b/internal/forge/forge.go index a4f4049d6..170dc6a25 100644 --- a/internal/forge/forge.go +++ b/internal/forge/forge.go @@ -15,6 +15,8 @@ type Issue struct { Title string Body string Labels []string + // Author is the login that opened the issue. + Author string } // Forge is the interface for interacting with a git host. diff --git a/internal/infrastructure/forgeext/forgeext.go b/internal/infrastructure/forgeext/forgeext.go index 0f89156ce..e88b2665a 100644 --- a/internal/infrastructure/forgeext/forgeext.go +++ b/internal/infrastructure/forgeext/forgeext.go @@ -163,7 +163,7 @@ func prRef(owner, repo string, number int) *forgev1.PullRequestRef { func issues(in []*forgev1.Issue) []forge.Issue { out := make([]forge.Issue, 0, len(in)) for _, i := range in { - out = append(out, forge.Issue{Number: int(i.GetNumber()), Title: i.GetTitle(), Body: i.GetBody(), Labels: i.GetLabels()}) + out = append(out, forge.Issue{Number: int(i.GetNumber()), Title: i.GetTitle(), Body: i.GetBody(), Labels: i.GetLabels(), Author: i.GetAuthor()}) } return out } diff --git a/internal/infrastructure/issuerpc/issuerpc.go b/internal/infrastructure/issuerpc/issuerpc.go index 18bb14fd3..9183164d3 100644 --- a/internal/infrastructure/issuerpc/issuerpc.go +++ b/internal/infrastructure/issuerpc/issuerpc.go @@ -72,6 +72,9 @@ func (s *Server) Register(nc *nats.Conn) (func(), error) { if err == nil { issues, err = f.IssuesWithLabel(ctx, req.Owner, req.Repo, "") } + if err == nil { + issues, err = writerIssues(ctx, s.Log, f, req.Owner, req.Repo, issues) + } natsrpc.Respond(msg, s.Log, "issuerpc", listResponse{Envelope: natsrpc.NewEnvelope(err), Issues: issues}) }}, {Subject: createSubject, Handler: func(ctx context.Context, msg *nats.Msg) { @@ -121,3 +124,32 @@ func (c Client) CreateIssue(ctx context.Context, owner, repo, title, body string } return resp.Number, nil } + +// writerIssues keeps the issues opened by users with write access: anyone can +// open an issue on a public repository, and agents act on what they read. A +// forge that cannot answer fails the listing closed. +func writerIssues(ctx context.Context, log *slog.Logger, f forge.IssueForge, owner, repo string, issues []forge.Issue) ([]forge.Issue, error) { + checker, ok := f.(forge.WriteAccessChecker) + if !ok { + return nil, errors.New("forge cannot check write access") + } + known := map[string]bool{} + out := issues[:0:0] + for _, is := range issues { + allowed, seen := known[is.Author] + if !seen { + var err error + if allowed, err = checker.HasWriteAccess(ctx, owner, repo, is.Author); err != nil { + return nil, fmt.Errorf("check write access for %s: %w", is.Author, err) + } + known[is.Author] = allowed + } + if allowed { + out = append(out, is) + continue + } + log.WarnContext(ctx, "dropped issue from author without write access", + "repo", owner+"/"+repo, "issue", is.Number, "author", is.Author) + } + return out, nil +} diff --git a/internal/infrastructure/issuerpc/issuerpc_test.go b/internal/infrastructure/issuerpc/issuerpc_test.go new file mode 100644 index 000000000..cd7df2235 --- /dev/null +++ b/internal/infrastructure/issuerpc/issuerpc_test.go @@ -0,0 +1,61 @@ +package issuerpc + +import ( + "context" + "errors" + "log/slog" + "testing" + + "github.com/samcharles93/archie-core/internal/forge" +) + +type writerForge struct { + forge.IssueForge + writers map[string]bool + err error +} + +func (f writerForge) HasWriteAccess(_ context.Context, _, _, user string) (bool, error) { + return f.writers[user], f.err +} + +type plainForge struct{ forge.IssueForge } + +func TestWriterIssues(t *testing.T) { + issues := []forge.Issue{ + {Number: 1, Author: "maint"}, + {Number: 2, Author: "stranger"}, + {Number: 3, Author: "maint"}, + {Number: 4}, + } + tests := []struct { + name string + forge forge.IssueForge + want []int + wantErr bool + }{ + {"keeps collaborators only", writerForge{writers: map[string]bool{"maint": true}}, []int{1, 3}, false}, + {"fails closed on forge error", writerForge{err: errors.New("down")}, nil, true}, + {"fails closed without a checker", plainForge{}, nil, true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := writerIssues(t.Context(), slog.New(slog.DiscardHandler), tt.forge, "o", "r", issues) + if (err != nil) != tt.wantErr { + t.Fatalf("err = %v, wantErr %v", err, tt.wantErr) + } + var nums []int + for _, is := range got { + nums = append(nums, is.Number) + } + if len(nums) != len(tt.want) { + t.Fatalf("got %v, want %v", nums, tt.want) + } + for i := range nums { + if nums[i] != tt.want[i] { + t.Fatalf("got %v, want %v", nums, tt.want) + } + } + }) + } +} diff --git a/proto/forge/v1/forge.proto b/proto/forge/v1/forge.proto index 5f822db36..ec6b3339a 100644 --- a/proto/forge/v1/forge.proto +++ b/proto/forge/v1/forge.proto @@ -70,6 +70,7 @@ message Issue { string title = 2; string body = 3; repeated string labels = 4; + string author = 5; } message AssignedIssuesResponse { repeated Issue issues = 1; } message IssuesWithLabelResponse { repeated Issue issues = 1; }