Skip to content

Commit 2c0fdf8

Browse files
committed
Updated public-login redirect to check url
Direct links to the login pages for public instances could lead to a redirect back to an external page upon login. This adds a check to ensure the URL is a URL expected from the current bookstack instance, or at least under the same domain. Fixes BookStackApp#2073
1 parent 2ed0317 commit 2c0fdf8

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

app/Http/Controllers/Auth/LoginController.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,13 @@ public function getLogin(Request $request)
7777
]);
7878
}
7979

80+
// Store the previous location for redirect after login
8081
$previous = url()->previous('');
81-
if (setting('app-public') && $previous && $previous !== url('/login')) {
82-
redirect()->setIntendedUrl($previous);
82+
if ($previous && $previous !== url('/login') && setting('app-public')) {
83+
$isPreviousFromInstance = (strpos($previous, url('/')) === 0);
84+
if ($isPreviousFromInstance) {
85+
redirect()->setIntendedUrl($previous);
86+
}
8387
}
8488

8589
return view('auth.login', [

tests/Auth/AuthTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,17 @@ public function test_login_redirects_to_initially_requested_url_correctly()
381381
->seePageUrlIs($page->getUrl());
382382
}
383383

384+
public function test_login_intended_redirect_does_not_redirect_to_external_pages()
385+
{
386+
config()->set('app.url', 'http://localhost');
387+
$this->setSettings(['app-public' => true]);
388+
389+
$this->get('/login', ['referer' => 'https://example.com']);
390+
$login = $this->post('/login', ['email' => 'admin@admin.com', 'password' => 'password']);
391+
392+
$login->assertRedirectedTo('http://localhost');
393+
}
394+
384395
public function test_login_authenticates_admins_on_all_guards()
385396
{
386397
$this->post('/login', ['email' => 'admin@admin.com', 'password' => 'password']);

0 commit comments

Comments
 (0)