From: Dan Brown Date: Mon, 30 Aug 2021 20:28:17 +0000 (+0100) Subject: Added back email confirmation check in middleware X-Git-Tag: v21.08~1^2~8 X-Git-Url: http://source.bookstackapp.com/bookstack/commitdiff_plain/2740603d99099b2efb607e06f760e8f454bc0768 Added back email confirmation check in middleware During writing of the update notes, found that the upgrade path would be tricky from a security point of view. If people were pending email confirmation but had an active session, they could technically be actively logged in after the next release. Added middlware as an extra precaution for now. --- diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 1733d29b3..4b8cdfba4 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -30,6 +30,7 @@ class Kernel extends HttpKernel \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \BookStack\Http\Middleware\VerifyCsrfToken::class, + \BookStack\Http\Middleware\CheckEmailConfirmed::class, \BookStack\Http\Middleware\RunThemeActions::class, \BookStack\Http\Middleware\Localization::class, ], @@ -38,6 +39,7 @@ class Kernel extends HttpKernel \BookStack\Http\Middleware\EncryptCookies::class, \BookStack\Http\Middleware\StartSessionIfCookieExists::class, \BookStack\Http\Middleware\ApiAuthenticate::class, + \BookStack\Http\Middleware\CheckEmailConfirmed::class, ], ]; diff --git a/app/Http/Middleware/CheckEmailConfirmed.php b/app/Http/Middleware/CheckEmailConfirmed.php new file mode 100644 index 000000000..b4843e79b --- /dev/null +++ b/app/Http/Middleware/CheckEmailConfirmed.php @@ -0,0 +1,48 @@ +confirmationService = $confirmationService; + } + + + /** + * Handle an incoming request. + * + * @param \Illuminate\Http\Request $request + * @param \Closure $next + * @return mixed + */ + public function handle($request, Closure $next) + { + /** @var User $user */ + $user = auth()->user(); + if (auth()->check() && !$user->email_confirmed && $this->confirmationService->confirmationRequired()) { + auth()->logout(); + return redirect()->to('/'); + } + + return $next($request); + } +} diff --git a/tests/Auth/AuthTest.php b/tests/Auth/AuthTest.php index 657728c17..718fb859d 100644 --- a/tests/Auth/AuthTest.php +++ b/tests/Auth/AuthTest.php @@ -459,6 +459,22 @@ class AuthTest extends BrowserKitTest $this->assertFalse($log->hasWarningThatContains('Failed login for admin@admin.com')); } + public function test_logged_in_user_with_unconfirmed_email_is_logged_out() + { + $this->setSettings(['registration-confirmation' => 'true']); + $user = $this->getEditor(); + $user->email_confirmed = false; + $user->save(); + + auth()->login($user); + $this->assertTrue(auth()->check()); + + $this->get('/books'); + $this->assertRedirectedTo("/"); + + $this->assertFalse(auth()->check()); + } + /** * Perform a login. */