Repository navigation
feat(github): add utilities for URL encoding and validation, enhance error handling in operations and tests - #4142
Merged
Merged
Conversation
…error handling in operations and tests
Contributor
Coverage report (github)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
bogdankostic
marked this pull request as draft
October 21, 2026 13:28
bogdankostic
marked this pull request as ready for review
October 21, 2026 13:28
bogdankostic
requested review from
a team and
anakin87
and removed request for
a team
October 21, 2026 13:50
anakin87
approved these changes
Oct 22, 2026
anakin87
left a comment
Member
There was a problem hiding this comment.
I left two minor comments
Feel free to apply (or discard) them and merge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related Issues
Proposed Changes:
The GitHub connectors built GitHub REST API URLs by pasting caller input straight into f-strings, e.g.
f"https://api.github.com/repos/{owner}/{repo}/contents/{payload['path']}". Before sending, requests (via urllib3) removes.and..segments from the URL path, so a../in the input moves the request to a different endpoint.?and#in the input change the query string or cut off the rest of the URL. For example,GitHubFileEditor(repo="acme/website")withpayload["path"] = "../../../acme/payments/contents/x"sends the request to/repos/acme/payments/contents/x.GitHubFileEditorTool's default schema only exposescommandandpayloadto the LLM, whilerepoandbranchare fixed at init. A prompt injection, e.g. in an issue the agent reads, could therefore make the agent create, edit or delete files in any repo the token can write to. A literal%2e%2ein a path had the same effect, because requests decodes%2eto.after urllib3 has already removed dot segments, so a raw..reached GitHub.connectors/github/utils.py:encode_path()andencode_ref()split the input on/, reject empty,.and..segments with aValueError, and percent-encode each segment withquote(segment, safe=""). Dot segments are rejected before encoding becausequote()never encodes.. Other characters are encoded rather than rejected, since?,#,%and spaces are valid in file names and git allows#in branch names.validate_repo_part()only accepts owner and repo names matching[A-Za-z0-9._-]+, except.and... It usesfullmatch, because$would also accept a trailing newline.GitHubFileEditor: the file path is encoded in all fourcontents/URLs, the branch is encoded in thegit/refs/heads/URL ofundo, and the owner and repo fromrepoare validated inrun(). The command handlers now also catchValueError, soraise_on_failure=Truere-raises andFalsereturns"Error: ..."._undo_changesnow builds the ref URL before_check_last_commit, which used to send two requests first.GitHubRepoViewer: the path is encoded, with an empty path still meaning the repo root, and owner and repo are validated in_parse_repo().refis now passed asparams={"ref": ref}instead of being appended as?ref={ref}.GitHubPRCreator:base_branchandbranch_nameare encoded in thegit/refs/heads/URLs.GitHubIssueViewer,GitHubIssueCommenter,GitHubPRCreatorandGitHubRepoForker: the([^/]+)groups accept.and.., so the matched owner and repo are now validated too.Invalid input raises
ValueErrorbefore any request is sent.Not a breaking change for valid input: URLs for ordinary paths and branches (
README.md,src/app.py,docs/,main,feature/x) are byte-identical to before, andGitHubRepoViewerstill accepts""and"docs/". Paths with characters like#or?now reach the intended file instead of a truncated URL.How did you test it?
hatch run fmt,hatch run test:types,hatch run test:unit: all green, 208 unit tests.GitHubFileEditor: edit, create and delete with../../../other/repo/contents/x,a/../b,./xanda//bsend no request, return"Error: ...", and raiseValueErrorwithraise_on_failure=Truedocs/a b#1?.mdand%2e%2e/xare sent to.../contents/docs/a%20b%231%3F.mdand.../contents/%252e%252e/x.requests.Request(...).prepare().urlleaves both unchanged, so the URL that actually goes out still points into the repo'scontents/undowith an invalid branch sends no requestGitHubRepoViewer: traversal is rejected,refgoes throughparams, and""and"docs/"produce the same URLs as beforeGitHubPRCreator:feature/xworks and../xis rejected before any request../evil,owner/..,a/b/cand.are rejected inGitHubFileEditorandGitHubRepoViewer, and..and.in issue URLs are rejected by all four parsersNotes for the reviewer
test_run_fileandtest_run_directoryintest_repo_viewer.pyasserted?ref=mainin the URL string. They now assertparams={"ref": "main"}.test_run_sends_encoded_path_and_refchecks that the URL sent to GitHub is the same as before.repoinGitHubFileEditor.run()raisesValueErrorregardless ofraise_on_failure, like a malformedrepoor an unknown command already did. The same goes for_create_branch()and_create_commit()inGitHubPRCreator, which build the URL before theirtry. Neither is called fromrun().ValueErrorin theGitHubFileEditorhandlers also coversUnicodeDecodeError, so editing a binary file withraise_on_failure=Falsenow returns"Error: ..."instead of raising.Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:.