Skip to content

fix: Escape file paths in contents methods - #4721

Open
ranjithguggilla wants to merge 1 commit into
google:masterfrom
ranjithguggilla:fix/escape-contents-file-paths
Open

ranjithguggilla wants to merge 1 commit into
google:masterfrom
ranjithguggilla:fix/escape-contents-file-paths

Conversation

@ranjithguggilla

@ranjithguggilla ranjithguggilla commented Nov 17, 2026 •

Copy link
Copy Markdown

Fixes #4720.

CreateFile, UpdateFile and DeleteFile insert the file path into the request URL without escaping it, unlike GetContents. A path containing # or ? is cut at that character, so the request goes to a different file (docs/C#.md becomes docs/C), and a path with a bare % fails with a URL parse error.

This escapes the path the same way GetContents already does.

I added TestRepositoriesService_filePath_isEscaped, which covers the three methods. Without the source change 12 of its 18 subtests fail; with it they all pass. script/fmt.sh, script/lint.sh and script/test.sh pass.

Note: a caller that pre-escaped the path as a workaround will now have it escaped twice.

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.
@google-cla

google-cla Bot commented Nov 17, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gmlewis gmlewis added the NeedsReview PR is awaiting a review before merging. label Nov 17, 2026
@codecov

codecov Bot commented Nov 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.64%. Comparing base (5d83c3d) to head (fb3961b).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4721   +/-   ##
=======================================
  Coverage   98.64%   98.64%           
=======================================
  Files         204      204           
  Lines       19159    19162    +3     
=======================================
+ Hits        18899    18902    +3     
  Misses        260      260           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread github/repos_contents.go
Comment on lines +269 to +270
escapedPath := (&url.URL{Path: path}).String()
u := fmt.Sprintf("repos/%v/%v/contents/%v", owner, repo, escapedPath)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment that path is now escaped just like we do for the other endpoints.
Here and everywhere else:

Suggested change
escapedPath := (&url.URL{Path: path}).String()
u := fmt.Sprintf("repos/%v/%v/contents/%v", owner, repo, escapedPath)
u := fmt.Sprintf("repos/%v/%v/contents/%v", owner, repo, url.QueryEscape(path))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

NeedsReview PR is awaiting a review before merging.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

File mutation methods do not escape paths

2 participants