Skip to content

fix: ignore a UTF-8 byte order mark in robots.txt - #2351

Merged
janbuchar merged 1 commit into
apify:masterfrom
RaphaelFakhri:fix/robots-txt-utf8-bom
Oct 26, 2026
Merged

janbuchar merged 1 commit into
apify:masterfrom
RaphaelFakhri:fix/robots-txt-utf8-bom

Conversation

@RaphaelFakhri

Copy link
Copy Markdown
Contributor

Description

  • RobotsTxtFile.load decodes the response body with utf-8, which keeps a leading UTF-8 byte order mark (BOM) as \ufeff. Protego then does not recognize the first line (\ufeffUser-agent: *) as a User-agent directive, so the rules of the first group are ignored and is_allowed returns True for disallowed paths. With respect_robots_txt_file=True, the crawler fetches pages the site disallows.
  • The body is now decoded with utf-8-sig, which drops the BOM if present and is otherwise identical to utf-8.
  • Adds a regression test that loads a robots.txt with a BOM through the test server.

Issues

Testing

  • New test test_load_robots_txt_with_utf8_bom in tests/unit/_utils/test_robots.py. It fails on master (assert not robots.is_allowed(...) gets True) 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, and ty check pass on the changed files.

Checklist

  • CI passed

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

LGTM

@apify-approval-mirror apify-approval-mirror Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Mirroring approval from @Mantisus.

@janbuchar
janbuchar merged commit 3e6ee66 into apify:master Oct 26, 2026
40 of 41 checks passed
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]>
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 rules are ignored when the file starts with a UTF-8 byte order mark

3 participants