Skip to content

feat(github): add utilities for URL encoding and validation, enhance error handling in operations and tests - #4142

Merged
bogdankostic merged 3 commits into
mainfrom
fix/github-url-path-traversal
Nov 2, 2026
Merged

bogdankostic merged 3 commits into
mainfrom
fix/github-url-path-traversal

Conversation

@bogdankostic

Copy link
Copy Markdown
Contributor

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") with payload["path"] = "../../../acme/payments/contents/x" sends the request to /repos/acme/payments/contents/x.

GitHubFileEditorTool's default schema only exposes command and payload to the LLM, while repo and branch are 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%2e in a path had the same effect, because requests decodes %2e to . after urllib3 has already removed dot segments, so a raw .. reached GitHub.

  • New helpers in connectors/github/utils.py: encode_path() and encode_ref() split the input on /, reject empty, . and .. segments with a ValueError, and percent-encode each segment with quote(segment, safe=""). Dot segments are rejected before encoding because quote() 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 uses fullmatch, because $ would also accept a trailing newline.
  • GitHubFileEditor: the file path is encoded in all four contents/ URLs, the branch is encoded in the git/refs/heads/ URL of undo, and the owner and repo from repo are validated in run(). The command handlers now also catch ValueError, so raise_on_failure=True re-raises and False returns "Error: ...". _undo_changes now 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(). ref is now passed as params={"ref": ref} instead of being appended as ?ref={ref}.
  • GitHubPRCreator: base_branch and branch_name are encoded in the git/refs/heads/ URLs.
  • Issue URL parsing in GitHubIssueViewer, GitHubIssueCommenter, GitHubPRCreator and GitHubRepoForker: the ([^/]+) groups accept . and .., so the matched owner and repo are now validated too.

Invalid input raises ValueError before 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, and GitHubRepoViewer still 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.
  • Unit tests:
    • GitHubFileEditor: edit, create and delete with ../../../other/repo/contents/x, a/../b, ./x and a//b send no request, return "Error: ...", and raise ValueError with raise_on_failure=True
    • docs/a b#1?.md and %2e%2e/x are sent to .../contents/docs/a%20b%231%3F.md and .../contents/%252e%252e/x. requests.Request(...).prepare().url leaves both unchanged, so the URL that actually goes out still points into the repo's contents/
    • undo with an invalid branch sends no request
    • GitHubRepoViewer: traversal is rejected, ref goes through params, and "" and "docs/" produce the same URLs as before
    • GitHubPRCreator: feature/x works and ../x is rejected before any request
    • owner/repo: ../evil, owner/.., a/b/c and . are rejected in GitHubFileEditor and GitHubRepoViewer, and .. and . in issue URLs are rejected by all four parsers
    • table tests for the three helpers

Notes for the reviewer

  • Two existing tests changed: test_run_file and test_run_directory in test_repo_viewer.py asserted ?ref=main in the URL string. They now assert params={"ref": "main"}. test_run_sends_encoded_path_and_ref checks that the URL sent to GitHub is the same as before.
  • Invalid repo in GitHubFileEditor.run() raises ValueError regardless of raise_on_failure, like a malformed repo or an unknown command already did. The same goes for _create_branch() and _create_commit() in GitHubPRCreator, which build the URL before their try. Neither is called from run().
  • Catching ValueError in the GitHubFileEditor handlers also covers UnicodeDecodeError, so editing a binary file with raise_on_failure=False now returns "Error: ..." instead of raising.

Checklist

@bogdankostic
bogdankostic requested a review from a team as a code owner October 21, 2026 13:19
@github-actions github-actions Bot added integration:github type:documentation Improvements or additions to documentation labels Oct 21, 2026
@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report (github)

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  integrations/github/src/haystack_integrations/components/connectors/github
  file_editor.py 210
  issue_commenter.py
  issue_viewer.py
  pr_creator.py 195
  repo_forker.py
  repo_viewer.py
  utils.py
Project Total  

This report was generated by python-coverage-comment-action

@bogdankostic
bogdankostic marked this pull request as draft October 21, 2026 13:28
@bogdankostic
bogdankostic marked this pull request as ready for review October 21, 2026 13:28
@bogdankostic
bogdankostic requested review from a team and anakin87 and removed request for a team October 21, 2026 13:50

@anakin87 anakin87 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I left two minor comments

Feel free to apply (or discard) them and merge

@bogdankostic
bogdankostic merged commit d771795 into main Nov 2, 2026
17 checks passed
@bogdankostic
bogdankostic deleted the fix/github-url-path-traversal branch November 2, 2026 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integration:github type:documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants