diff --git a/.github/workflows/reusable-dispatch.yml b/.github/workflows/reusable-dispatch.yml index f52e60c1fd..84a985b42a 100644 --- a/.github/workflows/reusable-dispatch.yml +++ b/.github/workflows/reusable-dispatch.yml @@ -1650,7 +1650,7 @@ jobs: [[ -f "${script}" ]] || script=".fullsend/.defaults/.github/scripts/report-agent-startup-failure.sh" if [[ ! -f "${script}" ]]; then script="${RUNNER_TEMP}/fullsend-agent-startup-failure.sh" - if encoded="$(gh api "repos/${{ job.workflow_repository }}/contents/.github/scripts/report-agent-startup-failure.sh?ref=${{ job.workflow_sha }}" --jq .content)"; then + if encoded="$(gh api "repos/${{ job.workflow_repository }}/contents/.github/scripts/report-agent-startup-failure.sh?ref=${{ (github.event_name == 'pull_request' && job.workflow_sha == github.sha && github.event.pull_request.base.sha || job.workflow_sha) }}" --jq .content)"; then printf '%s' "${encoded}" | base64 --decode > "${script}" chmod +x "${script}" else @@ -1825,7 +1825,7 @@ jobs: [[ -f "${script}" ]] || script=".fullsend/.defaults/.github/scripts/report-agent-startup-failure.sh" if [[ ! -f "${script}" ]]; then script="${RUNNER_TEMP}/fullsend-agent-startup-failure.sh" - if encoded="$(gh api "repos/${{ job.workflow_repository }}/contents/.github/scripts/report-agent-startup-failure.sh?ref=${{ job.workflow_sha }}" --jq .content)"; then + if encoded="$(gh api "repos/${{ job.workflow_repository }}/contents/.github/scripts/report-agent-startup-failure.sh?ref=${{ (github.event_name == 'pull_request' && job.workflow_sha == github.sha && github.event.pull_request.base.sha || job.workflow_sha) }}" --jq .content)"; then printf '%s' "${encoded}" | base64 --decode > "${script}" chmod +x "${script}" else @@ -2041,7 +2041,7 @@ jobs: [[ -f "${script}" ]] || script=".fullsend/.defaults/.github/scripts/report-agent-startup-failure.sh" if [[ ! -f "${script}" ]]; then script="${RUNNER_TEMP}/fullsend-agent-startup-failure.sh" - if encoded="$(gh api "repos/${{ job.workflow_repository }}/contents/.github/scripts/report-agent-startup-failure.sh?ref=${{ job.workflow_sha }}" --jq .content)"; then + if encoded="$(gh api "repos/${{ job.workflow_repository }}/contents/.github/scripts/report-agent-startup-failure.sh?ref=${{ (github.event_name == 'pull_request' && job.workflow_sha == github.sha && github.event.pull_request.base.sha || job.workflow_sha) }}" --jq .content)"; then printf '%s' "${encoded}" | base64 --decode > "${script}" chmod +x "${script}" else @@ -2349,7 +2349,7 @@ jobs: [[ -f "${script}" ]] || script=".fullsend/.defaults/.github/scripts/report-agent-startup-failure.sh" if [[ ! -f "${script}" ]]; then script="${RUNNER_TEMP}/fullsend-agent-startup-failure.sh" - if encoded="$(gh api "repos/${{ job.workflow_repository }}/contents/.github/scripts/report-agent-startup-failure.sh?ref=${{ job.workflow_sha }}" --jq .content)"; then + if encoded="$(gh api "repos/${{ job.workflow_repository }}/contents/.github/scripts/report-agent-startup-failure.sh?ref=${{ (github.event_name == 'pull_request' && job.workflow_sha == github.sha && github.event.pull_request.base.sha || job.workflow_sha) }}" --jq .content)"; then printf '%s' "${encoded}" | base64 --decode > "${script}" chmod +x "${script}" else @@ -2502,7 +2502,7 @@ jobs: [[ -f "${script}" ]] || script=".fullsend/.defaults/.github/scripts/report-agent-startup-failure.sh" if [[ ! -f "${script}" ]]; then script="${RUNNER_TEMP}/fullsend-agent-startup-failure.sh" - if encoded="$(gh api "repos/${{ job.workflow_repository }}/contents/.github/scripts/report-agent-startup-failure.sh?ref=${{ job.workflow_sha }}" --jq .content)"; then + if encoded="$(gh api "repos/${{ job.workflow_repository }}/contents/.github/scripts/report-agent-startup-failure.sh?ref=${{ (github.event_name == 'pull_request' && job.workflow_sha == github.sha && github.event.pull_request.base.sha || job.workflow_sha) }}" --jq .content)"; then printf '%s' "${encoded}" | base64 --decode > "${script}" chmod +x "${script}" else @@ -2633,7 +2633,7 @@ jobs: [[ -f "${script}" ]] || script=".fullsend/.defaults/.github/scripts/report-agent-startup-failure.sh" if [[ ! -f "${script}" ]]; then script="${RUNNER_TEMP}/fullsend-agent-startup-failure.sh" - if encoded="$(gh api "repos/${{ job.workflow_repository }}/contents/.github/scripts/report-agent-startup-failure.sh?ref=${{ job.workflow_sha }}" --jq .content)"; then + if encoded="$(gh api "repos/${{ job.workflow_repository }}/contents/.github/scripts/report-agent-startup-failure.sh?ref=${{ (github.event_name == 'pull_request' && job.workflow_sha == github.sha && github.event.pull_request.base.sha || job.workflow_sha) }}" --jq .content)"; then printf '%s' "${encoded}" | base64 --decode > "${script}" chmod +x "${script}" else @@ -2832,7 +2832,7 @@ jobs: [[ -f "${script}" ]] || script=".fullsend/.defaults/.github/scripts/report-agent-startup-failure.sh" if [[ ! -f "${script}" ]]; then script="${RUNNER_TEMP}/fullsend-agent-startup-failure.sh" - if encoded="$(gh api "repos/${{ job.workflow_repository }}/contents/.github/scripts/report-agent-startup-failure.sh?ref=${{ job.workflow_sha }}" --jq .content)"; then + if encoded="$(gh api "repos/${{ job.workflow_repository }}/contents/.github/scripts/report-agent-startup-failure.sh?ref=${{ (github.event_name == 'pull_request' && job.workflow_sha == github.sha && github.event.pull_request.base.sha || job.workflow_sha) }}" --jq .content)"; then printf '%s' "${encoded}" | base64 --decode > "${script}" chmod +x "${script}" else @@ -3103,7 +3103,7 @@ jobs: [[ -f "${script}" ]] || script=".fullsend/.defaults/.github/scripts/report-agent-startup-failure.sh" if [[ ! -f "${script}" ]]; then script="${RUNNER_TEMP}/fullsend-agent-startup-failure.sh" - if encoded="$(gh api "repos/${{ job.workflow_repository }}/contents/.github/scripts/report-agent-startup-failure.sh?ref=${{ job.workflow_sha }}" --jq .content)"; then + if encoded="$(gh api "repos/${{ job.workflow_repository }}/contents/.github/scripts/report-agent-startup-failure.sh?ref=${{ (github.event_name == 'pull_request' && job.workflow_sha == github.sha && github.event.pull_request.base.sha || job.workflow_sha) }}" --jq .content)"; then printf '%s' "${encoded}" | base64 --decode > "${script}" chmod +x "${script}" else diff --git a/.github/workflows/reusable-prioritize.yml b/.github/workflows/reusable-prioritize.yml index 0b3ad0b5e6..d4ad1cf1e3 100644 --- a/.github/workflows/reusable-prioritize.yml +++ b/.github/workflows/reusable-prioritize.yml @@ -240,7 +240,7 @@ jobs: [[ -f "${script}" ]] || script=".fullsend/.defaults/.github/scripts/report-agent-startup-failure.sh" if [[ ! -f "${script}" ]]; then script="${RUNNER_TEMP}/fullsend-agent-startup-failure.sh" - if encoded="$(gh api "repos/${{ job.workflow_repository }}/contents/.github/scripts/report-agent-startup-failure.sh?ref=${{ job.workflow_sha }}" --jq .content)"; then + if encoded="$(gh api "repos/${{ job.workflow_repository }}/contents/.github/scripts/report-agent-startup-failure.sh?ref=${{ (github.event_name == 'pull_request' && job.workflow_sha == github.sha && github.event.pull_request.base.sha || job.workflow_sha) }}" --jq .content)"; then printf '%s' "${encoded}" | base64 --decode > "${script}" chmod +x "${script}" else diff --git a/internal/scaffold/pr_trigger_test.go b/internal/scaffold/pr_trigger_test.go index 3ce1edc169..3cd4b46c2e 100644 --- a/internal/scaffold/pr_trigger_test.go +++ b/internal/scaffold/pr_trigger_test.go @@ -1,6 +1,7 @@ package scaffold import ( + "fmt" "regexp" "strings" "testing" @@ -135,26 +136,53 @@ func TestEveryCheckoutHasTrustedRef(t *testing.T) { } } +// workflowSHAViolations counts the lines in a workflow whose expressions read +// job.workflow_sha. For each line that reads it outside trustedWorkflowSHA it +// returns "file:line: text", with a 1-based line number. +func workflowSHAViolations(file string, content []byte) (uses int, violations []string) { + for i, line := range strings.Split(string(content), "\n") { + if !strings.Contains(line, "${{") || !strings.Contains(line, "job.workflow_sha") { + continue + } + uses++ + if strings.Contains(strings.ReplaceAll(line, trustedWorkflowSHA, ""), "job.workflow_sha") { + violations = append(violations, fmt.Sprintf("%s:%d: %s", file, i+1, strings.TrimSpace(line))) + } + } + return uses, violations +} + // TestWorkflowSHAUsesAreTrusted checks that every expression that reads // job.workflow_sha (checkout refs, CLI install and version inputs, cache // keys) goes through trustedWorkflowSHA. func TestWorkflowSHAUsesAreTrusted(t *testing.T) { for _, file := range []string{".github/workflows/reusable-dispatch.yml", ".github/workflows/reusable-prioritize.yml"} { t.Run(file, func(t *testing.T) { - var uses int - for _, line := range strings.Split(string(loadRepoFile(file)(t)), "\n") { - if !strings.Contains(line, "${{") || !strings.Contains(line, "job.workflow_sha") { - continue - } - uses++ - assert.NotContains(t, strings.ReplaceAll(line, trustedWorkflowSHA, ""), "job.workflow_sha", - "job.workflow_sha must be read through the trusted expression: %s", strings.TrimSpace(line)) + uses, violations := workflowSHAViolations(file, loadRepoFile(file)(t)) + for _, v := range violations { + t.Errorf("job.workflow_sha must be read through the trusted expression: %s", v) } assert.NotZero(t, uses) }) } } +// TestWorkflowSHAViolationsNamesFileAndLine checks that a bare +// job.workflow_sha read is reported with the workflow file and its 1-based +// line number, and that a read through trustedWorkflowSHA is not. +func TestWorkflowSHAViolationsNamesFileAndLine(t *testing.T) { + content := []byte("jobs:\n" + + " a:\n" + + " steps:\n" + + " - uses: actions/checkout@v4\n" + + " with:\n" + + " ref: ${{ " + trustedWorkflowSHA + " }}\n" + + " - run: gh api \"x?ref=${{ job.workflow_sha }}\"\n") + uses, violations := workflowSHAViolations("wf.yml", content) + assert.Equal(t, 2, uses) + assert.Equal(t, []string{`wf.yml:7: - run: gh api "x?ref=${{ job.workflow_sha }}"`}, violations) +} + // prDispatchGuard skips fork and Dependabot PRs on pull_request. const prDispatchGuard = "(github.event_name != 'pull_request' || (github.event.pull_request.head.repo.full_name ==" + " github.event.pull_request.base.repo.full_name && github.actor != 'dependabot[bot]'))"