Skip to content

Commit c97b628

Browse files
committed
Update whitelisting logic and tests for IP allowlists
Modified the `should_whitelist_request` documentation to clarify that the request IP must be in the endpoint allowlist. Updated the request processing logic to use `IsEndpointIpWhitelisted` for checking IPs against the endpoint allowlist. Refactored the context to store the allowlist status as an integer. Enhanced tests to validate multiple IPs in the global allowlist and adjusted assertions accordingly.
1 parent e35971b commit c97b628

File tree

7 files changed

+29
-16
lines changed

7 files changed

+29
-16
lines changed

docs/should_whitelist_request.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Returns an `AikidoWhitelistRequestStatus` object with the following properties:
2222

2323
The function checks three conditions in order. The first match wins:
2424

25-
1. **`endpoint-allowlist`** — The endpoint has a route-level IP allowlist configured and the request IP is not in it. This indicates that IP-based access control is active for this route.
25+
1. **`endpoint-allowlist`** — The endpoint has a route-level IP allowlist configured and the request IP is in it. This indicates that IP-based access control is active for this route.
2626
2. **`bypassed`** — The request IP is in the global firewall bypass list.
2727
3. **`allowlist`** — The request IP is found in the global allowed IP list.
2828

lib/request-processor/context/cache.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,7 @@ func ContextSetIsEndpointIpAllowed(instance *instance.RequestProcessorInstance)
299299
}
300300
}
301301

302-
isEndpointIpAllowedBool := isEndpointIpAllowed != utils.NotFound
303-
304-
c.IsEndpointIpAllowed = &isEndpointIpAllowedBool
302+
c.IsEndpointIpAllowed = &isEndpointIpAllowed
305303
}
306304

307305
func ContextSetIsEndpointRateLimited(instance *instance.RequestProcessorInstance) {

lib/request-processor/context/request_context.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"main/globals"
88
"main/instance"
99
"main/log"
10+
"main/utils"
1011
"unsafe"
1112
)
1213

@@ -29,7 +30,7 @@ type RequestContextData struct {
2930
IsEndpointConfigured *bool
3031
IsEndpointRateLimitingEnabled *bool
3132
IsEndpointProtectionTurnedOff *bool
32-
IsEndpointIpAllowed *bool
33+
IsEndpointIpAllowed *int
3334
IsEndpointRateLimited bool
3435
UserAgent *string
3536
UserId *string
@@ -247,5 +248,10 @@ func IsEndpointRateLimitingEnabled(instance *instance.RequestProcessorInstance)
247248

248249
func IsEndpointIpAllowed(instance *instance.RequestProcessorInstance) bool {
249250
ctx := GetContext(instance)
250-
return GetFromCache(instance, ContextSetIsEndpointIpAllowed, &ctx.IsEndpointIpAllowed)
251+
return GetFromCache(instance, ContextSetIsEndpointIpAllowed, &ctx.IsEndpointIpAllowed) != utils.NotFound
252+
}
253+
254+
func IsEndpointIpWhitelisted(instance *instance.RequestProcessorInstance) bool {
255+
ctx := GetContext(instance)
256+
return GetFromCache(instance, ContextSetIsEndpointIpAllowed, &ctx.IsEndpointIpAllowed) == utils.Found
251257
}

lib/request-processor/handle_blocking_request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func OnGetWhitelistedStatus(instance *instance.RequestProcessorInstance) string
164164
log.Debugf(instance, "OnGetWhitelistedStatus called!")
165165
ip := context.GetIp(instance)
166166

167-
if !context.IsEndpointIpAllowed(instance) {
167+
if context.IsEndpointIpWhitelisted(instance) {
168168
return GetAction("whitelisted", "endpoint-allowlist", "ip", "IP is configured in the route's allowlist", ip, 0)
169169
}
170170

tests/server/test_whitelist_endpoint_allowlist/test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515

1616

1717
def run_test():
18-
response = php_server_get("/test", headers={"X-Forwarded-For": "185.245.255.211"})
18+
response = php_server_get("/test", headers={"X-Forwarded-For": "185.245.255.212"})
1919
assert_response_code_is(response, 200)
2020
assert_response_body_contains(response, "whitelisted=true;")
2121
assert_response_body_contains(response, "type=endpoint-allowlist;")
2222
assert_response_body_contains(response, "trigger=ip;")
2323
assert_response_body_contains(response, "description=IP is configured in the route's allowlist;")
24-
assert_response_body_contains(response, "ip=185.245.255.211;")
24+
assert_response_body_contains(response, "ip=185.245.255.212;")
2525
assert_response_body_contains(response, "Something!")
2626

2727

tests/server/test_whitelist_global_allowlist/start_config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"key": "manual",
1111
"source": "manual",
1212
"description": "Manually allowed IPs",
13-
"ips": ["185.245.255.211"]
13+
"ips": ["185.245.255.211", "185.245.255.214"]
1414
}
1515
],
1616
"receivedAnyStats": true

tests/server/test_whitelist_global_allowlist/test.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,32 @@
77
Test that should_whitelist_request returns whitelisted=true with type=allowlist
88
when the request IP is found in the global allowed IP list.
99
10-
The IP 185.245.255.211 is in lists_allowedIPAddresses with description "Manually allowed IPs".
11-
It is not bypassed and no endpoint-level allowlist is configured.
12-
The allowlist check is the third condition in OnGetWhitelistedStatus.
10+
Both 185.245.255.211 and 185.245.255.214 are in lists_allowedIPAddresses with
11+
description "Manually allowed IPs". Multiple requests from both IPs should all
12+
return whitelisted=true. No bypass and no endpoint-level allowlist is configured.
1313
'''
1414

1515

16-
def run_test():
17-
response = php_server_get("/test", headers={"X-Forwarded-For": "185.245.255.211"})
16+
def assert_whitelisted_for_ip(ip):
17+
response = php_server_get("/test", headers={"X-Forwarded-For": ip})
1818
assert_response_code_is(response, 200)
1919
assert_response_body_contains(response, "whitelisted=true;")
2020
assert_response_body_contains(response, "type=allowlist;")
2121
assert_response_body_contains(response, "trigger=ip;")
2222
assert_response_body_contains(response, "description=IP is part of allowlist: Manually allowed IPs;")
23-
assert_response_body_contains(response, "ip=185.245.255.211;")
23+
assert_response_body_contains(response, f"ip={ip};")
2424
assert_response_body_contains(response, "Something!")
2525

2626

27+
def run_test():
28+
assert_whitelisted_for_ip("185.245.255.211")
29+
assert_whitelisted_for_ip("185.245.255.214")
30+
assert_whitelisted_for_ip("185.245.255.211")
31+
assert_whitelisted_for_ip("185.245.255.214")
32+
assert_whitelisted_for_ip("185.245.255.214")
33+
assert_whitelisted_for_ip("185.245.255.211")
34+
35+
2736
if __name__ == "__main__":
2837
load_test_args()
2938
run_test()

0 commit comments

Comments
 (0)