Fix safe_url_string to preserve IPv6 brackets in netloc#253
Merged
Gallaecio merged 1 commit intoscrapy:masterfrom Feb 19, 2026
Merged
Fix safe_url_string to preserve IPv6 brackets in netloc#253Gallaecio merged 1 commit intoscrapy:masterfrom
Gallaecio merged 1 commit intoscrapy:masterfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #253 +/- ##
=======================================
Coverage 97.96% 97.97%
=======================================
Files 9 9
Lines 491 493 +2
Branches 83 84 +1
=======================================
+ Hits 481 483 +2
Misses 6 6
Partials 4 4
🚀 New features to boost your workflow:
|
Gallaecio
approved these changes
Feb 19, 2026
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.
Fixes #193
Problem
safe_url_stringmanually rebuilds thenetloccomponent from the parts returned byurlsplit(). Thehostnameattribute always strips square brackets from IPv6 addresses (e.g.[2402:4e00:40:40::2:3b6]becomes2402:4e00:40:40::2:3b6). The brackets were not restored when rebuildingnetloc_bytes, so the reconstructed URL was malformed.On subsequent calls (e.g. inside Scrapy middlewares reconstructing a
Requestfrom the stored URL),urlsplit()on the bracket-less URL parsed the first IPv6 segment as the host and the remainder as a port, raisingValueError: Port could not be cast to integer value.Fix
Detect IPv6 addresses by checking for
:in the hostname and restore the brackets before appending tonetloc_bytes. IPv4 addresses and DNS hostnames never contain:, so the check is unambiguous.Tests
http://[2a01:5cc0:1:2::4]fromKNOWN_SAFE_URL_STRING_URL_ISSUES(the xfail for this issue) since it now passes.::1with and without a port and path.