Skip to content

Invalid User-agent tokens cause subsequent rules to bleed into the previous group #223

Description

@MaxDall

Hey! Thanks so much for the great work. Unfortunately I run into somewhat, at least for me, unexpected behaviour.

Description

When a User-agent: line contains a token that doesn't match the expected pattern (e.g. one starting with a digit like 5emeRue), robotspy silently drops the User-agent: line but continues applying the subsequent Disallow: rules to the previously open group. This causes silent misattribution of rules.

Reproduction

import robots

parser = robots.RobotsParser.from_string("""
User-agent: *
Disallow: /internal

User-agent: 5emeRue
Disallow: /
""")

print(parser.can_fetch("*", "/foo"))  # Expected: True — Actual: False

Root cause

In parser.py, RE_AGENT and RE_PRODUCT only match tokens consisting of [a-zA-Z_-], which excludes digits. This is consistent with RFC 9309, which defines a product token as letters, hyphens, and underscores only.

However, when the regex fails to match, the User-agent: line is silently ignored while the parser leaves the previous group context open. The Disallow: rules that follow are then attributed to that previous group instead of being discarded.

Expected behavior

Regardless of whether the token is valid per RFC 9309, encountering a User-agent: line should always reset the current group context. The subsequent rules should either be grouped under an ignored/discarded group, or skipped entirely — but never silently merged into the preceding group.

Suggested fix

In parse_tokens(), when a User-agent: line is detected but the token fails validation, the parser should still close the current group and open a new (discarded) one, rather than leaving the previous group context open.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions