Skip to content

A free open source IT asset/license management system

Use sameOriginUrl on login failure redirect

snipe8c33d8325e01
diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php
index 5e3e6fd45d..ed180b0580 100644
--- a/app/Http/Controllers/Auth/LoginController.php
+++ b/app/Http/Controllers/Auth/LoginController.php
@@ -319,7 +319,8 @@ class LoginController extends Controller
$validator = $this->validator($request->all());
if ($validator->fails()) {
- return redirect()->back()->withInput()->withErrors($validator);
+ return redirect()->to(Helper::sameOriginUrl(url()->previous()) ?? route('login'))
+ ->withInput()->withErrors($validator);
}
// Read the local login-form throttle ceiling and lockout window
@@ -367,7 +368,10 @@ class LoginController extends Controller
Log::debug('Local authentication failed.');
- return redirect()->back()->withInput()->with('error', trans('auth/message.account_not_found'));
+ // Same open-redirect guard as the validation-failure
+ // path above. See comment there.
+ return redirect()->to(Helper::sameOriginUrl(url()->previous()) ?? route('login'))
+ ->withInput()->with('error', trans('auth/message.account_not_found'));
} else {
$this->clearLoginAttempts($request);
}
diff --git a/tests/Feature/Authentication/LoginFailureRedirectTest.php b/tests/Feature/Authentication/LoginFailureRedirectTest.php
new file mode 100644
index 0000000000..d0be3f6fd4
--- /dev/null
+++ b/tests/Feature/Authentication/LoginFailureRedirectTest.php
@@ -0,0 +1,68 @@
+<?php
+
+namespace Tests\Feature\Authentication;
+
+use App\Models\User;
+use Illuminate\Support\Facades\Cache;
+use Tests\TestCase;
+
+/**
+ * On a failed login POST, LoginController previously called
+ * redirect()->back(), which emits a 302 to whatever Referer the browser
+ * sent. A crafted link to the login page could land the victim's failed
+ * submission on an attacker-controlled host for a phishing hand-off.
+ * The fix routes the fallback target through Helper::sameOriginUrl, so
+ * only in-origin Referers survive and anything else lands back on the
+ * login page (where the withInput + withErrors flash still renders).
+ */
+class LoginFailureRedirectTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ // ThrottlesLogins reads/writes RateLimiter cache counters. The
+ // array cache driver persists across tests in-process, so a prior
+ // test could push this one past the lockout threshold before
+ // its first assertion. Zero the counters.
+ Cache::flush();
+ }
+
+ public function test_validation_failure_does_not_redirect_to_offsite_referer()
+ {
+ User::factory()->create();
+
+ $this->from('https://evil.example.net/phish')
+ ->post('/login', [
+ 'username' => '',
+ 'password' => '',
+ ])
+ ->assertRedirect(route('login'));
+ }
+
+ public function test_authentication_failure_does_not_redirect_to_offsite_referer()
+ {
+ User::factory()->create(['username' => 'legit-user']);
+
+ $this->from('https://evil.example.net/phish')
+ ->post('/login', [
+ 'username' => 'legit-user',
+ 'password' => 'wrong-password',
+ ])
+ ->assertRedirect(route('login'));
+ }
+
+ public function test_validation_failure_honors_same_origin_referer()
+ {
+ User::factory()->create();
+
+ $safeReferer = config('app.url').'/login';
+
+ $this->from($safeReferer)
+ ->post('/login', [
+ 'username' => '',
+ 'password' => '',
+ ])
+ ->assertRedirect($safeReferer);
+ }
+}