Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/security-base-refresh-verification.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- A recorded decision, and the base-preparation proof, now follow a PR onto a new head only when the merge commit is one GitHub created and verified (signed, committed by `web-flow` or the App) rather than any commit whose author email resolves to the App bot, and a base...head compare that reaches GitHub's 300-file listing cap fails closed instead of being compared on a truncated file list ([#16860](https://github.com/hivecommons/hive/issues/16860)).
2 changes: 1 addition & 1 deletion src/cmd/hive/hold_guard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ func TestEnforceHoldGuardOperatorApprovalCarriesOverHiveBaseRefresh(t *testing.T
fake.pulls = map[int]map[string]any{7: {"number": 7, "head": map[string]any{"sha": "m1"}, "base": map[string]any{"ref": "main"}}}
fake.commitBySHA = map[string]map[string]any{"m1": {
"sha": "m1", "author": map[string]any{"login": tc.mergeAuthor}, "committer": map[string]any{"login": "web-flow"},
"parents": []map[string]any{{"sha": "c1"}, {"sha": "b1"}},
"commit": map[string]any{"verification": map[string]any{"verified": true}}, "parents": []map[string]any{{"sha": "c1"}, {"sha": "b1"}},
}}
fake.compareResult = map[string]map[string]any{
"main...c1": {"status": "ahead", "merge_base_commit": map[string]any{"sha": "b0"}, "files": files("blob1")},
Expand Down
12 changes: 8 additions & 4 deletions src/docs/hive-merge.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,14 @@ hand-applied return label) names the exact head the person saw; a new head
is a new tree nobody has decided on, so the record is superseded and the
reviewer's `decision_requested` question stands again. The one head change
that does not supersede it is the hive's own `require_current_base` refresh:
a merge commit authored by the App whose parents are the decided head and a
commit on the base branch, and whose three-dot diff against the base is
file-for-file (name, status, blob, line counts, patch) identical to the
decided head's. Up to five such refreshes may stack before any gate reads
a merge commit authored by the App, committed by GitHub (`web-flow`) or the
App and carrying a signature GitHub verified, whose parents are the decided
head and a commit on the base branch, and whose three-dot diff against the
base is file-for-file (name, status, blob, line counts, patch) identical to
the decided head's. An App login GitHub derived from an unsigned commit's
author email does not count, and a compare that lists GitHub's 300-file cap
cannot be verified, so neither carries
([#16860](https://github.com/hivecommons/hive/issues/16860)). Up to five such refreshes may stack before any gate reads
the PR: the carry walks the first-parent chain back from the new head and
every hop must itself pass the same check, so two App merge-from-base
commits in a row still carry while a content commit anywhere in the chain
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/github/automerge/automerge_decision_carry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (a *baseRefreshAPI) handler(t *testing.T) http.Handler {
return
}
_ = json.NewEncoder(w).Encode(map[string]any{"sha": sha, "author": map[string]any{"login": a.mergeAuthor}, "committer": map[string]any{"login": "web-flow"},
"parents": []map[string]any{{"sha": a.oldHead}, {"sha": a.baseTip}}})
"commit": map[string]any{"verification": map[string]any{"verified": true}}, "parents": []map[string]any{{"sha": a.oldHead}, {"sha": a.baseTip}}})
case r.Method == http.MethodGet && strings.HasSuffix(path, "/reviews"):
_ = json.NewEncoder(w).Encode([]map[string]any{})
case r.Method == http.MethodGet && strings.HasSuffix(path, "/pulls/15649"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ func TestActiveBasePreparationCurrentHeadLifecycleAndReadOnlyRestart(t *testing.
author = "external-author"
}
_ = json.NewEncoder(w).Encode(map[string]any{"sha": "refreshed", "parents": parents,
"author": map[string]string{"login": author}})
"author": map[string]string{"login": author}, "committer": map[string]string{"login": "web-flow"},
"commit": map[string]any{"verification": map[string]any{"verified": true}}})
case strings.Contains(r.URL.Path, "/compare/"):
if !strings.Contains(r.URL.Path, "/compare/main...") {
t.Errorf("refresh proof used stale recorded base instead of destination: %s", r.URL.Path)
Expand Down
48 changes: 43 additions & 5 deletions src/pkg/github/pr_decision_base_refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package github

import (
"context"
"errors"
"fmt"
"log/slog"
"sort"
"strconv"
Expand Down Expand Up @@ -29,6 +31,18 @@ const baseRefreshMergeParents = 2
// carry walks back through to reach the decided head (hivecommons/hive#15766).
const baseRefreshMaxHops = 5

// baseRefreshCompareFileCap is the most files GitHub lists for a compare,
// however it is paginated; a compare that reaches it may be truncated.
const baseRefreshCompareFileCap = 300

// baseRefreshWebFlowLogin is the committer login GitHub puts on commits it
// creates and signs itself, such as the update-branch merge.
const baseRefreshWebFlowLogin = "web-flow"

// ErrCompareFileCap reports a compare whose file list reached GitHub's
// 300-file cap, so the diff cannot be verified file for file.
var ErrCompareFileCap = errors.New("compare file list reached GitHub's 300-file cap")

// BaseRefreshRepos is the slice of the repositories API the base-refresh
// check reads: one commit lookup and two three-dot compares per new head.
type BaseRefreshRepos interface {
Expand All @@ -46,9 +60,11 @@ type BaseRefreshChecker struct {
}

// HeadIsBaseRefreshOf reports whether newHead on owner/repo#number is a
// merge commit authored by the App (appBotLogin) whose parents are oldHead
// GitHub-verified merge commit created by the App (appBotLogin, see
// CommitIsVerifiedAppMerge) whose parents are oldHead
// and a commit on baseRef, and whose three-dot diff against baseRef is
// file-for-file identical to oldHead's. That is exactly the head the
// file-for-file identical to oldHead's (a compare at GitHub's 300-file cap
// cannot show that and is false). That is exactly the head the
// require_current_base gate produces when it merges the base into the
// branch: the PR's content is unchanged, only its base moved. Any other
// head change — a content commit (even one that also merged the base), a
Expand Down Expand Up @@ -127,7 +143,7 @@ func mergeIsBaseRefreshOf(ctx context.Context, api BaseRefreshRepos, owner, repo
if commit == nil || len(commit.Parents) != baseRefreshMergeParents {
return false
}
if !CommitByGitHubLogin(commit, appBotLogin) {
if !CommitIsVerifiedAppMerge(commit, appBotLogin) {
return false
}
first, second := strings.TrimSpace(commit.Parents[0].GetSHA()), strings.TrimSpace(commit.Parents[1].GetSHA())
Expand Down Expand Up @@ -159,6 +175,8 @@ func mergeIsBaseRefreshOf(ctx context.Context, api BaseRefreshRepos, owner, repo
}

// compareAllFiles walks every page of the three-dot compare base...head.
// A file list at GitHub's cap is ErrCompareFileCap: past it GitHub drops
// files silently, so the two diffs could differ in what it left out.
func compareAllFiles(ctx context.Context, api BaseRefreshRepos, owner, repo, base, head string) (*gh.CommitsComparison, []*gh.CommitFile, error) {
opts := &gh.ListOptions{PerPage: 100}
var first *gh.CommitsComparison
Expand All @@ -174,6 +192,9 @@ func compareAllFiles(ctx context.Context, api BaseRefreshRepos, owner, repo, bas
if cmp != nil {
files = append(files, cmp.Files...)
}
if len(files) >= baseRefreshCompareFileCap {
return nil, nil, fmt.Errorf("compare %s...%s: %w", base, head, ErrCompareFileCap)
}
if resp == nil || resp.NextPage == 0 {
return first, files, nil
}
Expand Down Expand Up @@ -218,8 +239,10 @@ func sameChangedFiles(a, b []*gh.CommitFile) bool {
}

// CommitByGitHubLogin reports whether the commit's GitHub-attributed author
// or committer is login ([bot] suffix tolerated, case-insensitive). Only the
// server-resolved logins count, never the forgeable git name or email.
// or committer is login ([bot] suffix tolerated, case-insensitive). GitHub
// resolves those logins from the git author and committer emails, so on its
// own this is attribution, not proof: anyone can author a commit as the
// App's noreply address. Use CommitIsVerifiedAppMerge to trust a commit.
func CommitByGitHubLogin(commit *gh.RepositoryCommit, login string) bool {
want := normalizeGitHubLogin(login)
if commit == nil || want == "" {
Expand All @@ -228,6 +251,21 @@ func CommitByGitHubLogin(commit *gh.RepositoryCommit, login string) bool {
return normalizeGitHubLogin(commit.GetAuthor().GetLogin()) == want || normalizeGitHubLogin(commit.GetCommitter().GetLogin()) == want
}

// CommitIsVerifiedAppMerge reports whether GitHub itself created the commit
// for login, as it does for the App's update-branch merge: the signature is
// one GitHub verified, the committer is web-flow (GitHub-created and
// GitHub-signed, so GitHub set the author) or login itself, and the commit
// is attributed to login. A signature binds the committer only, so a
// verified commit committed by anyone else proves nothing about an author
// login derived from a forgeable email (hivecommons/hive#16860).
func CommitIsVerifiedAppMerge(commit *gh.RepositoryCommit, login string) bool {
if commit == nil || !commit.GetCommit().GetVerification().GetVerified() || !CommitByGitHubLogin(commit, login) {
return false
}
committer := normalizeGitHubLogin(commit.GetCommitter().GetLogin())
return committer == baseRefreshWebFlowLogin || committer == normalizeGitHubLogin(login)
}

func normalizeGitHubLogin(login string) string {
return strings.TrimSuffix(strings.ToLower(strings.TrimSpace(login)), "[bot]")
}
Expand Down
57 changes: 56 additions & 1 deletion src/pkg/github/pr_decision_base_refresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func (f *fakeBaseRefreshRepos) CompareCommits(_ context.Context, _, _, base, hea
}

func refreshCommit(sha, login string, parents ...string) *gh.RepositoryCommit {
c := &gh.RepositoryCommit{SHA: gh.Ptr(sha), Author: &gh.User{Login: gh.Ptr(login)}, Committer: &gh.User{Login: gh.Ptr("web-flow")}}
c := &gh.RepositoryCommit{SHA: gh.Ptr(sha), Author: &gh.User{Login: gh.Ptr(login)}, Committer: &gh.User{Login: gh.Ptr("web-flow")},
Commit: &gh.Commit{Verification: &gh.SignatureVerification{Verified: gh.Ptr(true)}}}
for _, p := range parents {
c.Parents = append(c.Parents, &gh.Commit{SHA: gh.Ptr(p)})
}
Expand Down Expand Up @@ -325,3 +326,57 @@ func TestSameChangedFilesIgnoresOrderButNotContent(t *testing.T) {
t.Fatal("App attribution must match the login case-insensitively, [bot] suffix aside, and nobody else")
}
}

// hivecommons/hive#16860: GitHub derives a commit's author login from the
// author email, so a merge authored as the App's noreply address resolves to
// the App. Only a commit GitHub created and signed itself counts.
func TestHeadIsBaseRefreshOfRequiresVerifiedGitHubCommit(t *testing.T) {
const oldHead, newHead, baseTip, app = "aaaa", "bbbb", "b45e", "kubestellar-hive[bot]"
same := []*gh.CommitFile{refreshFile("a.go", "blob1", "@@")}
for _, tc := range []struct {
name string
mutate func(*gh.RepositoryCommit)
want bool
}{
{name: "verified update-branch merge", mutate: func(*gh.RepositoryCommit) {}, want: true},
{name: "unverified", mutate: func(c *gh.RepositoryCommit) { c.Commit.Verification.Verified = gh.Ptr(false) }},
{name: "no verification", mutate: func(c *gh.RepositoryCommit) { c.Commit = nil }},
{name: "verified but committed by someone else", mutate: func(c *gh.RepositoryCommit) { c.Committer = &gh.User{Login: gh.Ptr("mallory")} }},
{name: "verified, committed by the App", mutate: func(c *gh.RepositoryCommit) { c.Committer = &gh.User{Login: gh.Ptr(app)} }, want: true},
} {
t.Run(tc.name, func(t *testing.T) {
commit := refreshCommit(newHead, app, oldHead, baseTip)
tc.mutate(commit)
api := &fakeBaseRefreshRepos{
commits: map[string]*gh.RepositoryCommit{newHead: commit},
compares: map[string]*gh.CommitsComparison{"v5..." + oldHead: refreshCompare("0ld", same...), "v5..." + newHead: refreshCompare(baseTip, same...)},
}
if got := (&BaseRefreshChecker{}).HeadIsBaseRefreshOf(context.Background(), api, "hivecommons", "hive", 1, "v5", oldHead, newHead, app); got != tc.want {
t.Fatalf("HeadIsBaseRefreshOf = %v, want %v", got, tc.want)
}
})
}
}

// hivecommons/hive#16860: GitHub lists at most 300 files for a compare, so
// two diffs at the cap may differ in files it left out; that fails closed.
func TestHeadIsBaseRefreshOfFailsClosedAtCompareFileCap(t *testing.T) {
const oldHead, newHead, baseTip, app = "aaaa", "bbbb", "b45e", "kubestellar-hive[bot]"
for _, n := range []int{baseRefreshCompareFileCap - 1, baseRefreshCompareFileCap} {
files := make([]*gh.CommitFile, 0, n)
for i := 0; i < n; i++ {
files = append(files, refreshFile("f"+strconv.Itoa(i)+".go", "blob"+strconv.Itoa(i), "@@"))
}
api := &fakeBaseRefreshRepos{
commits: map[string]*gh.RepositoryCommit{newHead: refreshCommit(newHead, app, oldHead, baseTip)},
compares: map[string]*gh.CommitsComparison{"v5..." + oldHead: refreshCompare("0ld", files...), "v5..." + newHead: refreshCompare(baseTip, files...)},
}
want := n < baseRefreshCompareFileCap
if got := (&BaseRefreshChecker{}).HeadIsBaseRefreshOf(context.Background(), api, "hivecommons", "hive", 1, "v5", oldHead, newHead, app); got != want {
t.Fatalf("%d files: HeadIsBaseRefreshOf = %v, want %v", n, got, want)
}
if _, _, err := compareAllFiles(context.Background(), api, "hivecommons", "hive", "v5", newHead); want != (err == nil) || (!want && !errors.Is(err, ErrCompareFileCap)) {
t.Fatalf("%d files: compareAllFiles err = %v", n, err)
}
}
}
Loading