Repository navigation
fix(sitemap): Skip URLs that cannot be parsed - #2576
Open
shaurya703 wants to merge 1 commit into
Open
shaurya703 wants to merge 1 commit into
shaurya703 wants to merge 1 commit into
Conversation
A sitemap or robots.txt entry with a URL that `yarl` cannot parse made `filter_url` raise `ValueError`. The error ended the processing of the whole file, so all the entries after it were lost. `filter_url` now rejects such a target URL with a reason, and the callers skip and log it like any other rejected URL.
Mantisus
suggested changes
Dec 3, 2026
Mantisus
left a comment
Collaborator
There was a problem hiding this comment.
Hi @shaurya703
Thanks for contributing! Please update one small thing below.
| assert ok is expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('strategy', ['all', 'same-hostname', 'same-domain', 'same-origin']) |
Collaborator
There was a problem hiding this comment.
Could you wrap these cases in pytest.param(..., id=...) too?
This branch has not been deployed
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.
Description
One URL that
yarlcannot parse makes a sitemap lose every entry after it.parse_sitemap()on a URL source yieldsa, hits the bad entry, and treats the error as a fetch failure. It fetches the sitemap 3 times, yieldsa3 times, never yieldsb, and raisesValueError: Port out of range 0-65535.Sitemap.from_xml_string(..., sitemap_url=...)returns onlya.RobotsTxtFile.get_sitemaps()raises the sameValueErrorwhen aSitemap:line holds such a URL.The cause is
filter_url(). It parses the target URL without handlingValueError, so the exception goes up through the parsing loop. The same happens for an unclosed IPv6 bracket (http://[::1) and an invalid IDNA host (http://xn--a.com/).filter_url()now returns(False, 'cannot be parsed (...)')for such a target, so the callers skip and log it like any other rejected URL. It also reads the host up front, because an invalid IDNA host only raises at that point. #2444 made the same change for links extracted from a page.Issues
Testing
test_filter_url_rejects_unparsable_targetcovers the three kinds of URL under every enqueue strategy.test_unparsable_url_does_not_drop_rest_of_sitemapchecks that the other URLs are kept and the sitemap is fetched once.test_get_sitemaps_skips_unparsable_urlcoversrobots.txt.urls.py. With it, the full unit suite passes, and so dotests/integration/_utilsandtests/integration/request_loaders. I did not run the other integration tests or the end-to-end tests.Checklist