File tree Expand file tree Collapse file tree
aikido_zen/sources/functions Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -19,7 +19,8 @@ def request_handler(stage, status_code=0):
1919 try :
2020 if stage == "init" :
2121 cache = get_cache ()
22- if ctx .get_current_context () and cache :
22+ context = ctx .get_current_context ()
23+ if context and cache and not cache .is_bypassed_ip (context .remote_address ):
2324 cache .stats .increment_total_hits ()
2425 if stage == "pre_response" :
2526 return pre_response ()
@@ -102,6 +103,9 @@ def post_response(status_code):
102103 if not cache :
103104 return
104105
106+ if cache .is_bypassed_ip (context .remote_address ):
107+ return
108+
105109 attack_wave = attack_wave_detector_store .is_attack_wave (context )
106110 if attack_wave :
107111 cache .stats .on_detected_attack_wave (blocked = False )
Original file line number Diff line number Diff line change @@ -131,6 +131,43 @@ def test_post_response_no_context(mock_get_comms):
131131 comms .send_data_to_bg_process .assert_not_called ()
132132
133133
134+ def test_bypassed_ip_no_stats_in_init ():
135+ cache = get_cache ()
136+ cache .config .set_bypassed_ips (["1.2.3.4" ])
137+ cache .stats .clear ()
138+
139+ context = MagicMock ()
140+ context .remote_address = "1.2.3.4"
141+ with patch ("aikido_zen.context.get_current_context" , return_value = context ):
142+ request_handler ("init" )
143+
144+ assert cache .stats .get_record ()["requests" ]["total" ] == 0
145+
146+
147+ def test_non_bypassed_ip_increments_stats_in_init ():
148+ cache = get_cache ()
149+ cache .config .set_bypassed_ips ([])
150+ cache .stats .clear ()
151+
152+ context = MagicMock ()
153+ context .remote_address = "1.2.3.4"
154+ with patch ("aikido_zen.context.get_current_context" , return_value = context ):
155+ request_handler ("init" )
156+
157+ assert cache .stats .get_record ()["requests" ]["total" ] == 1
158+
159+
160+ def test_bypassed_ip_no_route_tracking_in_post_response (mock_context ):
161+ cache = get_cache ()
162+ cache .config .set_bypassed_ips (["5.6.7.8" ])
163+ mock_context .remote_address = "5.6.7.8"
164+
165+ with patch ("aikido_zen.context.get_current_context" , return_value = mock_context ):
166+ request_handler ("post_response" , status_code = 200 )
167+
168+ assert cache .routes .routes == {}
169+
170+
134171# Test firewall lists
135172def set_context (remote_address , user_agent = "" , route = "/posts/:number" ):
136173 headers = Headers ()
You can’t perform that action at this time.
0 commit comments