Repository navigation
fix: Keep robots.txt rules when the file is not valid UTF-8 - #2531
Merged
Merged
Conversation
`RobotsTxtFile.load` decoded the body as strict UTF-8, so one byte that is not UTF-8 (for example a comment saved as Latin-1 or Windows-1252) raised `UnicodeDecodeError`. The crawler then retried the fetch and fell back to allowing every URL of the site, ignoring all its rules. Drop the bytes that do not decode, as Scrapy does with the same parser.
1 task
vdusek
approved these changes
Nov 27, 2026
robots.txt rules when the file has invalid UTF-8 bytes
robots.txt rules when the file has invalid UTF-8 bytesrobots.txt rules when the file is not valid UTF-8
vdusek
added a commit
that referenced
this pull request
Nov 27, 2026
### 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 #2531, so a small rebase may be needed after one of them is merged. ### Issues - Closes: #2532 ### 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. --------- Co-authored-by: Vlada Dusek <[email protected]>
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
RobotsTxtFile.loaddecoded the body as strict UTF-8, so one byte that is not UTF-8 (such as a Latin-1 comment) raisedUnicodeDecodeError. The crawler then retried the fetch and fell back to allowing every URL of the site, ignoring all its rules.errors='replace', so a byte that does not decode becomes U+FFFD. Protego decodes such a byte in a URL to U+FFFD too, so a rule path that holds it matches the URL it was written for. Witherrors='ignore',Disallow: /\xe9would becomeDisallow: /and block the whole site. A leading BOM is still removed byutf-8-sig(fix: ignore a UTF-8 byte order mark in robots.txt #2351).Issues
Testing
test_load_robots_txt_with_non_utf8_bytesintests/unit/_utils/test_robots.py, with aMockHTTP client returning a robots.txt with a Latin-1 byte. Thebyte_in_commentcase fails onmaster(UnicodeDecodeError). Thebyte_in_rule_pathcase fails witherrors='ignore'.pytest tests/unit/_utils/test_robots.py: 8 passed.pytest tests/unit/crawlers/_basic -k robots: 8 passed.ruff check,ruff format --check(0.17.6) andty check(0.0.109) pass on the changed files.Checklist