-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: URL-encode access tokens to handle special characters #399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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)
Summary of ChangesHello @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
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this 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.
| 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, '', '', '')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
| 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, '', '', '')) |
Fixes authentication failures when using access tokens containing special characters (e.g., underscores, hyphens) for private repositories.
Problem:
Solution:
Tested with: