Commit 6ef4edc
feat(middleware): add Redis-backed rate limiting with tier-based limits (#4423)
* feat(middleware): add Redis-backed rate limiting with tier-based limits
- Add RateLimitMiddleware with Redis-backed sliding window algorithm
- Implement tier-based rate limits (CRITICAL/HIGH/MEDIUM/LOW) per endpoint
- Add lockout mechanism after excessive violations (5 violations = 15 min lockout)
- Add multi-dimensional limiting (IP → User → Team)
- Include security event logging for audit trail
- Add 107 unit tests with 95% coverage
Implements X-Force Red security findings for API rate limiting.
Closes #4168
Signed-off-by: Mohan Lakshmaiah <mohan.economist@gmail.com>
* chore: fix end of file newline
Signed-off-by: Mohan Lakshmaiah <mohan.economist@gmail.com>
* test: disable rate limiting in test suite
Rate limiting middleware was causing 38 test failures with 429 responses.
Tests make rapid sequential requests that exceed rate limits.
Follow existing pattern in conftest.py to disable optional middleware
during tests (admin API, UI, llmchat are similarly disabled).
Tests specifically for rate limiting (test_rate_limit_middleware.py)
explicitly enable the middleware via settings override.
Fixes test suite CI failures after rate limiting feature merge.
Signed-off-by: Mohan Lakshmaiah <mohan.economist@gmail.com>
* fix(middleware): rate limiting council review fixes
- Prevent infinite lockout loop by skipping violation increment during lockout
- Add timestamp-based expiry to memory violation counts
- Harden IP extraction against proxy spoofing via request.scope
- Use atomic Lua script for Redis sliding window check+add
- Reuse pre-check results to avoid double-counting on success
- Update tests to properly exercise Redis Lua script path
- Add regression tests for lockout expiry and no-increment behavior
Signed-off-by: Jonathan Springer <jps@s390x.com>
* chore: update .secrets.baseline timestamp
Signed-off-by: Jonathan Springer <jps@s390x.com>
* test(middleware): improve rate limiting diff coverage to 100%
Add tests for previously uncovered code paths:
- Redis init success and exception handling
- User object email extraction from request.state.user
- Redis Lua script blocked (0) response
- Lockout async executor exception fallback
- Redis lockout count below threshold
- Memory violation expiry cleanup and initialization
- Violation increment async exception fallback
- Redis violation increment exception fallback
- Main app middleware registration when rate limiting enabled
Signed-off-by: Jonathan Springer <jps@s390x.com>
---------
Signed-off-by: Mohan Lakshmaiah <mohan.economist@gmail.com>
Signed-off-by: Jonathan Springer <jps@s390x.com>
Co-authored-by: Jonathan Springer <jps@s390x.com>1 parent 7a19c94 commit 6ef4edc
7 files changed
Lines changed: 1868 additions & 5 deletions
File tree
- mcpgateway
- middleware
- tests
- unit/mcpgateway
- middleware
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
6098 | 6098 | | |
6099 | 6099 | | |
6100 | 6100 | | |
6101 | | - | |
| 6101 | + | |
6102 | 6102 | | |
6103 | 6103 | | |
6104 | 6104 | | |
6105 | 6105 | | |
6106 | 6106 | | |
6107 | 6107 | | |
6108 | 6108 | | |
6109 | | - | |
| 6109 | + | |
6110 | 6110 | | |
6111 | 6111 | | |
6112 | 6112 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3115 | 3115 | | |
3116 | 3116 | | |
3117 | 3117 | | |
3118 | | - | |
3119 | | - | |
| 3118 | + | |
| 3119 | + | |
| 3120 | + | |
| 3121 | + | |
| 3122 | + | |
| 3123 | + | |
| 3124 | + | |
| 3125 | + | |
| 3126 | + | |
| 3127 | + | |
| 3128 | + | |
| 3129 | + | |
| 3130 | + | |
| 3131 | + | |
| 3132 | + | |
| 3133 | + | |
| 3134 | + | |
| 3135 | + | |
| 3136 | + | |
| 3137 | + | |
| 3138 | + | |
| 3139 | + | |
| 3140 | + | |
| 3141 | + | |
| 3142 | + | |
3120 | 3143 | | |
3121 | 3144 | | |
3122 | 3145 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
| 106 | + | |
106 | 107 | | |
107 | 108 | | |
108 | 109 | | |
| |||
2975 | 2976 | | |
2976 | 2977 | | |
2977 | 2978 | | |
| 2979 | + | |
| 2980 | + | |
| 2981 | + | |
| 2982 | + | |
| 2983 | + | |
| 2984 | + | |
| 2985 | + | |
| 2986 | + | |
| 2987 | + | |
| 2988 | + | |
| 2989 | + | |
2978 | 2990 | | |
2979 | 2991 | | |
2980 | 2992 | | |
| |||
0 commit comments