Skip to content

A free open source IT asset/license management system

Fixed FD-57283 - fixed login throttling and tests

snipe0766f27de952
diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php
index b618920635..b2133e9c82 100644
--- a/app/Http/Controllers/Auth/LoginController.php
+++ b/app/Http/Controllers/Auth/LoginController.php
@@ -300,11 +300,12 @@ class LoginController extends Controller
return redirect()->back()->withInput()->withErrors($validator);
}
- // Set the custom lockout attempts from the env and set the custom lockout throttle from the env.
- // We divide decayMinutes by 60 here to get minutes, since Laravel changed the default from minutes
- // to seconds, and we don't want to break limits on existing systems
- $this->maxAttempts = config('auth.passwords.users.throttle.max_attempts');
- $this->decayMinutes = (config('auth.passwords.users.throttle.lockout_duration') / 60);
+ // Read the local login-form throttle ceiling and lockout window
+ // from config. decayMinutes gets the raw seconds value divided
+ // by 60 so LOGIN_LOCKOUT_DURATION stays in seconds for admins
+ // while Laravel's ThrottlesLogins trait sees minutes.
+ $this->maxAttempts = config('auth.login_throttle.max_attempts');
+ $this->decayMinutes = config('auth.login_throttle.lockout_duration') / 60;
if ($lockedOut = $this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
@@ -319,7 +320,7 @@ class LoginController extends Controller
// demo seed points at Forumsys as a reference config for
// visitors to click through, we don't want the login form to
// actually try to bind against it on every demo sign-in.
- if (Setting::getSettings()->ldap_enabled && !config('app.lock_passwords')) { // avoid hitting the $this->ldap
+ if (Setting::getSettings()->ldap_enabled && ! config('app.lock_passwords')) { // avoid hitting the $this->ldap
Log::debug('LDAP is enabled.');
try {
Log::debug('Attempting to log user in by LDAP authentication.');
diff --git a/config/auth.php b/config/auth.php
index ad771e5896..a97710ef70 100644
--- a/config/auth.php
+++ b/config/auth.php
@@ -126,6 +126,24 @@ return [
'max_attempts_per_min' => env('TWO_FACTOR_MAX_ATTEMPTS_PER_MIN', 5),
],
+ /*
+ |--------------------------------------------------------------------------
+ | Login Form Throttle
+ |--------------------------------------------------------------------------
+ | Attempt / lockout ceiling for the local login form. Consumed by
+ | LoginController via Illuminate\Foundation\Auth\ThrottlesLogins.
+ |
+ | Kept as a dedicated top-level key rather than nested under
+ | `passwords.users` because Laravel 12 types
+ | DatabaseTokenRepository::$throttle as `int`, so an array value on
+ | that key TypeErrors password-reset construction.
+ |
+ */
+ 'login_throttle' => [
+ 'max_attempts' => env('LOGIN_MAX_ATTEMPTS', 5),
+ 'lockout_duration' => env('LOGIN_LOCKOUT_DURATION', 60),
+ ],
+
/*
|--------------------------------------------------------------------------
| Password Confirmation Timeout
diff --git a/tests/Feature/Authentication/LoginTest.php b/tests/Feature/Authentication/LoginTest.php
index 573f75c41c..c071f49074 100644
--- a/tests/Feature/Authentication/LoginTest.php
+++ b/tests/Feature/Authentication/LoginTest.php
@@ -3,10 +3,28 @@
namespace Tests\Feature\Authentication;
use App\Models\User;
+use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\RateLimiter;
+use Illuminate\Support\Str;
use Tests\TestCase;
class LoginTest extends TestCase
{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ // Every test in this class exercises Laravel's ThrottlesLogins
+ // trait, which reads and writes counters through the RateLimiter
+ // cache. The array cache driver used in tests persists for the
+ // lifetime of the process, so counters left over from a prior
+ // test (either in this file or another one that also POSTs to
+ // /login) can push the current test past its lockout threshold
+ // before its first assertion. Flush the whole cache so every
+ // test starts from a zeroed counter.
+ Cache::flush();
+ }
+
public function test_logs_failed_login_attempt()
{
@@ -30,21 +48,33 @@ class LoginTest extends TestCase
public function test_login_throttle_config_is_respected()
{
+ // Regression coverage for the Laravel 12 upgrade regression where
+ // config/auth.php collapsed the nested throttle array back to a
+ // scalar, causing LoginController's config reads to return null
+ // and the ThrottlesLogins trait to silently no-op.
+ //
+ // A user has to exist for the login POST to reach the controller.
+ // With an empty users table Snipe-IT's setup middleware short-
+ // circuits every POST /login to /setup, so the throttle path
+ // never runs.
+ User::factory()->create();
- $this->markTestIncomplete('This test is flaky and needs to be fixed. Passes and fails seemingly at random.');
- User::factory()->create(['username' => 'username_here']);
+ config(['auth.login_throttle.max_attempts' => 1]);
+ config(['auth.login_throttle.lockout_duration' => 60]);
- config(['auth.passwords.users.throttle.max_attempts' => 1]);
- config(['auth.passwords.users.throttle.lockout_duration' => 1]);
+ // Belt-and-suspenders: explicitly clear the exact key Laravel
+ // will use for this attempt (username|ip, transliterated /
+ // lowercased) in case anything survived Cache::flush().
+ RateLimiter::clear(Str::transliterate(Str::lower('invalid username').'|127.0.0.100'));
- for ($i = 0; $i < 2; $i++) {
- $this->from('/login')
- ->withServerVariables(['REMOTE_ADDR' => '127.0.0.100'])
- ->post('/login', [
- 'username' => 'invalid username',
- 'password' => 'invalid password',
- ]);
- }
+ // First failed login registers one attempt. max_attempts is 1,
+ // so the second POST must be blocked.
+ $this->from('/login')
+ ->withServerVariables(['REMOTE_ADDR' => '127.0.0.100'])
+ ->post('/login', [
+ 'username' => 'invalid username',
+ 'password' => 'invalid password',
+ ]);
$response = $this->from('/login')
->withServerVariables(['REMOTE_ADDR' => '127.0.0.100'])
@@ -61,7 +91,6 @@ class LoginTest extends TestCase
public function test_logs_successful_login()
{
- $this->markTestIncomplete('This test is flaky and needs to be fixed. Passes and fails seemingly at random.');
User::factory()->create(['username' => 'username_here']);
$this->withServerVariables(['REMOTE_ADDR' => '127.0.0.100'])