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

+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)
}
})
}
}