Fix IndexError in URL.replace() when netloc is empty#3154
Closed
bysiber wants to merge 1 commit intoKludex:mainfrom
Closed
Fix IndexError in URL.replace() when netloc is empty#3154bysiber wants to merge 1 commit intoKludex:mainfrom
bysiber wants to merge 1 commit intoKludex:mainfrom
Conversation
When calling replace() with username, password, or port on a URL that
has no network location (empty netloc), the code extracts hostname
from the netloc via rpartition('@'). If the netloc is empty, hostname
becomes an empty string, and the subsequent hostname[-1] indexing
raises an IndexError.
Adding a truthiness check before indexing prevents the crash and
allows the empty hostname to pass through to the URL reconstruction.
Owner
|
Test |
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.
Summary
URL.replace()raises anIndexErrorwhen called withusername,password, orportarguments on a URL that has no network location (e.g. a relative path or empty URL).Reproduction
Cause
When
hostnameis not provided in the kwargs, the code tries to extract it from the currentnetloc. If the netloc is empty (as it is for path-only URLs),rpartition("@")returns an empty string for hostname, and the subsequenthostname[-1]indexing raisesIndexError.Fix
Added a truthiness check (
if hostname and hostname[-1] != "]") to guard against indexing into an empty string.