From ac5359a870a8d89a6ec5a8ad0a76c9f0804293f6 Mon Sep 17 00:00:00 2001 From: fullsend-code <278716306+fullsend-ai-coder[bot]@users.noreply.github.com> Date: Mon, 19 Oct 2026 15:57:45 +0000 Subject: [PATCH] fix(#8173): verify review-thread roles only when authoritative The review-thread fetcher marked every successful collaborator permission lookup as verified=true. When GitHub returned a custom role with missing, contradictory, or unnormalizable permission data, baseRole() fell back to "none" and the output could not distinguish that from an authoritative non-member. Add collaboratorPermission.normalizedRole(), which returns the same role as baseRole() plus whether the response authoritatively establishes it. Built-in roles and custom roles that normalize via effective flags or the legacy permission field stay verified. "none" is verified only for a 404 or an explicit role_name "none" with no conflicting permission. A missing role_name, all-false flags, unknown custom data, and lookup failures yield RoleNone with verified=false, keeping authorization fail-closed. ListPullRequestReviewThreads now fetches the raw permission response via a new fetchCollaboratorPermission helper. GetCollaboratorPermission output is unchanged for existing callers. Document the verification semantics in cli-internals.md. Note: pre-commit could not fetch remote hook repos (TLS error in the sandbox); configured hooks were run directly instead (whitespace, EOF, private-key, gofmt, go vet, symlinks, docs links, lychee) and passed. Closes #8173 Co-Authored-By: Claude Opus 5.5 --- docs/guides/dev/cli-internals.md | 14 ++++- internal/forge/github/github.go | 59 +++++++++++++++---- internal/forge/github/github_test.go | 11 ++++ internal/forge/github/review_threads_test.go | 60 ++++++++++++++++++++ 4 files changed, 130 insertions(+), 14 deletions(-) diff --git a/docs/guides/dev/cli-internals.md b/docs/guides/dev/cli-internals.md index 641535a676..fb7ac48b3c 100644 --- a/docs/guides/dev/cli-internals.md +++ b/docs/guides/dev/cli-internals.md @@ -216,9 +216,17 @@ must treat `truncated: true` as an incomplete result. GitLab merge-request discussions are mapped to the same thread model, including resolution state, comments, positions, and resolver identity when GitLab provides it. Each comment also includes `author_role` and `author_role_verified`; resolved threads include -`resolved_by_role` and `resolved_by_role_verified`. A role is `none` with -`verified: true` for an authoritative non-member result, and `verified: false` -when the actor is a bot/unknown or the permission lookup fails. +`resolved_by_role` and `resolved_by_role_verified`. `verified: true` means the +forge authoritatively established the role, not merely that the lookup request +succeeded. On GitHub, a built-in role, or a custom role that normalizes through +effective permission flags or the legacy `permission` field, is reported with +`verified: true`. A role is `none` with `verified: true` only for an +authoritative non-member result: a 404 from the collaborator permission +endpoint, or an explicit `role_name: none` with no conflicting `permission`. A +role is `none` with `verified: false` when the actor is a bot/unknown, the +permission lookup fails, or a successful response has missing, contradictory, +or unnormalizable permission data. Consumers must treat unverified roles as +untrusted (fail closed). The `mint`, `inference`, and `github` subcommands decompose setup into role-specific operations for organizations that separate GCP and GitHub responsibilities: diff --git a/internal/forge/github/github.go b/internal/forge/github/github.go index 55ea7d9951..a02391e11f 100644 --- a/internal/forge/github/github.go +++ b/internal/forge/github/github.go @@ -3746,7 +3746,7 @@ func (c *LiveClient) ListPullRequestReviewThreads(ctx context.Context, owner, re if cached, ok := roleCache[login]; ok { return cached.role, cached.verified } - permission, err := c.GetCollaboratorPermission(ctx, owner, repo, login) + perm, err := c.fetchCollaboratorPermission(ctx, owner, repo, login) if err != nil { verified := forge.IsNotFound(err) roleCache[login] = struct { @@ -3755,12 +3755,18 @@ func (c *LiveClient) ListPullRequestReviewThreads(ctx context.Context, owner, re }{normevent.RoleNone, verified} return normevent.RoleNone, verified } - role := normevent.MapGitHubPermission(permission) + // A successful response is verified only when it authoritatively + // establishes the role; unnormalizable data stays RoleNone unverified. + permission, verified := perm.normalizedRole() + role := normevent.RoleNone + if verified { + role = normevent.MapGitHubPermission(permission) + } roleCache[login] = struct { role normevent.ActorRole verified bool - }{role, true} - return role, true + }{role, verified} + return role, verified } for page := 1; page <= 20; page++ { variables := map[string]any{ @@ -4356,20 +4362,30 @@ func (c *LiveClient) GetAppClientID(ctx context.Context, slug string) (string, e } func (c *LiveClient) GetCollaboratorPermission(ctx context.Context, owner, repo, username string) (string, error) { + perm, err := c.fetchCollaboratorPermission(ctx, owner, repo, username) + if err != nil { + return "", err + } + if perm.RoleName == "" { + return "", fmt.Errorf("%w: no permission for %s", forge.ErrNotFound, username) + } + return perm.baseRole(), nil +} + +// fetchCollaboratorPermission returns the decoded collaborator permission +// response without interpreting it. +func (c *LiveClient) fetchCollaboratorPermission(ctx context.Context, owner, repo, username string) (collaboratorPermission, error) { path := fmt.Sprintf("/repos/%s/%s/collaborators/%s/permission", url.PathEscape(owner), url.PathEscape(repo), url.PathEscape(username)) resp, err := c.get(ctx, path) if err != nil { - return "", fmt.Errorf("get collaborator permission for %s: %w", username, err) + return collaboratorPermission{}, fmt.Errorf("get collaborator permission for %s: %w", username, err) } var perm collaboratorPermission if err := decodeJSON(resp, &perm); err != nil { - return "", fmt.Errorf("decode collaborator permission for %s: %w", username, err) + return collaboratorPermission{}, fmt.Errorf("decode collaborator permission for %s: %w", username, err) } - if perm.RoleName == "" { - return "", fmt.Errorf("%w: no permission for %s", forge.ErrNotFound, username) - } - return perm.baseRole(), nil + return perm, nil } // collaboratorPermission is the subset of GitHub's collaborator permission @@ -4393,10 +4409,31 @@ type collaboratorPermission struct { // legacy permission field, else "none". Keep in sync with has_repo_permission // in reusable-dispatch.yml. func (p collaboratorPermission) baseRole() string { + role, _ := p.normalizedRole() + return role +} + +// normalizedRole returns baseRole plus whether the response authoritatively +// establishes that role. Built-in role names and custom role names that +// normalize to a real role through effective flags or the legacy permission +// are authoritative. "none" is authoritative only when GitHub explicitly +// reports role_name "none" with no conflicting legacy permission; a custom +// role whose data cannot be normalized, or a missing role_name, is not. +func (p collaboratorPermission) normalizedRole() (string, bool) { switch p.RoleName { case "admin", "maintain", "write", "triage", "read": - return p.RoleName + return p.RoleName, true + } + role := p.effectiveRole() + if role != "none" { + return role, p.RoleName != "" && p.RoleName != "none" } + return role, p.RoleName == "none" && (p.Permission == "" || p.Permission == "none") +} + +// effectiveRole resolves a non-built-in role name from GitHub's effective +// permission flags when present, otherwise from the legacy permission field. +func (p collaboratorPermission) effectiveRole() string { if f := p.User.Permissions; f != nil { switch { case f.Admin: diff --git a/internal/forge/github/github_test.go b/internal/forge/github/github_test.go index 40d7fa62b6..840012efdc 100644 --- a/internal/forge/github/github_test.go +++ b/internal/forge/github/github_test.go @@ -4960,6 +4960,17 @@ func TestGetCollaboratorPermission(t *testing.T) { require.Error(t, err) assert.True(t, forge.IsNotFound(err)) }) + + t.Run("missing role name is not found", func(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write([]byte(`{"permission":"write"}`)) + })) + defer srv.Close() + + _, err := newTestClient(t, srv).GetCollaboratorPermission(context.Background(), "o", "r", "alice") + require.Error(t, err) + assert.True(t, forge.IsNotFound(err)) + }) } func TestAddCollaborator(t *testing.T) { diff --git a/internal/forge/github/review_threads_test.go b/internal/forge/github/review_threads_test.go index 8187df95ac..570058fdb8 100644 --- a/internal/forge/github/review_threads_test.go +++ b/internal/forge/github/review_threads_test.go @@ -122,6 +122,66 @@ func TestListPullRequestReviewThreads_RoleLookupFailures(t *testing.T) { assert.False(t, got.Threads[0].Comments[1].AuthorRoleVerified, "bots do not use human permission roles") } +func TestListPullRequestReviewThreads_RoleVerification(t *testing.T) { + cases := []struct { + name string + body string + status int + wantRole normevent.ActorRole + wantVerified bool + }{ + {name: "built-in admin", body: `{"permission":"admin","role_name":"admin"}`, wantRole: normevent.RoleAdmin, wantVerified: true}, + {name: "built-in maintain", body: `{"permission":"write","role_name":"maintain"}`, wantRole: normevent.RoleMaintain, wantVerified: true}, + {name: "built-in write", body: `{"permission":"write","role_name":"write"}`, wantRole: normevent.RoleWrite, wantVerified: true}, + {name: "built-in triage", body: `{"permission":"read","role_name":"triage"}`, wantRole: normevent.RoleTriage, wantVerified: true}, + {name: "built-in read", body: `{"permission":"read","role_name":"read"}`, wantRole: normevent.RoleRead, wantVerified: true}, + {name: "custom maintain flags", body: `{"permission":"write","role_name":"Repo Maintainer","user":{"permissions":{"maintain":true,"push":true,"triage":true,"pull":true}}}`, wantRole: normevent.RoleMaintain, wantVerified: true}, + {name: "custom write flags", body: `{"permission":"write","role_name":"Dev","user":{"permissions":{"push":true,"pull":true}}}`, wantRole: normevent.RoleWrite, wantVerified: true}, + {name: "custom legacy permission", body: `{"permission":"write","role_name":"Dev","user":{"permissions":null}}`, wantRole: normevent.RoleWrite, wantVerified: true}, + {name: "explicit none", body: `{"permission":"none","role_name":"none"}`, wantRole: normevent.RoleNone, wantVerified: true}, + {name: "authoritative non-member 404", status: http.StatusNotFound, wantRole: normevent.RoleNone, wantVerified: true}, + {name: "custom role without usable data", body: `{"role_name":"Mystery"}`, wantRole: normevent.RoleNone, wantVerified: false}, + {name: "custom role with unknown legacy permission", body: `{"permission":"none","role_name":"Mystery"}`, wantRole: normevent.RoleNone, wantVerified: false}, + {name: "custom role with all flags false", body: `{"permission":"write","role_name":"Dev","user":{"permissions":{"admin":false,"maintain":false,"push":false,"triage":false,"pull":false}}}`, wantRole: normevent.RoleNone, wantVerified: false}, + {name: "none role with conflicting legacy permission", body: `{"permission":"write","role_name":"none"}`, wantRole: normevent.RoleNone, wantVerified: false}, + {name: "none role with conflicting flags", body: `{"role_name":"none","user":{"permissions":{"push":true}}}`, wantRole: normevent.RoleNone, wantVerified: false}, + {name: "missing role name", body: `{"permission":"write"}`, wantRole: normevent.RoleNone, wantVerified: false}, + {name: "malformed response", body: `{"role_name":"Dev","user":{"permissions":{"push":"true"}}}`, wantRole: normevent.RoleNone, wantVerified: false}, + {name: "transient lookup failure", status: http.StatusInternalServerError, wantRole: normevent.RoleNone, wantVerified: false}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + lookups := 0 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodGet { + require.Equal(t, "/repos/owner/repo/collaborators/alice/permission", r.URL.Path) + lookups++ + if tc.status != 0 { + w.WriteHeader(tc.status) + return + } + _, _ = w.Write([]byte(tc.body)) + return + } + _, _ = w.Write([]byte(`{"data":{"repository":{"pullRequest":{"reviewThreads":{"pageInfo":{"hasNextPage":false,"endCursor":""},"nodes":[{"id":"thread-1","isResolved":true,"resolvedBy":{"login":"alice","__typename":"User"},"comments":{"pageInfo":{"hasNextPage":false},"nodes":[{"author":{"login":"alice","__typename":"User"},"body":"comment","createdAt":"2026-10-06T10:00:00Z"}]}}]}}}}}`)) + })) + defer srv.Close() + + got, err := newTestClient(t, srv).ListPullRequestReviewThreads(context.Background(), "owner", "repo", 1) + require.NoError(t, err) + require.Len(t, got.Threads, 1) + require.Len(t, got.Threads[0].Comments, 1) + assert.Equal(t, tc.wantRole, got.Threads[0].ResolvedByRole) + assert.Equal(t, tc.wantVerified, got.Threads[0].ResolvedByRoleVerified) + assert.Equal(t, tc.wantRole, got.Threads[0].Comments[0].AuthorRole) + assert.Equal(t, tc.wantVerified, got.Threads[0].Comments[0].AuthorRoleVerified) + if tc.status != http.StatusInternalServerError { + assert.Equal(t, 1, lookups, "role lookups are cached per login") + } + }) + } +} + func TestListPullRequestReviewThreads_PaginatesAndCaps(t *testing.T) { var calls int srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {