Debian Codegraph @

src/cherrypy_foundation/tools/tests/test_ratelimit.py

raw
1 # CherryPy
2 # Copyright (C) 2026 IKUS Software
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17 import tempfile
18
19 import cherrypy
20 from cherrypy.test import helper
21
22 from .. import ratelimit # noqa
23
24
25 class Root:
26
27 @cherrypy.expose
28 def index(self):
29 return "OK"
30
31 @cherrypy.expose
32 @cherrypy.tools.ratelimit(on=True, methods=['POST'])
33 def login(self):
34 return "login"
35
36
37 class RateLimitTest(helper.CPWebCase):
38 interactive = False
39 rate_limit = 5
40 rate_limit_dir = None
41
42 def setUp(self):
43 cherrypy.tools.ratelimit.reset()
44 return super().setUp()
45
46 def tearDown(self):
47 cherrypy.tools.ratelimit.reset()
48 return super().tearDown()
49
50 @classmethod
51 def setup_server(cls):
52 rate_limit_storage_class = None
53 if cls.rate_limit_dir:
54 rate_limit_storage_class = ratelimit.FileRateLimit
55 cherrypy.config.update(
56 {
57 'tools.ratelimit.debug': True,
58 'tools.ratelimit.delay': 3600,
59 'tools.ratelimit.limit': cls.rate_limit,
60 'tools.ratelimit.storage_class': rate_limit_storage_class,
61 'tools.ratelimit.storage_path': cls.rate_limit_dir,
62 }
63 )
64 cherrypy.tree.mount(Root(), '/')
65
66 def test_ratelimit(self):
67 # Given a endpoint with ratelimit enabled
68 # When requesting multiple time the page
69 for i in range(0, 5):
70 self.getPage('/login', method='POST')
71 self.assertStatus(200)
72 # Then a 429 error (too many request) is return
73 self.getPage('/login', method='POST')
74 self.assertStatus(429)
75
76 def test_ratelimit_forwarded_for(self):
77 # Given a endpoint with ratelimit enabled
78 # When requesting multiple time the login page with different `X-Forwarded-For`
79 for i in range(0, 5):
80 self.getPage(
81 '/login',
82 headers=[('X-Forwarded-For', '127.0.0.%s' % i)],
83 method='POST',
84 )
85 self.assertStatus(200)
86 # Then original IP get blocked
87 self.getPage(
88 '/login',
89 headers=[('X-Forwarded-For', '127.0.0.%s' % i)],
90 method='POST',
91 )
92 self.assertHeaderItemValue('X-Ratelimit-Limit', '5')
93 self.assertHeaderItemValue('X-Ratelimit-Remaining', '0')
94 self.assertHeader('X-Ratelimit-Reset')
95 self.assertStatus(429)
96
97
98 class FileStorageRateLimitTest(RateLimitTest):
99
100 @classmethod
101 def setup_class(cls):
102 cls.tempdir = tempfile.TemporaryDirectory(prefix='cherrypy-foundation-', suffix='-ratelimit-test')
103 cls.rate_limit_dir = cls.tempdir.name
104 super().setup_class()
105
106 @classmethod
107 def teardown_class(cls):
108 cls.tempdir.cleanup()
109 super().teardown_class()