Repository navigation
fix: ignore a UTF-8 byte order mark in robots.txt - #2351
Merged
Merged
Conversation
This was referenced Nov 23, 2026
1 task
vdusek
added a commit
that referenced
this pull request
Nov 27, 2026
### 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` (#2351). ### Issues - Closes: #2530 ### 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. --------- 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.loaddecodes the response body withutf-8, which keeps a leading UTF-8 byte order mark (BOM) as\ufeff. Protego then does not recognize the first line (\ufeffUser-agent: *) as aUser-agentdirective, so the rules of the first group are ignored andis_allowedreturnsTruefor disallowed paths. Withrespect_robots_txt_file=True, the crawler fetches pages the site disallows.utf-8-sig, which drops the BOM if present and is otherwise identical toutf-8.Issues
Testing
test_load_robots_txt_with_utf8_bomintests/unit/_utils/test_robots.py. It fails onmaster(assert not robots.is_allowed(...)getsTrue) and passes with the fix, for all HTTP client parametrizations.uv run pytest tests/unit/_utils/test_robots.py: 20 passed.ruff check,ruff format --check, andty checkpass on the changed files.Checklist