fix(daemon): publish polled issues only from authors with write access (#1830)
The issue poller published title and body of every labelled or assigned issue to the agent. It now keeps only write-access authors, failing closed when the forge cannot answer. The filter moves to forge.WriterIssues, shared with the issue_list tool.
This commit is contained in:
6 files changed
+96
-40
No files matched your search
@@ -2,6 +2,7 @@ package daemon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"testing"
|
||||
|
||||
@@ -23,6 +24,18 @@ func (s taskLookup) TaskByIssue(_ context.Context, _, _ string, number int) (*wo
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// writerForge answers write access from a fixed set of logins; the rest of
|
||||
// the Forge surface is unused.
|
||||
type writerForge struct {
|
||||
forge.Forge
|
||||
writers map[string]bool
|
||||
err error
|
||||
}
|
||||
|
||||
func (f writerForge) HasWriteAccess(_ context.Context, _, _, user string) (bool, error) {
|
||||
return f.writers[user], f.err
|
||||
}
|
||||
|
||||
type publishCounter struct {
|
||||
stubTasks
|
||||
published int
|
||||
@@ -62,7 +75,39 @@ func TestPublishDiscoveredCap(t *testing.T) {
|
||||
}
|
||||
room := tt.cap
|
||||
repo := config.Repo{Owner: "acme", Name: "widget"}
|
||||
d.IssuePoller.publishDiscovered(t.Context(), nil, config.Config{}, repo, issues, string(sys), &room)
|
||||
d.IssuePoller.publishDiscovered(t.Context(), writerForge{writers: map[string]bool{"": true}}, config.Config{}, repo, issues, string(sys), &room)
|
||||
if bus.published != tt.want {
|
||||
t.Fatalf("published %d tasks, want %d", bus.published, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Forge text reaches an agent only from authors with write access: a labelled
|
||||
// or assigned issue opened by a stranger is not published, and an unanswerable
|
||||
// forge publishes nothing. Guards a public-repo stranger steering an agent
|
||||
// through an issue body.
|
||||
func TestPublishDiscoveredDropsNonWriters(t *testing.T) {
|
||||
issues := []forge.Issue{
|
||||
{Number: 1, Author: "maint"},
|
||||
{Number: 2, Author: "stranger"},
|
||||
{Number: 3, Author: "maint"},
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
forge writerForge
|
||||
want int
|
||||
}{
|
||||
{"stranger dropped", writerForge{writers: map[string]bool{"maint": true}}, 2},
|
||||
{"forge error publishes nothing", writerForge{err: errors.New("down")}, 0},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
bus := &publishCounter{}
|
||||
d := wired(&Daemon{Log: slog.New(slog.DiscardHandler), Store: taskLookup{}, TaskTransport: bus})
|
||||
room := 0
|
||||
repo := config.Repo{Owner: "acme", Name: "widget"}
|
||||
d.IssuePoller.publishDiscovered(t.Context(), tt.forge, config.Config{}, repo, issues, string(sys), &room)
|
||||
if bus.published != tt.want {
|
||||
t.Fatalf("published %d tasks, want %d", bus.published, tt.want)
|
||||
}
|
||||
|
||||
@@ -205,6 +205,14 @@ func (p *IssuePoller) withdrawUnpolled(ctx context.Context, repo config.Repo, id
|
||||
// over are found again by the next poll, because an issue with a task is
|
||||
// skipped and one without is not. A cap of zero or less is no cap.
|
||||
func (p *IssuePoller) publishDiscovered(ctx context.Context, fg forge.Forge, cfg config.Config, repo config.Repo, issues []forge.Issue, identity string, room *int) {
|
||||
// A label or assignment is an operator's act, but the title and body that
|
||||
// reach the agent were written by the issue's author, who on a public
|
||||
// repository can be anyone.
|
||||
issues, err := forge.WriterIssues(ctx, p.log, fg, repo.Owner, repo.Name, issues)
|
||||
if err != nil {
|
||||
p.log.ErrorContext(ctx, "issues not published: author write access unknown", "repo", repo.FullName(), "err", err)
|
||||
return
|
||||
}
|
||||
capped := *room > 0
|
||||
for _, is := range issues {
|
||||
if capped && *room == 0 {
|
||||
|
||||
@@ -20,6 +20,10 @@ func (f labelledIssues) IssuesWithLabel(context.Context, string, string, string)
|
||||
return f.issues, nil
|
||||
}
|
||||
|
||||
func (labelledIssues) HasWriteAccess(context.Context, string, string, string) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (labelledIssues) React(context.Context, string, string, int, string) error { return nil }
|
||||
|
||||
type publishedKeys struct {
|
||||
|
||||
@@ -6,7 +6,9 @@ package forge
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -188,6 +190,35 @@ type WriteAccessChecker interface {
|
||||
HasWriteAccess(ctx context.Context, owner, repo, user string) (bool, error)
|
||||
}
|
||||
|
||||
// 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 closed, returning an error and no issues.
|
||||
func WriterIssues(ctx context.Context, log *slog.Logger, f IssueForge, owner, repo string, issues []Issue) ([]Issue, error) {
|
||||
checker, ok := f.(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
|
||||
}
|
||||
|
||||
// InlineReviewComment is one line-anchored comment to post on a PR: the file,
|
||||
// the line in the reviewed commit's version of it, and the body -- which may
|
||||
// carry a fenced suggestion block the author can apply in one click.
|
||||
|
||||
+6
-8
@@ -1,16 +1,14 @@
|
||||
package issuerpc
|
||||
package forge
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/forge"
|
||||
)
|
||||
|
||||
type writerForge struct {
|
||||
forge.IssueForge
|
||||
IssueForge
|
||||
writers map[string]bool
|
||||
err error
|
||||
}
|
||||
@@ -19,10 +17,10 @@ func (f writerForge) HasWriteAccess(_ context.Context, _, _, user string) (bool,
|
||||
return f.writers[user], f.err
|
||||
}
|
||||
|
||||
type plainForge struct{ forge.IssueForge }
|
||||
type plainForge struct{ IssueForge }
|
||||
|
||||
func TestWriterIssues(t *testing.T) {
|
||||
issues := []forge.Issue{
|
||||
issues := []Issue{
|
||||
{Number: 1, Author: "maint"},
|
||||
{Number: 2, Author: "stranger"},
|
||||
{Number: 3, Author: "maint"},
|
||||
@@ -30,7 +28,7 @@ func TestWriterIssues(t *testing.T) {
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
forge forge.IssueForge
|
||||
forge IssueForge
|
||||
want []int
|
||||
wantErr bool
|
||||
}{
|
||||
@@ -40,7 +38,7 @@ func TestWriterIssues(t *testing.T) {
|
||||
}
|
||||
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)
|
||||
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)
|
||||
}
|
||||
@@ -5,7 +5,6 @@ package issuerpc
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
@@ -61,7 +60,7 @@ func (s *Server) Register(ctx context.Context, nc *nats.Conn) (func(), error) {
|
||||
issues, err = f.IssuesWithLabel(ctx, req.Owner, req.Repo, "")
|
||||
}
|
||||
if err == nil {
|
||||
issues, err = writerIssues(ctx, s.Log, f, req.Owner, req.Repo, issues)
|
||||
issues, err = forge.WriterIssues(ctx, s.Log, f, req.Owner, req.Repo, issues)
|
||||
}
|
||||
natsrpc.Respond(msg, s.Log, "issuerpc", listResponse{Envelope: natsrpc.NewEnvelope(err), Issues: issues})
|
||||
}},
|
||||
@@ -112,32 +111,3 @@ 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
|
||||
}
|
||||
Reference in new issue
Block a user