From fce4e128ef014e98eca2891846209e751f29a62c Mon Sep 17 00:00:00 2001 From: Anuj Yamdagni Date: Tue, 1 Dec 2026 10:18:09 -0500 Subject: [PATCH] fix: Escape workflow file names in URL paths The six *ByFileName methods in ActionsService put the workflow file name into the URL path unescaped. A name containing # is cut off at the fragment, and a percent-escape in a name is sent as is and decoded by the server, so the request fails or reaches the wrong workflow. Escape the name with url.PathEscape, as #4536, #4546 and #4750 did for other path parameters. --- github/actions_workflow_runs.go | 4 +- github/actions_workflow_runs_test.go | 57 +++++++++++++++ github/actions_workflows.go | 21 ++++-- github/actions_workflows_test.go | 101 +++++++++++++++++++++++++++ 4 files changed, 177 insertions(+), 6 deletions(-) diff --git a/github/actions_workflow_runs.go b/github/actions_workflow_runs.go index f44f4e10c0c..e962eecf3fe 100644 --- a/github/actions_workflow_runs.go +++ b/github/actions_workflow_runs.go @@ -170,11 +170,13 @@ func (s *ActionsService) ListWorkflowRunsByID(ctx context.Context, owner, repo s // ListWorkflowRunsByFileName lists all workflow runs by workflow file name. // +// Note: the workflow file name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . +// // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-workflow // //meta:operation GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs func (s *ActionsService) ListWorkflowRunsByFileName(ctx context.Context, owner, repo, workflowFileName string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) { - u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/runs", owner, repo, workflowFileName) + u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/runs", owner, repo, url.PathEscape(workflowFileName)) return s.listWorkflowRuns(ctx, u, opts) } diff --git a/github/actions_workflow_runs_test.go b/github/actions_workflow_runs_test.go index 54dd8c84683..fae1c8e0d9c 100644 --- a/github/actions_workflow_runs_test.go +++ b/github/actions_workflow_runs_test.go @@ -6,6 +6,7 @@ package github import ( + "context" "errors" "fmt" "net/http" @@ -1107,3 +1108,59 @@ func TestActionsService_GetPendingDeployments(t *testing.T) { return resp, err }) } + +func TestActionsService_ListWorkflowRunsByFileName_isEscaped(t *testing.T) { + t.Parallel() + for _, workflow := range []struct { + name string + escapedName string + }{ + {name: "main.yml", escapedName: "main.yml"}, + {name: "ci#1.yml", escapedName: "ci%231.yml"}, + {name: "build test.yml", escapedName: "build%20test.yml"}, + {name: "ci%231.yml", escapedName: "ci%25231.yml"}, + } { + for _, tt := range []struct { + method string + wantPrefix string + wantSuffix string + wantMethod string + body string + call func(ctx context.Context, client *Client) error + }{ + { + method: "ListWorkflowRunsByFileName", + wantPrefix: "/repos/o/r/actions/workflows/", + wantSuffix: "/runs", + wantMethod: "GET", + body: `{}`, + call: func(ctx context.Context, client *Client) error { + _, _, err := client.Actions.ListWorkflowRunsByFileName(ctx, "o", "r", workflow.name, nil) + return err + }, + }, + } { + t.Run(tt.method+"/"+workflow.name, func(t *testing.T) { + t.Parallel() + client, mux, _ := setup(t) + + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, tt.wantMethod) + if got, want := r.URL.EscapedPath(), tt.wantPrefix+workflow.escapedName+tt.wantSuffix; got != want { + t.Errorf("Request path = %q, want %q", got, want) + } + if got, want := r.URL.Path, tt.wantPrefix+workflow.name+tt.wantSuffix; got != want { + t.Errorf("Decoded request path = %q, want %q", got, want) + } + if tt.body != "" { + fmt.Fprint(w, tt.body) + } + }) + + if err := tt.call(t.Context(), client); err != nil { + t.Fatalf("Actions.%v returned error: %v", tt.method, err) + } + }) + } + } +} diff --git a/github/actions_workflows.go b/github/actions_workflows.go index 44176145ea4..8b787f6c918 100644 --- a/github/actions_workflows.go +++ b/github/actions_workflows.go @@ -8,6 +8,7 @@ package github import ( "context" "fmt" + "net/url" ) // Workflow represents a repository action workflow. @@ -137,11 +138,13 @@ func (s *ActionsService) GetWorkflowByID(ctx context.Context, owner, repo string // GetWorkflowByFileName gets a specific workflow by file name. // +// Note: the workflow file name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . +// // GitHub API docs: https://docs.github.com/rest/actions/workflows?apiVersion=2022-11-28#get-a-workflow // //meta:operation GET /repos/{owner}/{repo}/actions/workflows/{workflow_id} func (s *ActionsService) GetWorkflowByFileName(ctx context.Context, owner, repo, workflowFileName string) (*Workflow, *Response, error) { - u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v", owner, repo, workflowFileName) + u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v", owner, repo, url.PathEscape(workflowFileName)) return s.getWorkflow(ctx, u) } @@ -174,11 +177,13 @@ func (s *ActionsService) GetWorkflowUsageByID(ctx context.Context, owner, repo s // GetWorkflowUsageByFileName gets a specific workflow usage by file name in the unit of billable milliseconds. // +// Note: the workflow file name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . +// // GitHub API docs: https://docs.github.com/rest/actions/workflows?apiVersion=2022-11-28#get-workflow-usage // //meta:operation GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/timing func (s *ActionsService) GetWorkflowUsageByFileName(ctx context.Context, owner, repo, workflowFileName string) (*WorkflowUsage, *Response, error) { - u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/timing", owner, repo, workflowFileName) + u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/timing", owner, repo, url.PathEscape(workflowFileName)) return s.getWorkflowUsage(ctx, u) } @@ -211,11 +216,13 @@ func (s *ActionsService) CreateWorkflowDispatchEventByID(ctx context.Context, ow // CreateWorkflowDispatchEventByFileName manually triggers a GitHub Actions workflow run. // +// Note: the workflow file name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . +// // GitHub API docs: https://docs.github.com/rest/actions/workflows?apiVersion=2022-11-28#create-a-workflow-dispatch-event // //meta:operation POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches func (s *ActionsService) CreateWorkflowDispatchEventByFileName(ctx context.Context, owner, repo, workflowFileName string, body CreateWorkflowDispatchEventRequest) (*WorkflowDispatchRunDetails, *Response, error) { - u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/dispatches", owner, repo, workflowFileName) + u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/dispatches", owner, repo, url.PathEscape(workflowFileName)) return s.createWorkflowDispatchEvent(ctx, u, &body) } @@ -247,11 +254,13 @@ func (s *ActionsService) EnableWorkflowByID(ctx context.Context, owner, repo str // EnableWorkflowByFileName enables a workflow and sets the state of the workflow to "active". // +// Note: the workflow file name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . +// // GitHub API docs: https://docs.github.com/rest/actions/workflows?apiVersion=2022-11-28#enable-a-workflow // //meta:operation PUT /repos/{owner}/{repo}/actions/workflows/{workflow_id}/enable func (s *ActionsService) EnableWorkflowByFileName(ctx context.Context, owner, repo, workflowFileName string) (*Response, error) { - u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/enable", owner, repo, workflowFileName) + u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/enable", owner, repo, url.PathEscape(workflowFileName)) return s.doNewPutRequest(ctx, u) } @@ -267,11 +276,13 @@ func (s *ActionsService) DisableWorkflowByID(ctx context.Context, owner, repo st // DisableWorkflowByFileName disables a workflow and sets the state of the workflow to "disabled_manually". // +// Note: the workflow file name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape . +// // GitHub API docs: https://docs.github.com/rest/actions/workflows?apiVersion=2022-11-28#disable-a-workflow // //meta:operation PUT /repos/{owner}/{repo}/actions/workflows/{workflow_id}/disable func (s *ActionsService) DisableWorkflowByFileName(ctx context.Context, owner, repo, workflowFileName string) (*Response, error) { - u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/disable", owner, repo, workflowFileName) + u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/disable", owner, repo, url.PathEscape(workflowFileName)) return s.doNewPutRequest(ctx, u) } diff --git a/github/actions_workflows_test.go b/github/actions_workflows_test.go index c708c4af455..cbf822b8c5f 100644 --- a/github/actions_workflows_test.go +++ b/github/actions_workflows_test.go @@ -6,6 +6,7 @@ package github import ( + "context" "fmt" "net/http" "testing" @@ -531,3 +532,103 @@ func TestActionsService_DisableWorkflowByFileName(t *testing.T) { return client.Actions.DisableWorkflowByFileName(ctx, "o", "r", "main.yml") }) } + +func TestActionsService_workflowFileName_isEscaped(t *testing.T) { + t.Parallel() + for _, workflow := range []struct { + name string + escapedName string + }{ + {name: "main.yml", escapedName: "main.yml"}, + {name: "ci#1.yml", escapedName: "ci%231.yml"}, + {name: "build test.yml", escapedName: "build%20test.yml"}, + {name: "ci%231.yml", escapedName: "ci%25231.yml"}, + } { + for _, tt := range []struct { + method string + wantPrefix string + wantSuffix string + wantMethod string + body string + call func(ctx context.Context, client *Client) error + }{ + { + method: "GetWorkflowByFileName", + wantPrefix: "/repos/o/r/actions/workflows/", + wantSuffix: "", + wantMethod: "GET", + body: `{}`, + call: func(ctx context.Context, client *Client) error { + _, _, err := client.Actions.GetWorkflowByFileName(ctx, "o", "r", workflow.name) + return err + }, + }, + { + method: "GetWorkflowUsageByFileName", + wantPrefix: "/repos/o/r/actions/workflows/", + wantSuffix: "/timing", + wantMethod: "GET", + body: `{}`, + call: func(ctx context.Context, client *Client) error { + _, _, err := client.Actions.GetWorkflowUsageByFileName(ctx, "o", "r", workflow.name) + return err + }, + }, + { + method: "CreateWorkflowDispatchEventByFileName", + wantPrefix: "/repos/o/r/actions/workflows/", + wantSuffix: "/dispatches", + wantMethod: "POST", + body: `{}`, + call: func(ctx context.Context, client *Client) error { + _, _, err := client.Actions.CreateWorkflowDispatchEventByFileName(ctx, "o", "r", workflow.name, CreateWorkflowDispatchEventRequest{Ref: "main"}) + return err + }, + }, + { + method: "EnableWorkflowByFileName", + wantPrefix: "/repos/o/r/actions/workflows/", + wantSuffix: "/enable", + wantMethod: "PUT", + body: ``, + call: func(ctx context.Context, client *Client) error { + _, err := client.Actions.EnableWorkflowByFileName(ctx, "o", "r", workflow.name) + return err + }, + }, + { + method: "DisableWorkflowByFileName", + wantPrefix: "/repos/o/r/actions/workflows/", + wantSuffix: "/disable", + wantMethod: "PUT", + body: ``, + call: func(ctx context.Context, client *Client) error { + _, err := client.Actions.DisableWorkflowByFileName(ctx, "o", "r", workflow.name) + return err + }, + }, + } { + t.Run(tt.method+"/"+workflow.name, func(t *testing.T) { + t.Parallel() + client, mux, _ := setup(t) + + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, tt.wantMethod) + if got, want := r.URL.EscapedPath(), tt.wantPrefix+workflow.escapedName+tt.wantSuffix; got != want { + t.Errorf("Request path = %q, want %q", got, want) + } + if got, want := r.URL.Path, tt.wantPrefix+workflow.name+tt.wantSuffix; got != want { + t.Errorf("Decoded request path = %q, want %q", got, want) + } + if tt.body != "" { + fmt.Fprint(w, tt.body) + } + }) + + if err := tt.call(t.Context(), client); err != nil { + t.Fatalf("Actions.%v returned error: %v", tt.method, err) + } + }) + } + } +}