diff --git a/.github/workflows/bazel-autofix.yml b/.github/workflows/bazel-autofix.yml index 5bfce89e1..acd5b7ff3 100644 --- a/.github/workflows/bazel-autofix.yml +++ b/.github/workflows/bazel-autofix.yml @@ -40,15 +40,20 @@ # the disk) and re-checked once staged (scripts/bazel-autofix-push.sh), so a # hostile patch can at most rewrite BUILD.bazel / MODULE.bazel / # MODULE.bazel.lock on its own PR branch. Pushes are leased to the run's head -# SHA and skipped for heads that are protected or under a ruleset. +# SHA and skipped for heads that are protected or under a ruleset. The App +# private key is a secret of the `autofix` environment, whose deployment- +# branch policy allows only `main`, so only a job running on main can read +# it; the minted token itself is scoped to this one repo with contents:write +# only, so even a hostile patch gains nothing beyond what the job already +# had. # # TOKEN NOTE: pushes made with the default GITHUB_TOKEN do not retrigger PR -# checks. This reuses the DOCS_AUTOFIX_TOKEN repo secret (fine-grained PAT or -# GitHub App token with contents:write on this repo) rather than adding a -# second one; without it the pushed fix still lands but checks need a manual -# re-run. Fork PRs always get the apply-recipe comment - no token we hold can -# push to a fork. workflow_run always runs this file from the default branch, -# so edits take effect only after they merge. +# checks. On same-repo PRs the push step mints a short-lived installation +# token for the gastownhall-autofix GitHub App (contents:write, scoped to +# this repo only) and uses that instead. Fork PRs always get the apply-recipe +# comment - no token we hold can push to a fork, so the mint step is skipped +# for them. workflow_run always runs this file from the default branch, so +# edits take effect only after they merge. name: Bazel Autofix @@ -70,6 +75,10 @@ jobs: github.event.workflow_run.conclusion == 'failure' runs-on: ubuntu-latest timeout-minutes: 10 + # Grants the job read access to this environment's secrets; its + # deployment-branch policy restricts that to jobs running on main + # (workflow_run always does), not to any PR branch. + environment: autofix # Shared with docs-autofix.yml; queue, never cancel (see header). concurrency: group: autofix-${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }} @@ -114,6 +123,22 @@ jobs: fi echo "found=true" >> "$GITHUB_OUTPUT" + # Same-repo PRs only: no token we hold can push to a fork, and a fork's + # head_repository can even be absent (deleted fork), so this mints + # nothing for it and the push script falls back to the apply-recipe + # comment. app-token outputs are empty (not failed) when the step is + # skipped, which is what the fallback below relies on. + - name: Mint autofix app token + id: app-token + if: github.event.workflow_run.head_repository.full_name == github.repository + uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3 + with: + app-id: ${{ secrets.AUTOFIX_APP_ID }} + private-key: ${{ secrets.AUTOFIX_APP_PRIVATE_KEY }} + owner: ${{ github.repository_owner }} + repositories: ${{ github.event.repository.name }} + permission-contents: write + - name: Push sync commit or leave apply recipe if: steps.patch.outputs.found == 'true' env: @@ -127,9 +152,9 @@ jobs: RUN_URL: ${{ github.event.workflow_run.html_url }} # Split tokens: gh api calls (PR lookup, comments) always use the # workflow token, which carries this job's pull-requests:write; the - # optional PAT is used ONLY for the git push, so it needs no more + # App token is used ONLY for the git push, so it needs no more # than contents:write. GH_TOKEN: ${{ github.token }} - PUSH_TOKEN: ${{ secrets.DOCS_AUTOFIX_TOKEN || github.token }} - AUTOFIX_TOKEN_KIND: ${{ secrets.DOCS_AUTOFIX_TOKEN && 'pat' || 'default' }} + PUSH_TOKEN: ${{ steps.app-token.outputs.token || github.token }} + AUTOFIX_TOKEN_KIND: ${{ steps.app-token.outputs.token && 'app' || 'default' }} run: ./scripts/bazel-autofix-push.sh diff --git a/.github/workflows/docs-autofix.yml b/.github/workflows/docs-autofix.yml index 36d992c97..f8dcbfd33 100644 --- a/.github/workflows/docs-autofix.yml +++ b/.github/workflows/docs-autofix.yml @@ -15,18 +15,23 @@ # patch is applied to an index in a bare clone and re-checked once staged # (scripts/docs-autofix-push.sh), so a hostile patch can at most rewrite # generated doc files on its own PR branch. Pushes are leased to the run's -# head SHA and skipped for heads that are protected or under a ruleset. +# head SHA and skipped for heads that are protected or under a ruleset. The +# App private key is a secret of the `autofix` environment, whose deployment- +# branch policy allows only `main`, so only a job running on main can read +# it; the minted token itself is scoped to this one repo with contents:write +# only, so even a hostile patch gains nothing beyond what the job already +# had. # # CONCURRENCY: job-level, shared with bazel-autofix.yml per head branch and # without cancel-in-progress, so the two bots queue instead of racing on one # branch (see bazel-autofix.yml). # # TOKEN NOTE: pushes made with the default GITHUB_TOKEN do not retrigger PR -# checks. Configure a DOCS_AUTOFIX_TOKEN repo secret (fine-grained PAT or -# GitHub App token with contents:write on this repo) for fully hands-off -# operation on same-repo PRs; without it the pushed fix still lands but -# checks need a manual re-run. Fork PRs always get the apply-recipe comment - -# no token we hold can push to a fork. +# checks. On same-repo PRs the push step mints a short-lived installation +# token for the gastownhall-autofix GitHub App (contents:write, scoped to +# this repo only) and uses that instead. Fork PRs always get the apply-recipe +# comment - no token we hold can push to a fork, so the mint step is skipped +# for them. name: Docs Autofix @@ -49,6 +54,10 @@ jobs: runs-on: ubuntu-latest # Bounded: a hung run would hold the shared group for the default 6h. timeout-minutes: 10 + # Grants the job read access to this environment's secrets; its + # deployment-branch policy restricts that to jobs running on main + # (workflow_run always does), not to any PR branch. + environment: autofix # Shared with bazel-autofix.yml; queue, never cancel. concurrency: group: autofix-${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }} @@ -92,6 +101,22 @@ jobs: fi echo "found=true" >> "$GITHUB_OUTPUT" + # Same-repo PRs only: no token we hold can push to a fork, and a fork's + # head_repository can even be absent (deleted fork), so this mints + # nothing for it and the push script falls back to the apply-recipe + # comment. app-token outputs are empty (not failed) when the step is + # skipped, which is what the fallback below relies on. + - name: Mint autofix app token + id: app-token + if: github.event.workflow_run.head_repository.full_name == github.repository + uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3 + with: + app-id: ${{ secrets.AUTOFIX_APP_ID }} + private-key: ${{ secrets.AUTOFIX_APP_PRIVATE_KEY }} + owner: ${{ github.repository_owner }} + repositories: ${{ github.event.repository.name }} + permission-contents: write + - name: Push regen commit or leave apply recipe if: steps.patch.outputs.found == 'true' env: @@ -104,9 +129,9 @@ jobs: RUN_URL: ${{ github.event.workflow_run.html_url }} # Split tokens: gh api calls (PR lookup, comments) always use the # workflow token, which carries this job's pull-requests:write; the - # optional PAT is used ONLY for the git push, so it needs no more + # App token is used ONLY for the git push, so it needs no more # than contents:write. GH_TOKEN: ${{ github.token }} - PUSH_TOKEN: ${{ secrets.DOCS_AUTOFIX_TOKEN || github.token }} - AUTOFIX_TOKEN_KIND: ${{ secrets.DOCS_AUTOFIX_TOKEN && 'pat' || 'default' }} + PUSH_TOKEN: ${{ steps.app-token.outputs.token || github.token }} + AUTOFIX_TOKEN_KIND: ${{ steps.app-token.outputs.token && 'app' || 'default' }} run: ./scripts/docs-autofix-push.sh diff --git a/scripts/bazel-autofix-push.sh b/scripts/bazel-autofix-push.sh index 2af204d89..90db02264 100755 --- a/scripts/bazel-autofix-push.sh +++ b/scripts/bazel-autofix-push.sh @@ -36,12 +36,13 @@ # RUN_ID workflow run id that produced the patch (for comment text) # RUN_URL html url of that run (for commit/comment provenance) # GH_TOKEN token for gh api calls (PR lookup, comments) - needs the -# workflow's pull-requests:write; never the PAT +# workflow's pull-requests:write; never the App token # PUSH_TOKEN token for git fetch/push only (optional; defaults to -# GH_TOKEN), so the shared DOCS_AUTOFIX_TOKEN needs -# contents:write only -# AUTOFIX_TOKEN_KIND "pat" when a dedicated push token is in use, "default" -# for the workflow's GITHUB_TOKEN (retrigger caveat) +# GH_TOKEN), so the minted gastownhall-autofix App token +# needs contents:write only +# AUTOFIX_TOKEN_KIND "app" when the minted App push token is in use, +# "default" for the workflow's GITHUB_TOKEN (retrigger +# caveat) # # Exit 0 on every non-actionable outcome (PR closed, head moved, no patch); # exit 1 on a refused patch or a genuine error so the workflow surfaces them. @@ -486,7 +487,7 @@ fi if [ "$AUTOFIX_TOKEN_KIND" = "default" ]; then cat >> "$BODY" <<'EOF' -Note: this commit was pushed with the default workflow token, which does **not** retrigger PR checks - re-run them (or push any commit) to refresh the gate. Configuring the `DOCS_AUTOFIX_TOKEN` repo secret (shared with the docs autofix) removes this step. +Note: this commit was pushed with the default workflow token, which does **not** retrigger PR checks - re-run them (or push any commit) to refresh the gate. This only happens if the gastownhall-autofix App token could not be minted for this push. EOF fi post_or_update_comment "$BODY" diff --git a/scripts/bazel_autofix_push_test.go b/scripts/bazel_autofix_push_test.go index 646c34ee6..ced5b04b9 100644 --- a/scripts/bazel_autofix_push_test.go +++ b/scripts/bazel_autofix_push_test.go @@ -528,7 +528,7 @@ func TestBazelAutofixPushFlow(t *testing.T) { t.Errorf("subject = %q", subject) } if !strings.Contains(res.log, "") || !strings.Contains(res.log, "Pushed `") || - !strings.Contains(res.log, "DOCS_AUTOFIX_TOKEN") { + !strings.Contains(res.log, "gastownhall-autofix App token") { t.Errorf("want a pushed-commit comment with the token note:\n%s", res.log) } if strings.Contains(res.log, "push-token") { diff --git a/scripts/ci_workflow_test.go b/scripts/ci_workflow_test.go index 5565102fc..b1b9c6d76 100644 --- a/scripts/ci_workflow_test.go +++ b/scripts/ci_workflow_test.go @@ -2221,20 +2221,21 @@ type ciWorkflow struct { } type ciWorkflowJob struct { - Name string `yaml:"name"` - Uses string `yaml:"uses"` - With map[string]string `yaml:"with"` - Secrets any `yaml:"secrets"` - Permissions any `yaml:"permissions"` - Needs ciWorkflowStringList `yaml:"needs"` - Steps []ciWorkflowStep `yaml:"steps"` - RunsOn string `yaml:"runs-on"` - If string `yaml:"if"` - ContinueOnError bool `yaml:"continue-on-error"` - TimeoutMinutes int `yaml:"timeout-minutes"` - Strategy ciWorkflowStrategy `yaml:"strategy"` - Env map[string]string `yaml:"env"` - Outputs map[string]string `yaml:"outputs"` + Name string `yaml:"name"` + Uses string `yaml:"uses"` + With map[string]string `yaml:"with"` + Secrets any `yaml:"secrets"` + Permissions any `yaml:"permissions"` + Needs ciWorkflowStringList `yaml:"needs"` + Steps []ciWorkflowStep `yaml:"steps"` + RunsOn string `yaml:"runs-on"` + If string `yaml:"if"` + ContinueOnError bool `yaml:"continue-on-error"` + TimeoutMinutes int `yaml:"timeout-minutes"` + Strategy ciWorkflowStrategy `yaml:"strategy"` + Env map[string]string `yaml:"env"` + Outputs map[string]string `yaml:"outputs"` + Environment ciWorkflowEnvironment `yaml:"environment"` } type ciWorkflowStrategy struct { @@ -2538,6 +2539,9 @@ const ( uploadArtifactSHA = "043fb46d1a93c77aae656e7c1c64a875d1fc6a0a" downloadArtifactSHA = "3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c" checkoutSHA = "3d3c42e5aac5ba805825da76410c181273ba90b1" + // Same SHA/comment already used in this repo for this action (rbe- + // prewarm's mint step, update-flake-lock.yml). + appTokenActionSHA = "bcd2ba49218906704ab6c1aa796996da409d3eb1" bazelCacheKeyPrefix = "bazel-repo-v3-${{ runner.os }}-" bazelCacheKey = bazelCacheKeyPrefix + "${{ hashFiles('.bazelversion', 'MODULE.bazel.lock') }}" bazelCachePath = "${{ runner.temp }}/bazel-ci-cache" @@ -6222,18 +6226,46 @@ func TestBazelAutofixWorkflowSecurity(t *testing.T) { if job.TimeoutMinutes == 0 { t.Error("autofix job has no timeout-minutes") } + // The App private key is a secret of this environment; its deployment- + // branch policy (configured outside this repo) is what actually + // restricts reading it to jobs running on main. + if job.Environment.Name != "autofix" { + t.Errorf("autofix job environment = %q, want %q", job.Environment.Name, "autofix") + } var checkouts int for _, step := range job.Steps { if step.Uses != "" { family, sha, _ := strings.Cut(step.Uses, "@") - if family != "actions/checkout" || sha != checkoutSHA { - t.Errorf("step %q uses %q; only actions/checkout@%s is allowed", step.Name, step.Uses, checkoutSHA) + switch family { + case "actions/checkout": + if sha != checkoutSHA { + t.Errorf("step %q uses %q; only actions/checkout@%s is allowed", step.Name, step.Uses, checkoutSHA) + } + if !reflect.DeepEqual(step.With, map[string]string{"persist-credentials": "false"}) { + t.Errorf("checkout has with %v; want only persist-credentials: false (base default branch, never a PR ref, no token on disk)", step.With) + } + checkouts++ + case "actions/create-github-app-token": + if want := appTokenActionSHA; sha != want { + t.Errorf("step %q uses %q; want actions/create-github-app-token@%s", step.Name, step.Uses, want) + } + if step.If != "github.event.workflow_run.head_repository.full_name == github.repository" { + t.Errorf("app-token step if = %q, want the same-repo check (no token we hold can push to a fork)", step.If) + } + want := map[string]string{ + "app-id": "${{ secrets.AUTOFIX_APP_ID }}", + "private-key": "${{ secrets.AUTOFIX_APP_PRIVATE_KEY }}", + "owner": "${{ github.repository_owner }}", + "repositories": "${{ github.event.repository.name }}", + "permission-contents": "write", + } + if !reflect.DeepEqual(step.With, want) { + t.Errorf("app-token step with = %v, want %v", step.With, want) + } + default: + t.Errorf("step %q uses %q; only actions/checkout@%s or actions/create-github-app-token@%s is allowed", step.Name, step.Uses, checkoutSHA, appTokenActionSHA) } - if !reflect.DeepEqual(step.With, map[string]string{"persist-credentials": "false"}) { - t.Errorf("checkout has with %v; want only persist-credentials: false (base default branch, never a PR ref, no token on disk)", step.With) - } - checkouts++ } // Event fields (branch names, commit messages) are attacker text: // pass them through env, never interpolate them into a script. @@ -6247,7 +6279,11 @@ func TestBazelAutofixWorkflowSecurity(t *testing.T) { if checkouts != 1 { t.Errorf("want exactly one checkout step, got %d", checkouts) } + appToken := job.step(t, "Mint autofix app token") push := job.step(t, "Push sync commit or leave apply recipe") + if job.stepIndex(t, appToken.Name) >= job.stepIndex(t, push.Name) { + t.Errorf("%q must run before %q", appToken.Name, push.Name) + } if strings.TrimSpace(push.Run) != "./"+bazelAutofixPushScript { t.Errorf("push step run = %q, want ./%s", push.Run, bazelAutofixPushScript) } @@ -6261,21 +6297,27 @@ func TestBazelAutofixWorkflowSecurity(t *testing.T) { "HEAD_BRANCH": "${{ github.event.workflow_run.head_branch }}", "HEAD_SHA": "${{ github.event.workflow_run.head_sha }}", "GH_TOKEN": "${{ github.token }}", - "PUSH_TOKEN": "${{ secrets.DOCS_AUTOFIX_TOKEN || github.token }}", - "AUTOFIX_TOKEN_KIND": "${{ secrets.DOCS_AUTOFIX_TOKEN && 'pat' || 'default' }}", + "PUSH_TOKEN": "${{ steps.app-token.outputs.token || github.token }}", + "AUTOFIX_TOKEN_KIND": "${{ steps.app-token.outputs.token && 'app' || 'default' }}", } { if got := push.Env[key]; got != want { t.Errorf("push step env %s = %q, want %q", key, got, want) } } + // PUSH_TOKEN must never be fed by anything a PR controls: only the + // app-token step's output (itself minted from environment secrets) or + // the workflow's own token. + if regexp.MustCompile(`\bgithub\.event\.(workflow_run|pull_request)\b`).MatchString(push.Env["PUSH_TOKEN"]) { + t.Errorf("push step env PUSH_TOKEN = %q reads a PR-controlled event field", push.Env["PUSH_TOKEN"]) + } - // Secrets: only the push step's PUSH_TOKEN / AUTOFIX_TOKEN_KIND, and only - // the docs autofix token that this workflow shares. - pushIndex := job.stepIndex(t, push.Name) + // Secrets: only the app-token step's app-id/private-key, and only the + // autofix App credentials this workflow shares. + appTokenIndex := job.stepIndex(t, appToken.Name) secretRef := regexp.MustCompile(`\bsecrets\s*(\.|\[)`) allowed := map[string]bool{ - fmt.Sprintf(".jobs.autofix.steps[%d].env.PUSH_TOKEN", pushIndex): true, - fmt.Sprintf(".jobs.autofix.steps[%d].env.AUTOFIX_TOKEN_KIND", pushIndex): true, + fmt.Sprintf(".jobs.autofix.steps[%d].with.app-id", appTokenIndex): true, + fmt.Sprintf(".jobs.autofix.steps[%d].with.private-key", appTokenIndex): true, } walkYAML(root, "", func(path string, key bool, value string) { if key || !secretRef.MatchString(value) { @@ -6288,8 +6330,8 @@ func TestBazelAutofixWorkflowSecurity(t *testing.T) { t.Errorf("%s: %s uses a non-literal secrets reference", bazelAutofixWorkflowName, path) } else { for _, ref := range refs { - if ref[1] != "DOCS_AUTOFIX_TOKEN" { - t.Errorf("%s: %s reads secret %s; only DOCS_AUTOFIX_TOKEN is shared with this workflow", bazelAutofixWorkflowName, path, ref[1]) + if ref[1] != "AUTOFIX_APP_ID" && ref[1] != "AUTOFIX_APP_PRIVATE_KEY" { + t.Errorf("%s: %s reads secret %s; only AUTOFIX_APP_ID / AUTOFIX_APP_PRIVATE_KEY are shared with this workflow", bazelAutofixWorkflowName, path, ref[1]) } } } @@ -6743,3 +6785,27 @@ func TestBazelCmdDoltJob(t *testing.T) { } } } + +// ciWorkflowEnvironment is a job's environment, written either as a bare name +// (environment: autofix) or as a mapping (environment: {name: github-pages, +// url: ...}, deploy-pages-redirect.yml). +type ciWorkflowEnvironment struct { + Name string + URL string +} + +func (e *ciWorkflowEnvironment) UnmarshalYAML(value *yaml.Node) error { + if value.Kind == yaml.ScalarNode { + e.Name = value.Value + return nil + } + var m struct { + Name string `yaml:"name"` + URL string `yaml:"url"` + } + if err := value.Decode(&m); err != nil { + return err + } + e.Name, e.URL = m.Name, m.URL + return nil +} diff --git a/scripts/docs-autofix-push.sh b/scripts/docs-autofix-push.sh index 8f7cebe66..5d47ed555 100755 --- a/scripts/docs-autofix-push.sh +++ b/scripts/docs-autofix-push.sh @@ -32,12 +32,13 @@ # RUN_ID workflow run id that produced the patch (for comment text) # RUN_URL html url of that run (for commit/comment provenance) # GH_TOKEN token for gh api calls (PR lookup, comments) - needs the -# workflow's pull-requests:write; never the PAT +# workflow's pull-requests:write; never the App token # PUSH_TOKEN token for git fetch/push only (optional; defaults to -# GH_TOKEN), so a dedicated DOCS_AUTOFIX_TOKEN needs -# contents:write only -# AUTOFIX_TOKEN_KIND "pat" when a dedicated push token is in use, "default" -# for the workflow's GITHUB_TOKEN (retrigger caveat) +# GH_TOKEN), so the minted gastownhall-autofix App token +# needs contents:write only +# AUTOFIX_TOKEN_KIND "app" when the minted App push token is in use, +# "default" for the workflow's GITHUB_TOKEN (retrigger +# caveat) # # Exit 0 on every non-actionable outcome (PR closed, head moved, no patch); # exit 1 on a refused patch or a genuine error so the workflow surfaces them. @@ -378,7 +379,7 @@ EOF if [ "$AUTOFIX_TOKEN_KIND" = "default" ]; then cat >> "$BODY" <<'EOF' -Note: this commit was pushed with the default workflow token, which does **not** retrigger PR checks - re-run them (or push any commit) to refresh the gate. Configuring a `DOCS_AUTOFIX_TOKEN` repo secret removes this step. +Note: this commit was pushed with the default workflow token, which does **not** retrigger PR checks - re-run them (or push any commit) to refresh the gate. This only happens if the gastownhall-autofix App token could not be minted for this push. EOF fi post_or_update_comment "$BODY"