Skip to content

urllib.robotparser.RobotFileParser.can_fetch crashes on a malformed URL #153795

Description

@tonghuaroot

Bug description:

urllib.robotparser.RobotFileParser.can_fetch is documented to return a bool,
but it passes url straight to urllib.parse._urlsplit, so a malformed URL
(an unterminated IPv6 authority) raises a bare ValueError instead of a
result:

import urllib.robotparser
p = urllib.robotparser.RobotFileParser()
p.parse(["User-agent: *", "Disallow: /"])
p.can_fetch("*", "http://[::1")   # ValueError: Invalid IPv6 URL

The url argument is crawl-frontier-controlled (it comes from parsed pages).

Proposed behavior: a URL that cannot be parsed matches no rule, so can_fetch
returns True (RFC 9309 default that access is not restricted when no rule
applies), rather than raising or being conservatively False.

This is a sibling of #153404 (robotparser raising ValueError on a non-decimal
digit in Crawl-delay / Request-rate); the sink there is the robots.txt
content, here it is the url argument.

CPython versions tested on:

3.13, 3.14, 3.15

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions