From fb3961b03384007459514fe6993ccbc4b8968c79 Mon Sep 17 00:00:00 2001 From: Ranjith Guggilla Date: Tue, 17 Nov 2026 10:19:56 +0000 Subject: [PATCH] fix: Escape file paths in contents methods CreateFile, UpdateFile, and DeleteFile put the file path into the request URL without escaping it, unlike GetContents. A path containing '#' or '?' was cut at that character, so the request targeted a different file, and a path containing a bare '%' failed with a URL parse error. Escape the path the same way GetContents does and add a table test covering the three methods. --- github/repos_contents.go | 9 +++-- github/repos_contents_test.go | 71 +++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/github/repos_contents.go b/github/repos_contents.go index dc6b4a03c1e..f010bcb6a37 100644 --- a/github/repos_contents.go +++ b/github/repos_contents.go @@ -266,7 +266,8 @@ func (s *RepositoriesService) GetContents(ctx context.Context, owner, repo, path // //meta:operation PUT /repos/{owner}/{repo}/contents/{path} func (s *RepositoriesService) CreateFile(ctx context.Context, owner, repo, path string, body *RepositoryContentFileOptions) (*RepositoryContentResponse, *Response, error) { - u := fmt.Sprintf("repos/%v/%v/contents/%v", owner, repo, path) + escapedPath := (&url.URL{Path: path}).String() + u := fmt.Sprintf("repos/%v/%v/contents/%v", owner, repo, escapedPath) req, err := s.client.NewRequest(ctx, "PUT", u, body) if err != nil { return nil, nil, err @@ -288,7 +289,8 @@ func (s *RepositoriesService) CreateFile(ctx context.Context, owner, repo, path // //meta:operation PUT /repos/{owner}/{repo}/contents/{path} func (s *RepositoriesService) UpdateFile(ctx context.Context, owner, repo, path string, body *RepositoryContentFileOptions) (*RepositoryContentResponse, *Response, error) { - u := fmt.Sprintf("repos/%v/%v/contents/%v", owner, repo, path) + escapedPath := (&url.URL{Path: path}).String() + u := fmt.Sprintf("repos/%v/%v/contents/%v", owner, repo, escapedPath) req, err := s.client.NewRequest(ctx, "PUT", u, body) if err != nil { return nil, nil, err @@ -310,7 +312,8 @@ func (s *RepositoriesService) UpdateFile(ctx context.Context, owner, repo, path // //meta:operation DELETE /repos/{owner}/{repo}/contents/{path} func (s *RepositoriesService) DeleteFile(ctx context.Context, owner, repo, path string, opts *RepositoryContentFileOptions) (*RepositoryContentResponse, *Response, error) { - u := fmt.Sprintf("repos/%v/%v/contents/%v", owner, repo, path) + escapedPath := (&url.URL{Path: path}).String() + u := fmt.Sprintf("repos/%v/%v/contents/%v", owner, repo, escapedPath) req, err := s.client.NewRequest(ctx, "DELETE", u, opts) if err != nil { return nil, nil, err diff --git a/github/repos_contents_test.go b/github/repos_contents_test.go index e59dff65310..44a5c38bf12 100644 --- a/github/repos_contents_test.go +++ b/github/repos_contents_test.go @@ -6,6 +6,7 @@ package github import ( + "context" "errors" "fmt" "io" @@ -945,6 +946,76 @@ func TestRepositoriesService_DeleteFile(t *testing.T) { }) } +func TestRepositoriesService_filePath_isEscaped(t *testing.T) { + t.Parallel() + for _, file := range []struct { + path string + escapedPath string + }{ + {path: "p", escapedPath: "p"}, + {path: "some directory/file.go", escapedPath: "some%20directory/file.go"}, + {path: "docs/C#.md", escapedPath: "docs/C%23.md"}, + {path: "docs/why?.md", escapedPath: "docs/why%3F.md"}, + {path: "docs/100%.md", escapedPath: "docs/100%25.md"}, + {path: "docs/50%25.md", escapedPath: "docs/50%2525.md"}, + } { + for _, tt := range []struct { + method string + wantMethod string + call func(ctx context.Context, client *Client) error + }{ + { + method: "CreateFile", + wantMethod: "PUT", + call: func(ctx context.Context, client *Client) error { + _, _, err := client.Repositories.CreateFile(ctx, "o", "r", file.path, &RepositoryContentFileOptions{}) + return err + }, + }, + { + method: "UpdateFile", + wantMethod: "PUT", + call: func(ctx context.Context, client *Client) error { + _, _, err := client.Repositories.UpdateFile(ctx, "o", "r", file.path, &RepositoryContentFileOptions{}) + return err + }, + }, + { + method: "DeleteFile", + wantMethod: "DELETE", + call: func(ctx context.Context, client *Client) error { + _, _, err := client.Repositories.DeleteFile(ctx, "o", "r", file.path, &RepositoryContentFileOptions{}) + return err + }, + }, + } { + t.Run(tt.method+"/"+file.path, func(t *testing.T) { + t.Parallel() + client, mux, _ := setup(t) + + const wantPath = "/repos/o/r/contents/" + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, tt.wantMethod) + if got, want := r.URL.EscapedPath(), wantPath+file.escapedPath; got != want { + t.Errorf("Request path = %q, want %q", got, want) + } + if got, want := r.URL.Path, wantPath+file.path; got != want { + t.Errorf("Decoded request path = %q, want %q", got, want) + } + if got := r.URL.RawQuery; got != "" { + t.Errorf("Request query = %q, want none", got) + } + fmt.Fprint(w, `{}`) + }) + + if err := tt.call(t.Context(), client); err != nil { + t.Fatalf("Repositories.%v returned error: %v", tt.method, err) + } + }) + } + } +} + func TestRepositoriesService_GetArchiveLink(t *testing.T) { t.Parallel() tcs := []struct {