Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eludic's avatar

Laravel protection for brute force attack after login

Recently we asked a security company to do a VAPT on our Laravel application and they discovered a brute force attack on our application after login into the system

For instance we had a route with parameters something like

https://www.mydomain.com/laravel/employee/3434

Instead of 3434 he was passing values, 1,00,000 request within a few minutes. Although the framework only displayed him content of those values which he was entitled to see while others were giving him a 302 error which is re-directing to do not have permissions page.

The security advisors view was that the software should not allow to place so many request even though the user was logged it. It should have probably stopped the user to 10-12 request within a few seconds/minutes.

How do I change this in the framework?

0 likes
2 replies
LaryAI's avatar

You can address this issue by implementing rate limiting on your sensitive routes, even for authenticated users. Laravel provides built-in support for rate limiting using middleware.

Solution Steps

1. Use Laravel's Throttle Middleware

You can apply the throttle middleware to your routes. For example, to limit access to the employee route to 10 requests per minute:

Route::middleware(['auth', 'throttle:10,1'])->group(function () {
    Route::get('/employee/{id}', [EmployeeController::class, 'show']);
});
  • throttle:10,1 means 10 requests per 1 minute per user (if authenticated) or per IP (if not).

2. Customizing Rate Limiting (Laravel 8+)

If you want more control (e.g., different limits for different routes), you can define custom rate limiters in app/Providers/RouteServiceProvider.php:

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;

public function boot()
{
    RateLimiter::for('employee', function ($request) {
        return Limit::perMinute(10)->by($request->user()?->id ?: $request->ip());
    });

    // ...
}

Then, apply this limiter to your route:

Route::middleware(['auth', 'throttle:employee'])->group(function () {
    Route::get('/employee/{id}', [EmployeeController::class, 'show']);
});

3. Handling Rate Limit Responses

When the limit is exceeded, Laravel automatically returns a 429 Too Many Requests response. You can customize the response if needed by handling the ThrottleRequestsException.

4. Considerations

  • Logging: You may want to log excessive requests for further analysis.
  • User Feedback: Inform users when they hit the rate limit.
  • Fine-tuning: Adjust the rate limits based on your application's needs.

Summary:
Apply Laravel's throttle middleware to your sensitive routes to prevent brute force attacks, even after login. This will limit the number of requests a user can make in a given time period and protect your application from abuse.

Please or to participate in this conversation.