Skip to content

Count rate-limited hits #408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions aikido_zen/middleware/init_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def test_with_context_with_cache():
}
assert get_current_context().executed_middleware == True
assert thread_cache.middleware_installed == True
assert thread_cache.stats.rate_limited_hits == 0

thread_cache.config.blocked_uids = []
assert should_block_request() == {"block": False}
Expand All @@ -69,6 +70,7 @@ def test_with_context_with_cache():
assert should_block_request() == {"block": False}
assert get_current_context().executed_middleware == True
assert thread_cache.middleware_installed == True
assert thread_cache.stats.rate_limited_hits == 0


def test_cache_comms_with_endpoints():
Expand Down Expand Up @@ -158,9 +160,11 @@ def test_cache_comms_with_endpoints():
"success": True,
"data": {"block": True, "trigger": "my_trigger"},
}
assert thread_cache.stats.rate_limited_hits == 0
assert should_block_request() == {
"block": True,
"ip": "::1",
"type": "ratelimited",
"trigger": "my_trigger",
}
assert thread_cache.stats.rate_limited_hits == 1
1 change: 1 addition & 0 deletions aikido_zen/middleware/should_block_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def should_block_request():
receive=True,
)
if ratelimit_res["success"] and ratelimit_res["data"]["block"]:
cache.stats.on_rate_limit()
return {
"block": True,
"type": "ratelimited",
Expand Down
7 changes: 7 additions & 0 deletions aikido_zen/storage/statistics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ def __init__(self):
self.total_hits = 0
self.attacks_detected = 0
self.attacks_blocked = 0
self.rate_limited_hits = 0
self.started_at = t.get_unixtime_ms()
self.operations = Operations()

def clear(self):
self.total_hits = 0
self.attacks_detected = 0
self.attacks_blocked = 0
self.rate_limited_hits = 0
self.started_at = t.get_unixtime_ms()
self.operations.clear()

Expand All @@ -31,13 +33,17 @@ def on_detected_attack(self, blocked, operation):
self.attacks_blocked += 1
self.operations.on_detected_attack(blocked, operation)

def on_rate_limit(self):
self.rate_limited_hits += 1

def get_record(self):
current_time = t.get_unixtime_ms()
return {
"startedAt": self.started_at,
"endedAt": current_time,
"requests": {
"total": self.total_hits,
"rateLimited": self.rate_limited_hits,
"aborted": 0, # statistic currently not in use
"attacksDetected": {
"total": self.attacks_detected,
Expand All @@ -50,6 +56,7 @@ def get_record(self):
def import_from_record(self, record):
attacks_detected = record.get("requests", {}).get("attacksDetected", {})
self.total_hits += record.get("requests", {}).get("total", 0)
self.rate_limited_hits += record.get("requests", {}).get("rateLimited", 0)
self.attacks_detected += attacks_detected.get("total", 0)
self.attacks_blocked += attacks_detected.get("blocked", 0)
self.operations.update(record.get("operations", {}))
Expand Down
47 changes: 47 additions & 0 deletions aikido_zen/storage/statistics/init_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ def test_get_record(monkeypatch):

stats = Statistics()
stats.total_hits = 10
stats.on_rate_limit()
stats.on_rate_limit()
stats.operations.register_call("test.test", "nosql_op")
stats.on_detected_attack(blocked=True, operation="test.test")
stats.attacks_detected = 5
Expand All @@ -77,6 +79,7 @@ def test_get_record(monkeypatch):
assert record["startedAt"] == stats.started_at
assert record["endedAt"] == mock_time
assert record["requests"]["total"] == 10
assert record["requests"]["rateLimited"] == 2
assert record["requests"]["aborted"] == 0
assert record["requests"]["attacksDetected"]["total"] == 5
assert record["requests"]["attacksDetected"]["blocked"] == 3
Expand All @@ -97,6 +100,7 @@ def test_import_from_record():
record = {
"requests": {
"total": 10,
"rateLimited": 5,
"attacksDetected": {
"total": 5,
"blocked": 3,
Expand All @@ -117,6 +121,7 @@ def test_import_from_record():
}
stats.import_from_record(record)
assert stats.total_hits == 10
assert stats.rate_limited_hits == 5
assert stats.attacks_detected == 5
assert stats.attacks_blocked == 3
assert stats.operations == {
Expand Down Expand Up @@ -152,6 +157,7 @@ def test_multiple_imports(stats):
record1 = {
"requests": {
"total": 10,
"rateLimited": 20,
"attacksDetected": {
"total": 5,
"blocked": 3,
Expand All @@ -168,6 +174,7 @@ def test_multiple_imports(stats):
record2 = {
"requests": {
"total": 20,
"rateLimited": 5,
"attacksDetected": {
"total": 10,
"blocked": 7,
Expand All @@ -184,6 +191,7 @@ def test_multiple_imports(stats):
stats.import_from_record(record1)
stats.import_from_record(record2)
assert stats.total_hits == 30
assert stats.rate_limited_hits == 25
assert stats.attacks_detected == 15
assert stats.attacks_blocked == 10
assert stats.operations == {
Expand All @@ -204,6 +212,7 @@ def test_import_empty_record(stats):
record = {"requests": {}}
stats.import_from_record(record)
assert stats.total_hits == 0
assert stats.rate_limited_hits == 0
assert stats.attacks_detected == 0
assert stats.attacks_blocked == 0
assert stats.operations == {}
Expand All @@ -213,6 +222,7 @@ def test_import_partial_record(stats):
record = {"requests": {"total": 10}}
stats.import_from_record(record)
assert stats.total_hits == 10
assert stats.rate_limited_hits == 0
assert stats.attacks_detected == 0
assert stats.attacks_blocked == 0
assert stats.operations == {}
Expand Down Expand Up @@ -242,3 +252,40 @@ def test_multiple_increments_and_detects(stats):
"kind": "sql_op",
"total": 1,
}

stats.on_rate_limit()
assert stats.rate_limited_hits == 1

stats.on_rate_limit()
assert stats.rate_limited_hits == 2


def test_multiple_rate_limits(stats):
"""Test multiple rate limit calls"""
for _ in range(5):
stats.on_rate_limit()
assert stats.rate_limited_hits == 5


def test_rate_limit_in_get_record():
"""Test that rate_limited_hits is included in get_record output"""
stats = Statistics()
stats.total_hits = 10
stats.on_rate_limit()
stats.on_rate_limit()
stats.on_rate_limit()

record = stats.get_record()
assert record["requests"]["rateLimited"] == 3
assert record["requests"]["total"] == 10


def test_rate_limit_clear():
"""Test that clear() resets rate_limited_hits"""
stats = Statistics()
stats.on_rate_limit()
stats.on_rate_limit()
assert stats.rate_limited_hits == 2

stats.clear()
assert stats.rate_limited_hits == 0
Loading