Skip to content
Closed
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-pin-only-commit-trust.md
Original file line number Diff line number Diff line change
@@ -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)
52 changes: 50 additions & 2 deletions src/pkg/github/automerge/automerge_human_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ""
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion src/pkg/github/automerge/automerge_human_paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion src/pkg/github/automerge/pr_approve_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions src/pkg/github/automerge/pr_approve_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading