-
Notifications
You must be signed in to change notification settings - Fork 14
Allow disabling rate limiting per user #616
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,14 +15,15 @@ def user(): | |
| return {"id": "user123"} | ||
|
|
||
|
|
||
| def create_connection_manager(endpoints=[], bypassed_ips=[]): | ||
| def create_connection_manager(endpoints=[], bypassed_ips=[], excluded_uids=[]): | ||
| cm = MagicMock() | ||
| cm.conf = ServiceConfig( | ||
| endpoints=endpoints, | ||
| last_updated_at=1, | ||
| blocked_uids=[], | ||
| bypassed_ips=bypassed_ips, | ||
| received_any_stats=True, | ||
| excluded_uids_from_rate_limiting=excluded_uids, | ||
| ) | ||
| cm.rate_limiter = RateLimiter( | ||
| max_items=5000, time_to_live_in_ms=120 * 60 * 1000 # 120 minutes | ||
|
|
@@ -478,6 +479,90 @@ def test_works_with_multiple_rate_limit_groups_and_different_users(): | |
| } | ||
|
|
||
|
|
||
| def test_excluded_user_bypasses_user_rate_limit(): | ||
| endpoint = { | ||
| "method": "POST", | ||
| "route": "/login", | ||
| "forceProtectionOff": False, | ||
| "rateLimiting": { | ||
| "enabled": True, | ||
| "maxRequests": 3, | ||
| "windowSizeInMS": 1000, | ||
| }, | ||
| } | ||
| cm = create_connection_manager([endpoint], excluded_uids=["user123"]) | ||
| route_metadata = create_route_metadata() | ||
|
|
||
| # Excluded user should never be blocked, even past maxRequests | ||
| for _ in range(5): | ||
| assert should_ratelimit_request( | ||
| route_metadata, "1.2.3.4", {"id": "user123"}, cm | ||
| ) == {"block": False} | ||
|
|
||
|
|
||
| def test_non_excluded_user_still_rate_limited(): | ||
| endpoint = { | ||
| "method": "POST", | ||
| "route": "/login", | ||
| "forceProtectionOff": False, | ||
| "rateLimiting": { | ||
| "enabled": True, | ||
| "maxRequests": 3, | ||
| "windowSizeInMS": 1000, | ||
| }, | ||
| } | ||
| cm = create_connection_manager([endpoint], excluded_uids=["other_user"]) | ||
| route_metadata = create_route_metadata() | ||
|
|
||
| assert should_ratelimit_request( | ||
| route_metadata, "1.2.3.4", {"id": "user123"}, cm | ||
| ) == {"block": False} | ||
| assert should_ratelimit_request( | ||
| route_metadata, "1.2.3.4", {"id": "user123"}, cm | ||
| ) == {"block": False} | ||
| assert should_ratelimit_request( | ||
| route_metadata, "1.2.3.4", {"id": "user123"}, cm | ||
| ) == {"block": False} | ||
| assert should_ratelimit_request( | ||
| route_metadata, "1.2.3.4", {"id": "user123"}, cm | ||
| ) == { | ||
| "block": True, | ||
| "trigger": "user", | ||
| } | ||
|
|
||
|
|
||
| def test_excluded_user_still_blocked_by_group_rate_limit(): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this what we want? I would have expected that a user that has been excluded would never be rate limited, even if they're assigned to a group.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed as discussed in 802f1ea |
||
| endpoint = { | ||
| "method": "POST", | ||
| "route": "/login", | ||
| "forceProtectionOff": False, | ||
| "rateLimiting": { | ||
| "enabled": True, | ||
| "maxRequests": 3, | ||
| "windowSizeInMS": 1000, | ||
| }, | ||
| } | ||
| cm = create_connection_manager([endpoint], excluded_uids=["user123"]) | ||
| route_metadata = create_route_metadata() | ||
|
|
||
| assert should_ratelimit_request( | ||
| route_metadata, "1.2.3.4", {"id": "user123"}, cm, "group1" | ||
| ) == {"block": False} | ||
| assert should_ratelimit_request( | ||
| route_metadata, "1.2.3.4", {"id": "user123"}, cm, "group1" | ||
| ) == {"block": False} | ||
| assert should_ratelimit_request( | ||
| route_metadata, "1.2.3.4", {"id": "user123"}, cm, "group1" | ||
| ) == {"block": False} | ||
| # Group rate limit still applies even for excluded users | ||
| assert should_ratelimit_request( | ||
| route_metadata, "1.2.3.4", {"id": "user123"}, cm, "group1" | ||
| ) == { | ||
| "block": True, | ||
| "trigger": "group", | ||
| } | ||
|
|
||
|
|
||
| def test_rate_limits_by_group_if_user_is_not_set(): | ||
| cm = create_connection_manager( | ||
| [ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.