Skip to content

Conversation

@Ishang22
Copy link

Fixes authentication failures when using access tokens containing special characters (e.g., underscores, hyphens) for private repositories.

Problem:

  • Access tokens with special characters were not being URL-encoded before being embedded in git clone URLs
  • This caused 'URL rejected: Malformed input to a URL function' errors
  • Affected GitHub, GitLab, and Bitbucket private repository access

Solution:

  • URL-encode tokens using quote(access_token, safe='') before embedding in authentication URLs for all supported platforms (GitHub, GitLab, Bitbucket)
  • Also strips leading/trailing whitespace from repository URLs to prevent parsing issues

Tested with:

  • GitLab tokens containing underscores and hyphens
  • Custom GitLab instances (non-gitlab.com domains)

Fixes authentication failures when using access tokens containing special
characters (e.g., underscores, hyphens) for private repositories.

Problem:
- Access tokens with special characters were not being URL-encoded before
  being embedded in git clone URLs
- This caused 'URL rejected: Malformed input to a URL function' errors
- Affected GitHub, GitLab, and Bitbucket private repository access

Solution:
- URL-encode tokens using quote(access_token, safe='') before embedding
  in authentication URLs for all supported platforms (GitHub, GitLab, Bitbucket)
- Also strips leading/trailing whitespace from repository URLs to prevent
  parsing issues

Tested with:
- GitLab tokens containing underscores and hyphens
- Custom GitLab instances (non-gitlab.com domains)
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Ishang22, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses critical authentication failures experienced when using access tokens containing special characters for private repositories across GitHub, GitLab, and Bitbucket. By ensuring that access tokens are properly URL-encoded before being used in clone URLs, it guarantees successful authentication. Furthermore, the change enhances the robustness of repository handling by trimming whitespace from provided repository URLs, preventing parsing errors.

Highlights

  • Access Token URL Encoding: Implemented URL encoding for access tokens using urllib.parse.quote before embedding them in clone URLs for GitHub, GitLab, and Bitbucket. This resolves authentication failures when tokens contain special characters.
  • Repository URL Whitespace Handling: Added a step to strip leading and trailing whitespace from repository URLs to prevent potential parsing issues during repository creation.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly addresses authentication failures with private repositories by URL-encoding access tokens that may contain special characters. The implementation is applied across GitHub, GitLab, and Bitbucket providers. Additionally, stripping whitespace from repository URLs is a good defensive measure against potential parsing issues. I've suggested a small refactoring to reduce code duplication in the URL construction logic, which will improve maintainability.

Comment on lines 108 to +117
if repo_type == "github":
# Format: https://{token}@{domain}/owner/repo.git
# Works for both github.com and enterprise GitHub domains
clone_url = urlunparse((parsed.scheme, f"{access_token}@{parsed.netloc}", parsed.path, '', '', ''))
clone_url = urlunparse((parsed.scheme, f"{encoded_token}@{parsed.netloc}", parsed.path, '', '', ''))
elif repo_type == "gitlab":
# Format: https://oauth2:{token}@gitlab.com/owner/repo.git
clone_url = urlunparse((parsed.scheme, f"oauth2:{access_token}@{parsed.netloc}", parsed.path, '', '', ''))
clone_url = urlunparse((parsed.scheme, f"oauth2:{encoded_token}@{parsed.netloc}", parsed.path, '', '', ''))
elif repo_type == "bitbucket":
# Format: https://x-token-auth:{token}@bitbucket.org/owner/repo.git
clone_url = urlunparse((parsed.scheme, f"x-token-auth:{access_token}@{parsed.netloc}", parsed.path, '', '', ''))
clone_url = urlunparse((parsed.scheme, f"x-token-auth:{encoded_token}@{parsed.netloc}", parsed.path, '', '', ''))
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The logic for constructing the clone URL is repeated for each repository type. This can be refactored to reduce code duplication and improve maintainability by using a dictionary to map repository types to their authentication prefixes. This makes the code cleaner and easier to extend with new repository types in the future.

Suggested change
if repo_type == "github":
# Format: https://{token}@{domain}/owner/repo.git
# Works for both github.com and enterprise GitHub domains
clone_url = urlunparse((parsed.scheme, f"{access_token}@{parsed.netloc}", parsed.path, '', '', ''))
clone_url = urlunparse((parsed.scheme, f"{encoded_token}@{parsed.netloc}", parsed.path, '', '', ''))
elif repo_type == "gitlab":
# Format: https://oauth2:{token}@gitlab.com/owner/repo.git
clone_url = urlunparse((parsed.scheme, f"oauth2:{access_token}@{parsed.netloc}", parsed.path, '', '', ''))
clone_url = urlunparse((parsed.scheme, f"oauth2:{encoded_token}@{parsed.netloc}", parsed.path, '', '', ''))
elif repo_type == "bitbucket":
# Format: https://x-token-auth:{token}@bitbucket.org/owner/repo.git
clone_url = urlunparse((parsed.scheme, f"x-token-auth:{access_token}@{parsed.netloc}", parsed.path, '', '', ''))
clone_url = urlunparse((parsed.scheme, f"x-token-auth:{encoded_token}@{parsed.netloc}", parsed.path, '', '', ''))
auth_user_map = {
"github": "",
"gitlab": "oauth2:",
"bitbucket": "x-token-auth:",
}
if repo_type in auth_user_map:
user_prefix = auth_user_map[repo_type]
clone_url = urlunparse((parsed.scheme, f"{user_prefix}{encoded_token}@{parsed.netloc}", parsed.path, '', '', ''))

@Ishang22 Ishang22 closed this by deleting the head repository Nov 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant