Skip to content

Commit a75d5b8

Browse files
committed
Sessions: Prevent image urls being part of session URL history
To prevent them being considered for redirects. Includes test to cover. For #4863
1 parent 055bbf1 commit a75d5b8

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

app/Http/Kernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Kernel extends HttpKernel
2828
\BookStack\Http\Middleware\ApplyCspRules::class,
2929
\BookStack\Http\Middleware\EncryptCookies::class,
3030
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
31-
\Illuminate\Session\Middleware\StartSession::class,
31+
\BookStack\Http\Middleware\StartSessionExtended::class,
3232
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
3333
\BookStack\Http\Middleware\VerifyCsrfToken::class,
3434
\BookStack\Http\Middleware\CheckEmailConfirmed::class,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace BookStack\Http\Middleware;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Session\Middleware\StartSession as Middleware;
7+
8+
/**
9+
* An extended version of the default Laravel "StartSession" middleware
10+
* with customizations applied as required:
11+
*
12+
* - Adds filtering for the request URLs stored in session history.
13+
*/
14+
class StartSessionExtended extends Middleware
15+
{
16+
protected static array $pathPrefixesExcludedFromHistory = [
17+
'uploads/images/'
18+
];
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
protected function storeCurrentUrl(Request $request, $session): void
24+
{
25+
$requestPath = strtolower($request->path());
26+
foreach (static::$pathPrefixesExcludedFromHistory as $excludedPath) {
27+
if (str_starts_with($requestPath, $excludedPath)) {
28+
return;
29+
}
30+
}
31+
32+
parent::storeCurrentUrl($request, $session);
33+
}
34+
}

tests/Uploads/ImageTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,29 @@ public function test_system_images_remain_public_with_local_secure()
383383
}
384384
}
385385

386+
public function test_secure_images_not_tracked_in_session_history()
387+
{
388+
config()->set('filesystems.images', 'local_secure');
389+
$this->asEditor();
390+
$page = $this->entities->page();
391+
$result = $this->files->uploadGalleryImageToPage($this, $page);
392+
$expectedPath = storage_path($result['path']);
393+
$this->assertFileExists($expectedPath);
394+
395+
$this->get('/books');
396+
$this->assertEquals(url('/books'), session()->previousUrl());
397+
398+
$resp = $this->get($result['path']);
399+
$resp->assertOk();
400+
$resp->assertHeader('Content-Type', 'image/png');
401+
402+
$this->assertEquals(url('/books'), session()->previousUrl());
403+
404+
if (file_exists($expectedPath)) {
405+
unlink($expectedPath);
406+
}
407+
}
408+
386409
public function test_system_images_remain_public_with_local_secure_restricted()
387410
{
388411
config()->set('filesystems.images', 'local_secure_restricted');

0 commit comments

Comments
 (0)