Skip to content
Merged
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
16 changes: 8 additions & 8 deletions .github/workflows/reusable-dispatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/reusable-prioritize.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 36 additions & 8 deletions internal/scaffold/pr_trigger_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scaffold

import (
"fmt"
"regexp"
"strings"
"testing"
Expand Down Expand Up @@ -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]'))"
Expand Down
Loading