Skip to content

fix: Apply robots.txt rules to URLs with the default port - #2533

Merged
vdusek merged 3 commits into
apify:masterfrom
shlokbam:fix/robots-default-port
Nov 27, 2026
Merged

vdusek merged 3 commits into
apify:masterfrom
shlokbam:fix/robots-default-port

Conversation

@shlokbam

Copy link
Copy Markdown
Contributor

Description

  • RobotsTxtFile.is_allowed returns True when the URL's origin is not equal to the robots.txt origin. yarl's URL equality treats https://example.com:443 and https://example.com as different, while BasicCrawler caches robots.txt files under the string form of the origin, which drops the default port. So a URL that spelled out the default port skipped the rules: with Disallow: /private/, https://example.com:443/private/page was allowed.
  • The origins are now compared by their string form, the same key the crawler caches robots.txt files under. A non-default port is still a different origin.
  • This touches the imports at the top of tests/unit/_utils/test_robots.py, like fix: Keep robots.txt rules when the file is not valid UTF-8 #2531, so a small rebase may be needed after one of them is merged.

Issues

Testing

  • New tests in tests/unit/_utils/test_robots.py: the default port in the URL (https and http) and in the robots.txt URL is disallowed (3 cases, fail on master and pass with the fix), and a non-default port (:8443) is still treated as another origin (passes on both).
  • pytest tests/unit/_utils/test_robots.py test_urls.py test_requests.py test_sitemap.py: 113 passed. pytest tests/unit/crawlers/_basic -k robots: 8 passed.
  • Checked that str(URL(...).origin()) drops the default port on yarl 1.18.0, the lowest version in pyproject.toml.
  • ruff check, ruff format --check (0.17.6) and ty check (0.0.109) pass on the changed files.

Checklist

  • CI passed

`RobotsTxtFile.is_allowed` allowed any URL whose origin is not equal to the robots.txt origin. yarl's `URL` equality treats `https://example.com:443` and `https://example.com` as different origins, while `BasicCrawler` caches robots.txt files under the string form of the origin, which drops the default port. So with `Disallow: /private/`, `https://example.com:443/private/page` was allowed. Compare the string forms of the origins instead.
@github-actions github-actions Bot added the t-tooling Issues with this label are in the ownership of the tooling team. label Nov 26, 2026
@vdusek vdusek changed the title fix: apply robots.txt rules to URLs that spell out the default port fix: Apply robots.txt rules to URLs with an explicit default port Nov 27, 2026
@vdusek vdusek changed the title fix: Apply robots.txt rules to URLs with an explicit default port fix: Apply robots.txt rules to URLs with the default port Nov 27, 2026

@vdusek vdusek 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.

LGTM

# Conflicts:
#	tests/unit/_utils/test_robots.py
@vdusek
vdusek merged commit 4d50067 into apify:master Nov 27, 2026
45 checks passed
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

2 participants