Skip to content

Commit 597ec2d

Browse files
authored
gh-153404: Silently ignore non-decimal digits in robots.txt Crawl-delay and Request-rate (GH-153405)
str.isdigit() returned True for non-decimal Unicode digits such as U+00B2 SUPERSCRIPT TWO, which int() then rejected with ValueError.
1 parent f62050d commit 597ec2d

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

Lib/test/test_robotparser.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ def test_request_rate(self):
188188
parsed_request_rate.seconds,
189189
self.request_rate.seconds
190190
)
191+
else:
192+
self.assertIsNone(parsed_request_rate)
191193

192194

193195
class EmptyFileTest(BaseRequestRateTest, unittest.TestCase):
@@ -246,6 +248,32 @@ class InvalidCrawlDelayTest(BaseRobotTest, unittest.TestCase):
246248
bad = []
247249

248250

251+
class NonDecimalDigitsTest(BaseRequestRateTest, unittest.TestCase):
252+
# Non-decimal Unicode digits pass str.isdigit() but int() rejects
253+
# them, so the directive must be silently ignored, not raise.
254+
robots_txt = """\
255+
User-Agent: *
256+
Disallow: /tmp/
257+
Crawl-delay: ²
258+
Request-rate: ²/5
259+
"""
260+
good = ['/foo.html']
261+
bad = ['/tmp/']
262+
crawl_delay = None
263+
request_rate = None
264+
265+
266+
class NonDecimalDenominatorTest(BaseRequestRateTest, unittest.TestCase):
267+
robots_txt = """\
268+
User-agent: *
269+
Disallow: /tmp/
270+
Request-rate: 5/²
271+
"""
272+
good = ['/foo.html']
273+
request_rate = None
274+
bad = ['/tmp/']
275+
276+
249277
class AnotherInvalidRequestRateTest(BaseRobotTest, unittest.TestCase):
250278
# also test that Allow and Diasallow works well with each other
251279
robots_txt = """\

Lib/urllib/robotparser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,15 @@ def parse(self, lines):
148148
# before trying to convert to int we need to make
149149
# sure that robots.txt has valid syntax otherwise
150150
# it will crash
151-
if line[1].strip().isdigit():
151+
if line[1].strip().isdecimal():
152152
entry.delay = int(line[1])
153153
state = 2
154154
elif line[0] == "request-rate":
155155
if state != 0:
156156
numbers = line[1].split('/')
157157
# check if all values are sane
158-
if (len(numbers) == 2 and numbers[0].strip().isdigit()
159-
and numbers[1].strip().isdigit()):
158+
if (len(numbers) == 2 and numbers[0].strip().isdecimal()
159+
and numbers[1].strip().isdecimal()):
160160
entry.req_rate = RequestRate(int(numbers[0]), int(numbers[1]))
161161
state = 2
162162
elif line[0] == "sitemap":
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:class:`urllib.robotparser.RobotFileParser` now silently ignores a
2+
``Crawl-delay`` or ``Request-rate`` value written with non-decimal digits
3+
(such as ``U+00B2 SUPERSCRIPT TWO``) instead of raising :exc:`ValueError`
4+
and aborting the parse of the whole ``robots.txt`` file.

0 commit comments

Comments
 (0)