Skip to content

fix: Keep robots.txt rules when the file is not valid UTF-8 - #2531

Merged
vdusek merged 2 commits into
apify:masterfrom
shlokbam:fix/robots-non-utf8
Nov 27, 2026
Merged

vdusek merged 2 commits into
apify:masterfrom
shlokbam:fix/robots-non-utf8

Conversation

@shlokbam

@shlokbam shlokbam commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Description

  • RobotsTxtFile.load decoded the body as strict UTF-8, so one byte that is not UTF-8 (such as a Latin-1 comment) raised UnicodeDecodeError. The crawler then retried the fetch and fell back to allowing every URL of the site, ignoring all its rules.
  • The body is now decoded with 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. With errors='ignore', Disallow: /\xe9 would become Disallow: / and block the whole site. A leading BOM is still removed by utf-8-sig (fix: ignore a UTF-8 byte order mark in robots.txt #2351).

Issues

Testing

  • New test test_load_robots_txt_with_non_utf8_bytes in tests/unit/_utils/test_robots.py, with a Mock HTTP client returning a robots.txt with a Latin-1 byte. The byte_in_comment case fails on master (UnicodeDecodeError). The byte_in_rule_path case fails with errors='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) and ty check (0.0.109) pass on the changed files.

Checklist

  • CI passed

`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.
@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: keep robots.txt rules when the file has bytes that are not UTF-8 fix: Keep robots.txt rules when the file has invalid UTF-8 bytes Nov 27, 2026
@vdusek vdusek changed the title fix: Keep robots.txt rules when the file has invalid UTF-8 bytes fix: Keep robots.txt rules when the file is not valid UTF-8 Nov 27, 2026
@vdusek
vdusek merged commit fb26519 into apify:master Nov 27, 2026
46 checks passed
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]>
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.

robots.txt with a byte that is not UTF-8 is ignored completely, so every URL is allowed

2 participants