Skip to content

fix(sitemap): Skip URLs that cannot be parsed - #2576

Open
shaurya703 wants to merge 1 commit into
apify:masterfrom
shaurya703:fix/sitemap-unparsable-url
Open

shaurya703 wants to merge 1 commit into
apify:masterfrom
shaurya703:fix/sitemap-unparsable-url

Conversation

@shaurya703

Copy link
Copy Markdown

Description

One URL that yarl cannot parse makes a sitemap lose every entry after it.

<urlset>
  <url><loc>http://example.com/a</loc></url>
  <url><loc>http://example.com:99999/</loc></url>
  <url><loc>http://example.com/b</loc></url>
</urlset>
  • parse_sitemap() on a URL source yields a, hits the bad entry, and treats the error as a fetch failure. It fetches the sitemap 3 times, yields a 3 times, never yields b, and raises ValueError: Port out of range 0-65535.
  • Sitemap.from_xml_string(..., sitemap_url=...) returns only a.
  • RobotsTxtFile.get_sitemaps() raises the same ValueError when a Sitemap: line holds such a URL.

The cause is filter_url(). It parses the target URL without handling ValueError, 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

  • N/A

Testing

  • test_filter_url_rejects_unparsable_target covers the three kinds of URL under every enqueue strategy.
  • test_unparsable_url_does_not_drop_rest_of_sitemap checks that the other URLs are kept and the sitemap is fetched once.
  • test_get_sitemaps_skips_unparsable_url covers robots.txt.
  • These tests fail without the change in urls.py. With it, the full unit suite passes, and so do tests/integration/_utils and tests/integration/request_loaders. I did not run the other integration tests or the end-to-end tests.

Checklist

  • CI passed

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.
@github-actions github-actions Bot added the t-tooling Issues with this label are in the ownership of the tooling team. label Dec 3, 2026

@Mantisus Mantisus left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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'])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could you wrap these cases in pytest.param(..., id=...) too?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

t-tooling Issues with this label are in the ownership of the tooling team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants