Skip to content

Commit 15034a2

Browse files
committed
firewall_lists: Add a check on self.allowed_ips, fix test cases
1 parent 33a1855 commit 15034a2

3 files changed

Lines changed: 126 additions & 76 deletions

File tree

aikido_zen/sources/functions/request_handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from aikido_zen.helpers.logging import logger
77
from aikido_zen.thread.thread_cache import get_cache
88
from .ip_allowed_to_access_route import ip_allowed_to_access_route
9-
from ...background_process import get_comms
9+
import aikido_zen.background_process.comms as c
1010

1111

1212
def request_handler(stage, status_code=0):
@@ -50,7 +50,7 @@ def pre_response():
5050
return message, 403
5151

5252
# Do a check on firewall lists, this happens in background because of the heavy data.
53-
comms = get_comms()
53+
comms = c.get_comms()
5454
check_fw_lists_res = comms.send_data_to_bg_process(
5555
action="CHECK_FIREWALL_LISTS",
5656
obj={

aikido_zen/sources/functions/request_handler_test.py

Lines changed: 121 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
from unittest.mock import patch, MagicMock
33
from aikido_zen.thread.thread_cache import get_cache, ThreadCache
44
from .request_handler import request_handler
5+
from ...background_process.commands import process_check_firewall_lists
56
from ...background_process.service_config import ServiceConfig
67
from ...context import Context, current_context
78
from ...helpers.headers import Headers
9+
from ...storage.firewall_lists import FirewallLists
810

911

1012
@pytest.fixture
@@ -31,6 +33,22 @@ def run_around_tests():
3133
current_context.set(None)
3234

3335

36+
class MyMockComms:
37+
def __init__(self):
38+
self.firewall_lists = FirewallLists()
39+
self.conn_manager = MagicMock()
40+
self.conn_manager.firewall_lists = self.firewall_lists
41+
42+
def send_data_to_bg_process(self, action, obj, receive=False, timeout_in_sec=0.1):
43+
if action != "CHECK_FIREWALL_LISTS":
44+
return {"success": False}
45+
res = process_check_firewall_lists(self.conn_manager, obj, None, None)
46+
return {
47+
"success": True,
48+
"data": res,
49+
}
50+
51+
3452
def test_post_response_useful_route(mock_context):
3553
"""Test post_response when the route is useful."""
3654

@@ -127,7 +145,7 @@ def set_context(remote_address, user_agent=""):
127145
).set_as_current_context()
128146

129147

130-
def create_service_config(blocked_ips=None):
148+
def create_service_config():
131149
config = ServiceConfig(
132150
endpoints=[
133151
{
@@ -142,19 +160,35 @@ def create_service_config(blocked_ips=None):
142160
bypassed_ips=[],
143161
received_any_stats=False,
144162
)
145-
if blocked_ips:
146-
config.set_blocked_ips(blocked_ips)
147163
get_cache().config = config
148164
return config
149165

150166

151-
def test_blocked_ip():
167+
def patch_firewall_lists(func):
168+
def wrapper(*args, **kwargs):
169+
with patch("aikido_zen.background_process.comms.get_comms") as mock_comms:
170+
comms = MyMockComms()
171+
mock_comms.return_value = comms
172+
173+
return func(*args, firewall_lists=comms.firewall_lists, **kwargs)
174+
175+
return wrapper
176+
177+
178+
@patch_firewall_lists
179+
def test_blocked_ip(firewall_lists):
152180
# Arrange
181+
firewall_lists.set_blocked_ips(
182+
[
183+
{
184+
"source": "test",
185+
"description": "Blocked for testing",
186+
"ips": ["192.168.1.1"],
187+
}
188+
]
189+
)
153190
set_context("192.168.1.1")
154-
blocked_ips = [
155-
{"source": "test", "description": "Blocked for testing", "ips": ["192.168.1.1"]}
156-
]
157-
config = create_service_config(blocked_ips)
191+
config = create_service_config()
158192
config.endpoints[0]["allowedIPAddresses"] = [] # Clear allowed ips for endpoint
159193

160194
# Act
@@ -167,13 +201,19 @@ def test_blocked_ip():
167201
)
168202

169203

170-
def test_allowed_ip():
204+
@patch_firewall_lists
205+
def test_allowed_ip(firewall_lists):
171206
# Arrange
172207
set_context("1.1.1.1")
173-
blocked_ips = [
174-
{"source": "test", "description": "Blocked for testing", "ips": ["192.168.1.1"]}
175-
]
176-
create_service_config(blocked_ips)
208+
firewall_lists.set_blocked_ips(
209+
[
210+
{
211+
"source": "test",
212+
"description": "Blocked for testing",
213+
"ips": ["192.168.1.1"],
214+
}
215+
]
216+
)
177217

178218
# Act
179219
result = request_handler("pre_response")
@@ -193,13 +233,15 @@ def test_invalid_context():
193233
assert result is None
194234

195235

196-
def test_not_allowed_ip():
236+
@patch_firewall_lists
237+
def test_not_allowed_ip(firewall_lists):
197238
# Arrange
198239
set_context("192.168.1.3")
240+
create_service_config()
199241
blocked_ips = [
200242
{"source": "test", "description": "Blocked for testing", "ips": ["192.168.1.1"]}
201243
]
202-
create_service_config(blocked_ips)
244+
firewall_lists.set_blocked_ips(blocked_ips)
203245

204246
# Act
205247
result = request_handler("pre_response")
@@ -211,29 +253,15 @@ def test_not_allowed_ip():
211253
)
212254

213255

214-
def test_bypassed_ip():
215-
# Arrange
216-
set_context("1.1.1.1") # This IP is in the allowed list
217-
blocked_ips = [
218-
{"source": "test", "description": "Blocked for testing", "ips": ["192.168.1.1"]}
219-
]
220-
config = create_service_config(blocked_ips)
221-
config.bypassed_ips.add("1.1.1.1")
222-
223-
# Act
224-
result = request_handler("pre_response")
225-
226-
# Assert
227-
assert result is None # Should be allowed since it's in the bypass list
228-
229-
230-
def test_ip_allowed_by_endpoint():
256+
@patch_firewall_lists
257+
def test_ip_allowed_by_endpoint(firewall_lists):
231258
# Arrange
232259
set_context("2.2.2.2") # This IP is in the allowed list
233260
blocked_ips = [
234261
{"source": "test", "description": "Blocked for testing", "ips": ["192.168.1.1"]}
235262
]
236-
config = create_service_config(blocked_ips)
263+
firewall_lists.set_blocked_ips(blocked_ips)
264+
create_service_config()
237265

238266
# Act
239267
result = request_handler("pre_response")
@@ -242,13 +270,15 @@ def test_ip_allowed_by_endpoint():
242270
assert result is None # Should be allowed since it's in the allowed list
243271

244272

245-
def test_ip_not_allowed_by_endpoint():
273+
@patch_firewall_lists
274+
def test_ip_not_allowed_by_endpoint(firewall_lists):
246275
# Arrange
247276
set_context("4.4.4.4") # Not in the allowed list
248277
blocked_ips = [
249278
{"source": "test", "description": "Blocked for testing", "ips": ["192.168.1.1"]}
250279
]
251-
config = create_service_config(blocked_ips)
280+
firewall_lists.set_blocked_ips(blocked_ips)
281+
config = create_service_config()
252282

253283
# Act
254284
result = request_handler("pre_response")
@@ -260,17 +290,19 @@ def test_ip_not_allowed_by_endpoint():
260290
)
261291

262292

263-
def test_first_checks_resource_blocked():
293+
@patch_firewall_lists
294+
def test_first_checks_resource_blocked(firewall_lists):
264295
# Arrange
265296
set_context("192.168.1.1") # This IP is blocked
297+
create_service_config()
266298
blocked_ips = [
267299
{
268300
"source": "test",
269301
"description": "Blocked for testing",
270302
"ips": ["192.168.1.1", "192.168.1.2"],
271303
}
272304
]
273-
create_service_config(blocked_ips)
305+
firewall_lists.set_blocked_ips(blocked_ips)
274306

275307
# Act
276308
result = request_handler("pre_response")
@@ -282,17 +314,19 @@ def test_first_checks_resource_blocked():
282314
)
283315

284316

285-
def test_allowed_for_endpoint_but_blocked():
317+
@patch_firewall_lists
318+
def test_allowed_for_endpoint_but_blocked(firewall_lists):
286319
# Arrange
287320
set_context("1.1.1.1") # This IP is blocked
321+
create_service_config()
288322
blocked_ips = [
289323
{
290324
"source": "test",
291325
"description": "Blocked for testing",
292326
"ips": ["192.168.1.1", "192.168.1.2", "1.1.1.0/24"],
293327
}
294328
]
295-
create_service_config(blocked_ips)
329+
firewall_lists.set_blocked_ips(blocked_ips)
296330

297331
# Act
298332
result = request_handler("pre_response")
@@ -304,18 +338,21 @@ def test_allowed_for_endpoint_but_blocked():
304338
)
305339

306340

307-
def test_allowed_for_endpoint_but_not_in_allowlist_and_blocked():
341+
@patch_firewall_lists
342+
def test_allowed_for_endpoint_but_not_in_allowlist_and_blocked(firewall_lists):
308343
# Arrange
309344
set_context("1.1.1.1") # This IP is blocked
310-
blocked_ips = [
311-
{
312-
"source": "test",
313-
"description": "Blocked for testing",
314-
"ips": ["192.168.1.1", "192.168.1.2", "1.1.1.0/24"],
315-
}
316-
]
317-
config = create_service_config(blocked_ips)
318-
config.set_allowed_ips(
345+
create_service_config()
346+
firewall_lists.set_blocked_ips(
347+
[
348+
{
349+
"source": "test",
350+
"description": "Blocked for testing",
351+
"ips": ["192.168.1.1", "192.168.1.2", "1.1.1.0/24"],
352+
}
353+
]
354+
)
355+
firewall_lists.set_allowed_ips(
319356
[
320357
{
321358
"source": "test",
@@ -335,11 +372,12 @@ def test_allowed_for_endpoint_but_not_in_allowlist_and_blocked():
335372
)
336373

337374

338-
def test_allowed_for_endpoint_but_not_in_allowlist_not_blocked():
375+
@patch_firewall_lists
376+
def test_allowed_for_endpoint_but_not_in_allowlist_not_blocked(firewall_lists):
339377
# Arrange
340378
set_context("1.1.1.1") # This IP is blocked
341-
config = create_service_config()
342-
config.set_allowed_ips(
379+
create_service_config()
380+
firewall_lists.set_allowed_ips(
343381
[
344382
{
345383
"source": "test",
@@ -359,11 +397,12 @@ def test_allowed_for_endpoint_but_not_in_allowlist_not_blocked():
359397
)
360398

361399

362-
def test_allowed_for_endpoint_and_in_allowlist():
400+
@patch_firewall_lists
401+
def test_allowed_for_endpoint_and_in_allowlist(firewall_lists):
363402
# Arrange
364403
set_context("1.1.1.1") # This IP is blocked
365-
config = create_service_config()
366-
config.set_allowed_ips(
404+
create_service_config()
405+
firewall_lists.set_allowed_ips(
367406
[
368407
{
369408
"source": "test",
@@ -380,18 +419,21 @@ def test_allowed_for_endpoint_and_in_allowlist():
380419
assert result is None
381420

382421

383-
def test_allowed_for_endpoint_in_allowlist_but_blocked():
422+
@patch_firewall_lists
423+
def test_allowed_for_endpoint_in_allowlist_but_blocked(firewall_lists):
384424
# Arrange
385425
set_context("1.1.1.1") # This IP is blocked
386-
blocked_ips = [
387-
{
388-
"source": "test",
389-
"description": "Blocked for testing",
390-
"ips": ["192.168.1.1", "192.168.1.2", "1.1.1.0/24"],
391-
}
392-
]
393-
config = create_service_config(blocked_ips)
394-
config.set_allowed_ips(
426+
create_service_config()
427+
firewall_lists.set_blocked_ips(
428+
[
429+
{
430+
"source": "test",
431+
"description": "Blocked for testing",
432+
"ips": ["192.168.1.1", "192.168.1.2", "1.1.1.0/24"],
433+
}
434+
]
435+
)
436+
firewall_lists.set_allowed_ips(
395437
[
396438
{
397439
"source": "test",
@@ -411,11 +453,12 @@ def test_allowed_for_endpoint_in_allowlist_but_blocked():
411453
)
412454

413455

414-
def test_allowed_for_endpoint_and_in_allowlist_but_is_bot():
456+
@patch_firewall_lists
457+
def test_allowed_for_endpoint_and_in_allowlist_but_is_bot(firewall_lists):
415458
# Arrange
416459
set_context("1.1.1.1", "Test_be bot")
417-
config = create_service_config()
418-
config.set_blocked_user_agents("test_ua|test_be")
460+
create_service_config()
461+
firewall_lists.set_blocked_user_agents("test_ua|test_be")
419462

420463
# Act
421464
result = request_handler("pre_response")
@@ -427,14 +470,15 @@ def test_allowed_for_endpoint_and_in_allowlist_but_is_bot():
427470
)
428471

429472

430-
def test_allowed_for_endpoint_and_in_allowlist_but_is_bot_2():
473+
@patch_firewall_lists
474+
def test_allowed_for_endpoint_and_in_allowlist_but_is_bot_2(firewall_lists):
431475
# Arrange
432476
set_context(
433477
"1.1.1.1",
434478
"Mozilla/5.0 (compatible; Bytespider/1.0; +http://bytespider.com/bot.html)",
435479
)
436-
config = create_service_config()
437-
config.set_blocked_user_agents(
480+
create_service_config()
481+
firewall_lists.set_blocked_user_agents(
438482
"AI2Bot|Applebot-Extended|Bytespider|CCBot|ClaudeBot|cohere-training-data-crawler|Diffbot|Google-Extended|GPTBot|Kangaroo Bot|meta-externalagent|anthropic-ai|omgili|PanguBot|Webzio-Extended|Timpibot|img2dataset|ImagesiftBot|archive.org_bot"
439483
)
440484

@@ -458,11 +502,14 @@ def test_allowed_for_endpoint_and_in_allowlist_but_is_bot_2():
458502
)
459503

460504

461-
def test_allowed_for_endpoint_and_in_allowlist_bots_are_set_but_is_not_bot():
505+
@patch_firewall_lists
506+
def test_allowed_for_endpoint_and_in_allowlist_bots_are_set_but_is_not_bot(
507+
firewall_lists,
508+
):
462509
# Arrange
463510
set_context("1.1.1.1", "Test_u4 bot")
464-
config = create_service_config()
465-
config.set_blocked_user_agents("test_ua|test_be")
511+
create_service_config()
512+
firewall_lists.set_blocked_user_agents("test_ua|test_be")
466513

467514
# Act
468515
result = request_handler("pre_response")

0 commit comments

Comments
 (0)