diff --git a/changelog.d/security-pin-only-commit-trust.md b/changelog.d/security-pin-only-commit-trust.md new file mode 100644 index 0000000000..99aca70531 --- /dev/null +++ b/changelog.d/security-pin-only-commit-trust.md @@ -0,0 +1 @@ +- The human-merge pin-only exception now also requires every commit on the PR to be authored and committed by a trusted author, not just the PR opener (#16380) diff --git a/src/pkg/github/automerge/automerge_human_paths.go b/src/pkg/github/automerge/automerge_human_paths.go index ea1457f81c..4fad12f270 100644 --- a/src/pkg/github/automerge/automerge_human_paths.go +++ b/src/pkg/github/automerge/automerge_human_paths.go @@ -85,7 +85,7 @@ func (c *Engine) currentHumanMergePinOnly(repo string) (config.PinOnlyException, // edits any other file (even an unguarded changelog or doc) is not pin-only // and stays held (#16326). The sweep lanes and /approve share this helper so // they never disagree. -func (c *Engine) humanMergePinOnlyExempt(owner, repo string, pr *gh.PullRequest, files []intent.ChangedFile, matches []string) (bool, string) { +func (c *Engine) humanMergePinOnlyExempt(ctx context.Context, owner, repo string, pr *gh.PullRequest, files []intent.ChangedFile, matches []string) (bool, string) { if len(matches) == 0 || pr == nil { return false, "" } @@ -109,9 +109,57 @@ func (c *Engine) humanMergePinOnlyExempt(owner, repo string, pr *gh.PullRequest, if reported := pr.GetChangedFiles(); reported <= 0 || reported != len(files) { return false, "complete changed-file list not proven" } + if why := c.pinOnlyCommitsUntrusted(ctx, owner, repo, pr, policy.TrustedAuthors); why != "" { + return false, why + } return intent.ClassifyPinOnly(files, matches, policy.Spec()) } +// pinOnlyCommitsUntrusted proves the pushed commits, not just the PR opener, +// are the trusted author's: anyone with push access to the head branch could +// add a commit under a trusted bot's PR. Every commit's author and committer +// login must be trusted and the list must be complete; it returns a reason +// when that is not proven (including on a listing error, failing closed). +func (c *Engine) pinOnlyCommitsUntrusted(ctx context.Context, owner, repo string, pr *gh.PullRequest, trustedAuthors []string) string { + trusted := func(u *gh.User) bool { + login := strings.TrimSpace(hgithub.SafeGetLogin(u)) + if login == "" { + return false + } + for _, t := range trustedAuthors { + if strings.EqualFold(strings.TrimSpace(t), login) { + return true + } + } + return false + } + opts := &gh.ListOptions{PerPage: 100} + total := 0 + for page := 0; ; page++ { + if page >= stageDCOMaxPages { + return "complete commit list not proven" + } + commits, resp, err := c.gh.PullRequests.ListCommits(ctx, owner, repo, pr.GetNumber(), opts) + if err != nil { + return "commit list unavailable" + } + for _, rc := range commits { + if rc == nil || !trusted(rc.Author) || !trusted(rc.Committer) { + return "PR has a commit not authored and committed by a trusted author" + } + } + total += len(commits) + if resp == nil || resp.NextPage == 0 { + break + } + opts.Page = resp.NextPage + } + if total == 0 || (pr.GetCommits() > 0 && pr.GetCommits() != total) { + return "complete commit list not proven" + } + return "" +} + // humanMergePathGate refuses a PR that touches a path a person must merge. // It is independent of intent tier and of IntentGate/intent.enforce: the App // self-merge contract (EvaluateForAppSelfMerge) authorizes Tier 2/3 without a @@ -140,7 +188,7 @@ func (c *Engine) humanMergePathGate(ctx context.Context, displayRepo, owner, rep if len(matches) == 0 { return "", nil } - exempt, why := c.humanMergePinOnlyExempt(owner, repo, pr, files, matches) + exempt, why := c.humanMergePinOnlyExempt(ctx, owner, repo, pr, files, matches) if exempt { c.info("automerge sweep admitted pin-only change on human-merge paths", "repo", displayRepo, "pr", number, "paths", matches, "config", humanMergePinOnlyConfigKey, "reason", why) return "", nil diff --git a/src/pkg/github/automerge/automerge_human_paths_test.go b/src/pkg/github/automerge/automerge_human_paths_test.go index e53580c970..284cb7ac67 100644 --- a/src/pkg/github/automerge/automerge_human_paths_test.go +++ b/src/pkg/github/automerge/automerge_human_paths_test.go @@ -31,6 +31,7 @@ type humanPathFront struct { fileEntries []map[string]any // when set, served first, before the entries built from files filesStatus int filesHits int + commits []map[string]any // served as the PR commit list labels []string comments []string } @@ -57,6 +58,8 @@ func newHumanPathFront(t *testing.T, backend string, f *humanPathFront) *httptes files = append(files, map[string]any{"filename": name, "status": "modified", "additions": 1}) } json.NewEncoder(w).Encode(files) + case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/pulls/7/commits": + json.NewEncoder(w).Encode(f.commits) case r.Method == http.MethodPost && r.URL.Path == "/repos/acme/widget/issues/7/labels": var labels []string _ = json.NewDecoder(r.Body).Decode(&labels) @@ -365,7 +368,8 @@ func TestHumanMergePathGatePinOnlyException(t *testing.T) { paths func(string) []string pinOnly func(string) (config.PinOnlyException, bool) author string - changed int // 0 means len(entries) + changed int // 0 means len(entries) + pushers []string // commit author/committer logins; nil means one commit by author entries []map[string]any wantHold bool wantHits int @@ -374,6 +378,7 @@ func TestHumanMergePathGatePinOnlyException(t *testing.T) { {name: "exception unconfigured: held as before", paths: humanPathsFor("acme/widget", testHumanMergePattern), author: "renovate-bot", entries: []map[string]any{testPinFile(workflow)}, wantHold: true, wantHits: 1}, {name: "exception for another repo: held", paths: humanPathsFor("acme/widget", testHumanMergePattern), pinOnly: pinOnlyFor("acme/other", testPinOnlyException()), author: "renovate-bot", entries: []map[string]any{testPinFile(workflow)}, wantHold: true, wantHits: 1}, {name: "untrusted author: held", paths: humanPathsFor("acme/widget", testHumanMergePattern), pinOnly: pinOnlyFor("acme/widget", testPinOnlyException()), author: "mallory", entries: []map[string]any{testPinFile(workflow)}, wantHold: true, wantHits: 1}, + {name: "trusted PR with a commit pushed by another account: held", paths: humanPathsFor("acme/widget", testHumanMergePattern), pinOnly: pinOnlyFor("acme/widget", testPinOnlyException()), author: "renovate-bot", pushers: []string{"renovate-bot", "mallory"}, entries: []map[string]any{testPinFile(workflow)}, wantHold: true, wantHits: 1}, {name: "trusted pure pin: not held", paths: humanPathsFor("acme/widget", testHumanMergePattern), pinOnly: pinOnlyFor("acme/widget", testPinOnlyException()), author: "renovate-bot", entries: []map[string]any{testPinFile(workflow)}, wantHits: 1}, {name: "trusted pin plus non-pin line: held", paths: humanPathsFor("acme/widget", testHumanMergePattern), pinOnly: pinOnlyFor("acme/widget", testPinOnlyException()), author: "renovate-bot", entries: []map[string]any{{ "filename": workflow, "status": "modified", "additions": 2, "deletions": 1, @@ -396,6 +401,13 @@ func TestHumanMergePathGatePinOnlyException(t *testing.T) { })) defer backend.Close() front := &humanPathFront{fileEntries: tc.entries} + pushers := tc.pushers + if pushers == nil { + pushers = []string{tc.author} + } + for _, login := range pushers { + front.commits = append(front.commits, map[string]any{"sha": "c-" + login, "author": map[string]any{"login": login}, "committer": map[string]any{"login": login}}) + } srv := newHumanPathFront(t, backend.URL, front) defer srv.Close() diff --git a/src/pkg/github/automerge/pr_approve_command.go b/src/pkg/github/automerge/pr_approve_command.go index a54bae883a..db64740994 100644 --- a/src/pkg/github/automerge/pr_approve_command.go +++ b/src/pkg/github/automerge/pr_approve_command.go @@ -90,7 +90,7 @@ func (c *Engine) PRApproveReply(ctx context.Context, displayRepo string, number // The same pin-only exception the sweep lanes apply (#16266), so the // reply never contradicts what the lane will do on its next pass. matches := intent.HumanMergePathMatches(files, patterns) - if exempt, _ := c.humanMergePinOnlyExempt(owner, repo, pr, files, matches); exempt { + if exempt, _ := c.humanMergePinOnlyExempt(ctx, owner, repo, pr, files, matches); exempt { matches = nil } if len(matches) == 0 && diffReason == contributorLaneReasonWorkflowPath && c.currentContributorLanePolicy().OwnerApprovalReleasesWorkflowFence { diff --git a/src/pkg/github/automerge/pr_approve_command_test.go b/src/pkg/github/automerge/pr_approve_command_test.go index b28c96ffe1..8be1af83b2 100644 --- a/src/pkg/github/automerge/pr_approve_command_test.go +++ b/src/pkg/github/automerge/pr_approve_command_test.go @@ -121,6 +121,9 @@ func TestPRApproveReplyPinOnlyMatchesSweep(t *testing.T) { switch r.URL.Path { case "/repos/acme/widget/pulls/7": json.NewEncoder(w).Encode(&gh.PullRequest{Number: gh.Ptr(7), State: gh.Ptr("open"), ChangedFiles: gh.Ptr(1), User: &gh.User{Login: gh.Ptr(testHiveAppBotLogin)}, Head: &gh.PullRequestBranch{SHA: gh.Ptr("sha7")}}) + case "/repos/acme/widget/pulls/7/commits": + bot := &gh.User{Login: gh.Ptr(testHiveAppBotLogin)} + json.NewEncoder(w).Encode([]*gh.RepositoryCommit{{SHA: gh.Ptr("c1"), Author: bot, Committer: bot}}) case "/repos/acme/widget/pulls/7/files": json.NewEncoder(w).Encode([]*gh.CommitFile{{Filename: gh.Ptr("sensitive/build.yml"), Status: gh.Ptr("modified"), Additions: gh.Ptr(1), Deletions: gh.Ptr(1), Patch: gh.Ptr(tc.patch)}}) default: