-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
Rate Limiting Fails with Multiple Distinct Keys and Order-Sensitive Limits #54386
Comments
To your first issue, you define a limit per day per user, so I think it is right, that he is not able to update product 2. If you want to achieve on update per product and user, you should use one key like I think for the second issue: it would be best to use distinctive keys like |
Issue 1: Multiple Distinct Keys vs Composite Keys Your Suggestion: Why This Doesn’t Fully Address the Issue: Business Requirement:
Your Suggestion Code: // Composite Key Approach
Limit::perDay(1)->by("user:{$user->id}:product:{$product->id}"); Result:
Conclusion:
Issue 2: Order Sensitivity & Distinctive Keys Your Suggestion:
Why This Doesn’t Fix the Core Problem: RateLimiter::for('test', function (Request $request) {
return [
Limit::perDay(1000)->by("daily:{$user->id}"), // Checked first
Limit::perMinute(10)->by("minute:{$user->id}"), // Checked second
];
}); If limits are ordered as [daily, minute], the daily limit is checked first. Root Cause: My solution: protected function handleRequest($request, Closure $next, array $limits) {
// 1. Check all limits first
foreach ($limits as $limit) {
if ($this->tooManyAttempts(...)) {
throw $exception;
}
}
// 2. Apply hits only if all limits pass
foreach ($limits as $limit) {
$this->hit(...);
}
return $next($request);
} Advantages:
Performance Trade-Off: |
Laravel Version
11
PHP Version
8.1.4
Database Driver & Version
No response
Description
The current
ThrottleRequests
middleware incorrectly handles rate limits when:user_id
+product_id
) are used in the same limiter.Steps To Reproduce
Issue 1: Over-Throttling with Multiple Distinct Keys
Issue 2: Order-Sensitive Limits in Documentation
Description:
The documentation example shows limits ordered as perMinute then perDay, but reversing the order breaks functionality.
Broken Example:
Proposed Solution
Modify
src/Illuminate/Routing/Middleware/ThrottleRequests.php
to:hit()
.Code Fix:
Suggested Labels:
bug
,rate limiting
The text was updated successfully, but these errors were encountered: