fix(forge): accept GitHub invitations only to configured repositories

AcceptInvitations accepted every pending repository invitation, so any
GitHub user could add the forge's account to a repository by sending an
invitation. The host now passes the connection's configured repositories
in AcceptInvitationsRequest and the extension leaves an invitation to
anything else pending. The Gitea forge is unchanged: it has no
invitations to accept.
This commit is contained in:
sam committed 2026-10-29 00:02:50 +11:00
1 parent 1c4ec39cd8
commit 60fbcd4c59
6 files changed
+212 -73

No files matched your search

+5 -4
View File
@@ -72,10 +72,11 @@ func notFound(resp *gitea.Response) bool {
// ── Forge interface ──────────────────────────────────────────────────
// AcceptInvitations auto-accepts pending repository invitations.
func (c *GiteaClient) AcceptInvitations(ctx context.Context) error {
// Gitea doesn't have a direct equivalent to GitHub's user invitations
// API. Repository collaborators are added directly by the owner.
// AcceptInvitations accepts pending repository invitations. Gitea doesn't
// have a direct equivalent to GitHub's user invitations API -- repository
// collaborators are added directly by the owner -- so there is nothing to
// accept and the allowlist is moot.
func (c *GiteaClient) AcceptInvitations(ctx context.Context, repos []forge.RepoRef) error {
return nil
}
+33 -6
View File
@@ -50,23 +50,50 @@ func newGitHub(token, host string, log *slog.Logger) (forge.Forge, error) {
return &GitHubClient{gh: client.WithAuthToken(token), log: log}, nil
}
// AcceptInvitations auto-accepts pending repository invitations so
// adding archie as a collaborator is all a human has to do.
func (c *GitHubClient) AcceptInvitations(ctx context.Context) error {
// AcceptInvitations accepts pending repository invitations, but only to the
// repositories the operator configured this connection for, so adding archie
// as a collaborator to a configured repository is all a human has to do. An
// invitation to anything else is left pending.
func (c *GitHubClient) AcceptInvitations(ctx context.Context, repos []forge.RepoRef) error {
invites, _, err := c.gh.Users.ListInvitations(ctx, nil)
if err != nil {
return apiError("list invitations", err)
}
for _, inv := range invites {
accept, pending := splitInvitations(invites, repos)
for _, inv := range pending {
c.log.Warn("ignoring repository invitation outside the configured repositories",
"repo", inv.GetRepo().GetFullName(), "inviter", inv.GetInviter().GetLogin())
}
for _, inv := range accept {
full := inv.GetRepo().GetFullName()
if _, err := c.gh.Users.AcceptInvitation(ctx, inv.GetID()); err != nil {
c.log.Warn("accept invitation failed", "repo", inv.GetRepo().GetFullName(), "err", apiError("accept invitation", err))
c.log.Warn("accept invitation failed", "repo", full, "err", apiError("accept invitation", err))
continue
}
c.log.Info("accepted repository invitation", "repo", inv.GetRepo().GetFullName())
c.log.Info("accepted repository invitation", "repo", full)
}
return nil
}
// splitInvitations partitions pending invitations into those to accept --
// invitations to repositories the operator configured -- and those to leave
// pending. Matching is on the lower-cased "owner/repo" full name, because
// GitHub names are case-insensitive.
func splitInvitations(invites []*github.RepositoryInvitation, repos []forge.RepoRef) (accept, pending []*github.RepositoryInvitation) {
allowed := make(map[string]bool, len(repos))
for _, r := range repos {
allowed[strings.ToLower(r.Owner+"/"+r.Repo)] = true
}
for _, inv := range invites {
if allowed[strings.ToLower(inv.GetRepo().GetFullName())] {
accept = append(accept, inv)
} else {
pending = append(pending, inv)
}
}
return accept, pending
}
// AssignedIssues returns open issues assigned to the given user,
// excluding PRs. Assigning an issue to the bot is how work is handed to
// archie (tink-bot style); labels only influence workflow routing.
+84
View File
@@ -0,0 +1,84 @@
package main
import (
"slices"
"testing"
"github.com/google/go-github/v78/github"
"github.com/samcharles93/archipelago/sdk/forge"
)
func TestSplitInvitations(t *testing.T) {
invitation := func(id int64, fullName string) *github.RepositoryInvitation {
return &github.RepositoryInvitation{ID: new(id), Repo: &github.Repository{FullName: new(fullName)}}
}
ids := func(invites []*github.RepositoryInvitation) []int64 {
out := make([]int64, 0, len(invites))
for _, inv := range invites {
out = append(out, inv.GetID())
}
return out
}
configured := []forge.RepoRef{{Owner: "samcharles93", Repo: "archie-core"}, {Owner: "acme", Repo: "widgets"}}
for _, tc := range []struct {
name string
invites []*github.RepositoryInvitation
repos []forge.RepoRef
wantAccepted []int64
wantPending []int64
}{
{
name: "invitation to a configured repository is accepted",
invites: []*github.RepositoryInvitation{invitation(1, "samcharles93/archie-core")},
repos: configured,
wantAccepted: []int64{1},
},
{
name: "matching is case-insensitive",
invites: []*github.RepositoryInvitation{invitation(1, "SamCharles93/Archie-Core")},
repos: configured,
wantAccepted: []int64{1},
},
{
name: "same owner, different repository stays pending",
invites: []*github.RepositoryInvitation{invitation(1, "samcharles93/other")},
repos: configured,
wantPending: []int64{1},
},
{
name: "different owner, same repository name stays pending",
invites: []*github.RepositoryInvitation{invitation(1, "mallory/archie-core")},
repos: configured,
wantPending: []int64{1},
},
{
name: "no configured repositories accepts nothing",
invites: []*github.RepositoryInvitation{invitation(1, "samcharles93/archie-core")},
repos: nil,
wantPending: []int64{1},
},
{
name: "a mixed batch is partitioned",
invites: []*github.RepositoryInvitation{invitation(1, "acme/widgets"), invitation(2, "mallory/archie-core"), invitation(3, "samcharles93/archie-core")},
repos: configured,
wantAccepted: []int64{1, 3},
wantPending: []int64{2},
},
{
name: "no invitations accepts nothing",
invites: nil,
repos: configured,
},
} {
t.Run(tc.name, func(t *testing.T) {
accept, pending := splitInvitations(tc.invites, tc.repos)
if got := ids(accept); !slices.Equal(got, tc.wantAccepted) {
t.Errorf("accepted = %v, want %v", got, tc.wantAccepted)
}
if got := ids(pending); !slices.Equal(got, tc.wantPending) {
t.Errorf("pending = %v, want %v", got, tc.wantPending)
}
})
}
}
+11 -1
View File
@@ -24,6 +24,12 @@ type Issue struct {
Labels []string
}
// RepoRef identifies one repository on a forge.
type RepoRef struct {
Owner string
Repo string
}
// Forge is the capability every forge extension has.
type Forge interface {
AssignedIssues(ctx context.Context, owner, repo, assignee string) ([]Issue, error)
@@ -42,7 +48,11 @@ type Forge interface {
ClosePR(ctx context.Context, owner, repo string, number int, comment string) error
// MergePR merges a pull request with the forge's default method.
MergePR(ctx context.Context, owner, repo string, number int) error
AcceptInvitations(ctx context.Context) error
// AcceptInvitations accepts pending repository invitations, but only to
// the given repositories: an invitation to anything else is left
// pending, so a stranger cannot add the forge's account to a repository
// the operator never configured.
AcceptInvitations(ctx context.Context, repos []RepoRef) error
VerifyPush(ctx context.Context, owner, repo string) error
LinkBranch(ctx context.Context, owner, repo string, issueNumber int, branch string) error
}
+6 -2
View File
@@ -254,12 +254,16 @@ func (s *server) CreateReviewComments(ctx context.Context, r *forgev1.CreateRevi
return &forgev1.CreateReviewCommentsResponse{}, fail(c.CreateReviewComments(ctx, pr.GetRepo().GetOwner(), pr.GetRepo().GetRepo(), int(pr.GetNumber()), r.GetReviewedHeadSha(), ReviewSubmission{Event: event, Body: r.GetBody(), Comments: comments}))
}
func (s *server) AcceptInvitations(ctx context.Context, _ *forgev1.AcceptInvitationsRequest) (*forgev1.AcceptInvitationsResponse, error) {
func (s *server) AcceptInvitations(ctx context.Context, r *forgev1.AcceptInvitationsRequest) (*forgev1.AcceptInvitationsResponse, error) {
f, err := s.forge()
if err != nil {
return nil, err
}
return &forgev1.AcceptInvitationsResponse{}, fail(f.AcceptInvitations(ctx))
repos := make([]RepoRef, 0, len(r.GetRepositories()))
for _, repo := range r.GetRepositories() {
repos = append(repos, RepoRef{Owner: repo.GetOwner(), Repo: repo.GetRepo()})
}
return &forgev1.AcceptInvitationsResponse{}, fail(f.AcceptInvitations(ctx, repos))
}
func (s *server) VerifyPush(ctx context.Context, r *forgev1.VerifyPushRequest) (*forgev1.VerifyPushResponse, error) {
+73 -60
View File
@@ -2311,8 +2311,12 @@ func (*LinkBranchResponse) Descriptor() ([]byte, []int) {
return file_forge_v1_forge_proto_rawDescGZIP(), []int{42}
}
// AcceptInvitationsRequest carries the repositories the operator configured
// this connection for. Only invitations to these are accepted; an invitation
// to anything else stays pending.
type AcceptInvitationsRequest struct {
state protoimpl.MessageState `protogen:"open.v1"`
Repositories []*RepoRef `protobuf:"bytes,1,rep,name=repositories,proto3" json:"repositories,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
@@ -2347,6 +2351,13 @@ func (*AcceptInvitationsRequest) Descriptor() ([]byte, []int) {
return file_forge_v1_forge_proto_rawDescGZIP(), []int{43}
}
func (x *AcceptInvitationsRequest) GetRepositories() []*RepoRef {
if x != nil {
return x.Repositories
}
return nil
}
type AcceptInvitationsResponse struct {
state protoimpl.MessageState `protogen:"open.v1"`
unknownFields protoimpl.UnknownFields
@@ -3161,8 +3172,9 @@ const file_forge_v1_forge_proto_rawDesc = "" +
"\x04repo\x18\x01 \x01(\v2\x11.forge.v1.RepoRefR\x04repo\x12!\n" +
"\fissue_number\x18\x02 \x01(\x05R\vissueNumber\x12\x16\n" +
"\x06branch\x18\x03 \x01(\tR\x06branch\"\x14\n" +
"\x12LinkBranchResponse\"\x1a\n" +
"\x18AcceptInvitationsRequest\"\x1b\n" +
"\x12LinkBranchResponse\"Q\n" +
"\x18AcceptInvitationsRequest\x125\n" +
"\frepositories\x18\x01 \x03(\v2\x11.forge.v1.RepoRefR\frepositories\"\x1b\n" +
"\x19AcceptInvitationsResponse\":\n" +
"\x11VerifyPushRequest\x12%\n" +
"\x04repo\x18\x01 \x01(\v2\x11.forge.v1.RepoRefR\x04repo\"\x14\n" +
@@ -3342,64 +3354,65 @@ var file_forge_v1_forge_proto_depIdxs = []int32{
3, // 24: forge.v1.CreateReviewCommentsRequest.pull_request:type_name -> forge.v1.PullRequestRef
38, // 25: forge.v1.CreateReviewCommentsRequest.comments:type_name -> forge.v1.InlineReviewComment
2, // 26: forge.v1.LinkBranchRequest.repo:type_name -> forge.v1.RepoRef
2, // 27: forge.v1.VerifyPushRequest.repo:type_name -> forge.v1.RepoRef
2, // 28: forge.v1.GetRepoArchiveRequest.repo:type_name -> forge.v1.RepoRef
55, // 29: forge.v1.ParseWebhookRequest.headers:type_name -> forge.v1.ParseWebhookRequest.HeadersEntry
51, // 30: forge.v1.ParseWebhookResponse.issue:type_name -> forge.v1.IssueEvent
52, // 31: forge.v1.ParseWebhookResponse.review:type_name -> forge.v1.ReviewEvent
53, // 32: forge.v1.ParseWebhookResponse.review_comment:type_name -> forge.v1.ReviewCommentEvent
2, // 33: forge.v1.IssueEvent.repo:type_name -> forge.v1.RepoRef
2, // 34: forge.v1.ReviewEvent.repo:type_name -> forge.v1.RepoRef
2, // 35: forge.v1.ReviewCommentEvent.repo:type_name -> forge.v1.RepoRef
0, // 36: forge.v1.ForgeService.Configure:input_type -> forge.v1.ConfigureRequest
7, // 37: forge.v1.ForgeService.AssignedIssues:input_type -> forge.v1.AssignedIssuesRequest
8, // 38: forge.v1.ForgeService.IssuesWithLabel:input_type -> forge.v1.IssuesWithLabelRequest
9, // 39: forge.v1.ForgeService.Comment:input_type -> forge.v1.CommentRequest
11, // 40: forge.v1.ForgeService.CloseIssue:input_type -> forge.v1.CloseIssueRequest
17, // 41: forge.v1.ForgeService.React:input_type -> forge.v1.ReactRequest
19, // 42: forge.v1.ForgeService.SetStateLabel:input_type -> forge.v1.SetStateLabelRequest
21, // 43: forge.v1.ForgeService.CreatePR:input_type -> forge.v1.CreatePRRequest
23, // 44: forge.v1.ForgeService.PRState:input_type -> forge.v1.PRStateRequest
13, // 45: forge.v1.ForgeService.ClosePR:input_type -> forge.v1.ClosePRRequest
15, // 46: forge.v1.ForgeService.MergePR:input_type -> forge.v1.MergePRRequest
26, // 47: forge.v1.ForgeService.GetPullRequest:input_type -> forge.v1.GetPullRequestRequest
28, // 48: forge.v1.ForgeService.GetPullRequestDiff:input_type -> forge.v1.GetPullRequestDiffRequest
30, // 49: forge.v1.ForgeService.ListReviews:input_type -> forge.v1.ListReviewsRequest
34, // 50: forge.v1.ForgeService.ListReviewComments:input_type -> forge.v1.ListReviewCommentsRequest
36, // 51: forge.v1.ForgeService.ReplyToReview:input_type -> forge.v1.ReplyToReviewRequest
39, // 52: forge.v1.ForgeService.CreateReviewComments:input_type -> forge.v1.CreateReviewCommentsRequest
43, // 53: forge.v1.ForgeService.AcceptInvitations:input_type -> forge.v1.AcceptInvitationsRequest
45, // 54: forge.v1.ForgeService.VerifyPush:input_type -> forge.v1.VerifyPushRequest
41, // 55: forge.v1.ForgeService.LinkBranch:input_type -> forge.v1.LinkBranchRequest
49, // 56: forge.v1.ForgeService.ParseWebhook:input_type -> forge.v1.ParseWebhookRequest
47, // 57: forge.v1.ForgeService.GetRepoArchive:input_type -> forge.v1.GetRepoArchiveRequest
1, // 58: forge.v1.ForgeService.Configure:output_type -> forge.v1.ConfigureResponse
5, // 59: forge.v1.ForgeService.AssignedIssues:output_type -> forge.v1.AssignedIssuesResponse
6, // 60: forge.v1.ForgeService.IssuesWithLabel:output_type -> forge.v1.IssuesWithLabelResponse
10, // 61: forge.v1.ForgeService.Comment:output_type -> forge.v1.CommentResponse
12, // 62: forge.v1.ForgeService.CloseIssue:output_type -> forge.v1.CloseIssueResponse
18, // 63: forge.v1.ForgeService.React:output_type -> forge.v1.ReactResponse
20, // 64: forge.v1.ForgeService.SetStateLabel:output_type -> forge.v1.SetStateLabelResponse
22, // 65: forge.v1.ForgeService.CreatePR:output_type -> forge.v1.CreatePRResponse
24, // 66: forge.v1.ForgeService.PRState:output_type -> forge.v1.PRStateResponse
14, // 67: forge.v1.ForgeService.ClosePR:output_type -> forge.v1.ClosePRResponse
16, // 68: forge.v1.ForgeService.MergePR:output_type -> forge.v1.MergePRResponse
27, // 69: forge.v1.ForgeService.GetPullRequest:output_type -> forge.v1.GetPullRequestResponse
29, // 70: forge.v1.ForgeService.GetPullRequestDiff:output_type -> forge.v1.GetPullRequestDiffResponse
32, // 71: forge.v1.ForgeService.ListReviews:output_type -> forge.v1.ListReviewsResponse
35, // 72: forge.v1.ForgeService.ListReviewComments:output_type -> forge.v1.ListReviewCommentsResponse
37, // 73: forge.v1.ForgeService.ReplyToReview:output_type -> forge.v1.ReplyToReviewResponse
40, // 74: forge.v1.ForgeService.CreateReviewComments:output_type -> forge.v1.CreateReviewCommentsResponse
44, // 75: forge.v1.ForgeService.AcceptInvitations:output_type -> forge.v1.AcceptInvitationsResponse
46, // 76: forge.v1.ForgeService.VerifyPush:output_type -> forge.v1.VerifyPushResponse
42, // 77: forge.v1.ForgeService.LinkBranch:output_type -> forge.v1.LinkBranchResponse
50, // 78: forge.v1.ForgeService.ParseWebhook:output_type -> forge.v1.ParseWebhookResponse
48, // 79: forge.v1.ForgeService.GetRepoArchive:output_type -> forge.v1.GetRepoArchiveResponse
58, // [58:80] is the sub-list for method output_type
36, // [36:58] is the sub-list for method input_type
36, // [36:36] is the sub-list for extension type_name
36, // [36:36] is the sub-list for extension extendee
0, // [0:36] is the sub-list for field type_name
2, // 27: forge.v1.AcceptInvitationsRequest.repositories:type_name -> forge.v1.RepoRef
2, // 28: forge.v1.VerifyPushRequest.repo:type_name -> forge.v1.RepoRef
2, // 29: forge.v1.GetRepoArchiveRequest.repo:type_name -> forge.v1.RepoRef
55, // 30: forge.v1.ParseWebhookRequest.headers:type_name -> forge.v1.ParseWebhookRequest.HeadersEntry
51, // 31: forge.v1.ParseWebhookResponse.issue:type_name -> forge.v1.IssueEvent
52, // 32: forge.v1.ParseWebhookResponse.review:type_name -> forge.v1.ReviewEvent
53, // 33: forge.v1.ParseWebhookResponse.review_comment:type_name -> forge.v1.ReviewCommentEvent
2, // 34: forge.v1.IssueEvent.repo:type_name -> forge.v1.RepoRef
2, // 35: forge.v1.ReviewEvent.repo:type_name -> forge.v1.RepoRef
2, // 36: forge.v1.ReviewCommentEvent.repo:type_name -> forge.v1.RepoRef
0, // 37: forge.v1.ForgeService.Configure:input_type -> forge.v1.ConfigureRequest
7, // 38: forge.v1.ForgeService.AssignedIssues:input_type -> forge.v1.AssignedIssuesRequest
8, // 39: forge.v1.ForgeService.IssuesWithLabel:input_type -> forge.v1.IssuesWithLabelRequest
9, // 40: forge.v1.ForgeService.Comment:input_type -> forge.v1.CommentRequest
11, // 41: forge.v1.ForgeService.CloseIssue:input_type -> forge.v1.CloseIssueRequest
17, // 42: forge.v1.ForgeService.React:input_type -> forge.v1.ReactRequest
19, // 43: forge.v1.ForgeService.SetStateLabel:input_type -> forge.v1.SetStateLabelRequest
21, // 44: forge.v1.ForgeService.CreatePR:input_type -> forge.v1.CreatePRRequest
23, // 45: forge.v1.ForgeService.PRState:input_type -> forge.v1.PRStateRequest
13, // 46: forge.v1.ForgeService.ClosePR:input_type -> forge.v1.ClosePRRequest
15, // 47: forge.v1.ForgeService.MergePR:input_type -> forge.v1.MergePRRequest
26, // 48: forge.v1.ForgeService.GetPullRequest:input_type -> forge.v1.GetPullRequestRequest
28, // 49: forge.v1.ForgeService.GetPullRequestDiff:input_type -> forge.v1.GetPullRequestDiffRequest
30, // 50: forge.v1.ForgeService.ListReviews:input_type -> forge.v1.ListReviewsRequest
34, // 51: forge.v1.ForgeService.ListReviewComments:input_type -> forge.v1.ListReviewCommentsRequest
36, // 52: forge.v1.ForgeService.ReplyToReview:input_type -> forge.v1.ReplyToReviewRequest
39, // 53: forge.v1.ForgeService.CreateReviewComments:input_type -> forge.v1.CreateReviewCommentsRequest
43, // 54: forge.v1.ForgeService.AcceptInvitations:input_type -> forge.v1.AcceptInvitationsRequest
45, // 55: forge.v1.ForgeService.VerifyPush:input_type -> forge.v1.VerifyPushRequest
41, // 56: forge.v1.ForgeService.LinkBranch:input_type -> forge.v1.LinkBranchRequest
49, // 57: forge.v1.ForgeService.ParseWebhook:input_type -> forge.v1.ParseWebhookRequest
47, // 58: forge.v1.ForgeService.GetRepoArchive:input_type -> forge.v1.GetRepoArchiveRequest
1, // 59: forge.v1.ForgeService.Configure:output_type -> forge.v1.ConfigureResponse
5, // 60: forge.v1.ForgeService.AssignedIssues:output_type -> forge.v1.AssignedIssuesResponse
6, // 61: forge.v1.ForgeService.IssuesWithLabel:output_type -> forge.v1.IssuesWithLabelResponse
10, // 62: forge.v1.ForgeService.Comment:output_type -> forge.v1.CommentResponse
12, // 63: forge.v1.ForgeService.CloseIssue:output_type -> forge.v1.CloseIssueResponse
18, // 64: forge.v1.ForgeService.React:output_type -> forge.v1.ReactResponse
20, // 65: forge.v1.ForgeService.SetStateLabel:output_type -> forge.v1.SetStateLabelResponse
22, // 66: forge.v1.ForgeService.CreatePR:output_type -> forge.v1.CreatePRResponse
24, // 67: forge.v1.ForgeService.PRState:output_type -> forge.v1.PRStateResponse
14, // 68: forge.v1.ForgeService.ClosePR:output_type -> forge.v1.ClosePRResponse
16, // 69: forge.v1.ForgeService.MergePR:output_type -> forge.v1.MergePRResponse
27, // 70: forge.v1.ForgeService.GetPullRequest:output_type -> forge.v1.GetPullRequestResponse
29, // 71: forge.v1.ForgeService.GetPullRequestDiff:output_type -> forge.v1.GetPullRequestDiffResponse
32, // 72: forge.v1.ForgeService.ListReviews:output_type -> forge.v1.ListReviewsResponse
35, // 73: forge.v1.ForgeService.ListReviewComments:output_type -> forge.v1.ListReviewCommentsResponse
37, // 74: forge.v1.ForgeService.ReplyToReview:output_type -> forge.v1.ReplyToReviewResponse
40, // 75: forge.v1.ForgeService.CreateReviewComments:output_type -> forge.v1.CreateReviewCommentsResponse
44, // 76: forge.v1.ForgeService.AcceptInvitations:output_type -> forge.v1.AcceptInvitationsResponse
46, // 77: forge.v1.ForgeService.VerifyPush:output_type -> forge.v1.VerifyPushResponse
42, // 78: forge.v1.ForgeService.LinkBranch:output_type -> forge.v1.LinkBranchResponse
50, // 79: forge.v1.ForgeService.ParseWebhook:output_type -> forge.v1.ParseWebhookResponse
48, // 80: forge.v1.ForgeService.GetRepoArchive:output_type -> forge.v1.GetRepoArchiveResponse
59, // [59:81] is the sub-list for method output_type
37, // [37:59] is the sub-list for method input_type
37, // [37:37] is the sub-list for extension type_name
37, // [37:37] is the sub-list for extension extendee
0, // [0:37] is the sub-list for field type_name
}
func init() { file_forge_v1_forge_proto_init() }