Skip to content

Commit 43830a3

Browse files
committed
Updated showImage file serving to not be traversable
For #3030
1 parent ae155d6 commit 43830a3

File tree

4 files changed

+84
-14
lines changed

4 files changed

+84
-14
lines changed

app/Http/Controllers/Images/ImageController.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,31 @@
77
use BookStack\Http\Controllers\Controller;
88
use BookStack\Uploads\Image;
99
use BookStack\Uploads\ImageRepo;
10+
use BookStack\Uploads\ImageService;
1011
use Exception;
1112
use Illuminate\Filesystem\Filesystem as File;
13+
use Illuminate\Filesystem\FilesystemAdapter;
1214
use Illuminate\Http\Request;
15+
use Illuminate\Support\Facades\Storage;
1316
use Illuminate\Validation\ValidationException;
17+
use League\Flysystem\Util;
1418

1519
class ImageController extends Controller
1620
{
1721
protected $image;
1822
protected $file;
1923
protected $imageRepo;
24+
protected $imageService;
2025

2126
/**
2227
* ImageController constructor.
2328
*/
24-
public function __construct(Image $image, File $file, ImageRepo $imageRepo)
29+
public function __construct(Image $image, File $file, ImageRepo $imageRepo, ImageService $imageService)
2530
{
2631
$this->image = $image;
2732
$this->file = $file;
2833
$this->imageRepo = $imageRepo;
34+
$this->imageService = $imageService;
2935
}
3036

3137
/**
@@ -35,14 +41,13 @@ public function __construct(Image $image, File $file, ImageRepo $imageRepo)
3541
*/
3642
public function showImage(string $path)
3743
{
38-
$path = storage_path('uploads/images/' . $path);
39-
if (!file_exists($path)) {
44+
if (!$this->imageService->pathExistsInLocalSecure($path)) {
4045
throw (new NotFoundException(trans('errors.image_not_found')))
4146
->setSubtitle(trans('errors.image_not_found_subtitle'))
4247
->setDetails(trans('errors.image_not_found_details'));
4348
}
4449

45-
return response()->file($path);
50+
return $this->imageService->streamImageFromStorageResponse('gallery', $path);
4651
}
4752

4853
/**

app/Uploads/AttachmentService.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct(FileSystem $fileSystem)
2727
/**
2828
* Get the storage that will be used for storing files.
2929
*/
30-
protected function getStorage(): FileSystemInstance
30+
protected function getStorageDisk(): FileSystemInstance
3131
{
3232
return $this->fileSystem->disk($this->getStorageDiskName());
3333
}
@@ -70,7 +70,7 @@ protected function adjustPathForStorageDisk(string $path): string
7070
*/
7171
public function getAttachmentFromStorage(Attachment $attachment): string
7272
{
73-
return $this->getStorage()->get($this->adjustPathForStorageDisk($attachment->path));
73+
return $this->getStorageDisk()->get($this->adjustPathForStorageDisk($attachment->path));
7474
}
7575

7676
/**
@@ -195,7 +195,7 @@ public function deleteFile(Attachment $attachment)
195195
*/
196196
protected function deleteFileInStorage(Attachment $attachment)
197197
{
198-
$storage = $this->getStorage();
198+
$storage = $this->getStorageDisk();
199199
$dirPath = $this->adjustPathForStorageDisk(dirname($attachment->path));
200200

201201
$storage->delete($this->adjustPathForStorageDisk($attachment->path));
@@ -213,7 +213,7 @@ protected function putFileInStorage(UploadedFile $uploadedFile): string
213213
{
214214
$attachmentData = file_get_contents($uploadedFile->getRealPath());
215215

216-
$storage = $this->getStorage();
216+
$storage = $this->getStorageDisk();
217217
$basePath = 'uploads/files/' . date('Y-m-M') . '/';
218218

219219
$uploadFileName = Str::random(16) . '.' . $uploadedFile->getClientOriginalExtension();

app/Uploads/ImageService.php

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Intervention\Image\ImageManager;
1717
use League\Flysystem\Util;
1818
use Symfony\Component\HttpFoundation\File\UploadedFile;
19+
use Symfony\Component\HttpFoundation\StreamedResponse;
1920

2021
class ImageService
2122
{
@@ -39,11 +40,20 @@ public function __construct(Image $image, ImageManager $imageTool, FileSystem $f
3940
/**
4041
* Get the storage that will be used for storing images.
4142
*/
42-
protected function getStorage(string $imageType = ''): FileSystemInstance
43+
protected function getStorageDisk(string $imageType = ''): FileSystemInstance
4344
{
4445
return $this->fileSystem->disk($this->getStorageDiskName($imageType));
4546
}
4647

48+
/**
49+
* Check if local secure image storage (Fetched behind authentication)
50+
* is currently active in the instance.
51+
*/
52+
protected function usingSecureImages(): bool
53+
{
54+
return $this->getStorageDiskName('gallery') === 'local_secure_images';
55+
}
56+
4757
/**
4858
* Change the originally provided path to fit any disk-specific requirements.
4959
* This also ensures the path is kept to the expected root folders.
@@ -126,7 +136,7 @@ public function saveNewFromBase64Uri(string $base64Uri, string $name, string $ty
126136
*/
127137
public function saveNew(string $imageName, string $imageData, string $type, int $uploadedTo = 0): Image
128138
{
129-
$storage = $this->getStorage($type);
139+
$storage = $this->getStorageDisk($type);
130140
$secureUploads = setting('app-secure-images');
131141
$fileName = $this->cleanImageFileName($imageName);
132142

@@ -243,7 +253,7 @@ public function getThumbnail(Image $image, $width = 220, $height = 220, $keepRat
243253
return $this->getPublicUrl($thumbFilePath);
244254
}
245255

246-
$storage = $this->getStorage($image->type);
256+
$storage = $this->getStorageDisk($image->type);
247257
if ($storage->exists($this->adjustPathForStorageDisk($thumbFilePath, $image->type))) {
248258
return $this->getPublicUrl($thumbFilePath);
249259
}
@@ -307,7 +317,7 @@ protected function resizeImage(string $imageData, $width = 220, $height = null,
307317
*/
308318
public function getImageData(Image $image): string
309319
{
310-
$storage = $this->getStorage();
320+
$storage = $this->getStorageDisk();
311321

312322
return $storage->get($this->adjustPathForStorageDisk($image->path, $image->type));
313323
}
@@ -330,7 +340,7 @@ public function destroy(Image $image)
330340
protected function destroyImagesFromPath(string $path, string $imageType): bool
331341
{
332342
$path = $this->adjustPathForStorageDisk($path, $imageType);
333-
$storage = $this->getStorage($imageType);
343+
$storage = $this->getStorageDisk($imageType);
334344

335345
$imageFolder = dirname($path);
336346
$imageFileName = basename($path);
@@ -417,7 +427,7 @@ public function imageUriToBase64(string $uri): ?string
417427
}
418428

419429
$storagePath = $this->adjustPathForStorageDisk($storagePath);
420-
$storage = $this->getStorage();
430+
$storage = $this->getStorageDisk();
421431
$imageData = null;
422432
if ($storage->exists($storagePath)) {
423433
$imageData = $storage->get($storagePath);
@@ -435,6 +445,31 @@ public function imageUriToBase64(string $uri): ?string
435445
return 'data:image/' . $extension . ';base64,' . base64_encode($imageData);
436446
}
437447

448+
/**
449+
* Check if the given path exists in the local secure image system.
450+
* Returns false if local_secure is not in use.
451+
*/
452+
public function pathExistsInLocalSecure(string $imagePath): bool
453+
{
454+
$disk = $this->getStorageDisk('gallery');
455+
456+
// Check local_secure is active
457+
return $this->usingSecureImages()
458+
// Check the image file exists
459+
&& $disk->exists($imagePath)
460+
// Check the file is likely an image file
461+
&& strpos($disk->getMimetype($imagePath), 'image/') === 0;
462+
}
463+
464+
/**
465+
* For the given path, if existing, provide a response that will stream the image contents.
466+
*/
467+
public function streamImageFromStorageResponse(string $imageType, string $path): StreamedResponse
468+
{
469+
$disk = $this->getStorageDisk($imageType);
470+
return $disk->response($path);
471+
}
472+
438473
/**
439474
* Get a storage path for the given image URL.
440475
* Ensures the path will start with "uploads/images".

tests/Uploads/ImageTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,36 @@ public function test_secure_images_uploads_to_correct_place()
241241
}
242242
}
243243

244+
public function test_secure_image_paths_traversal_causes_500()
245+
{
246+
config()->set('filesystems.images', 'local_secure');
247+
$this->asEditor();
248+
249+
$resp = $this->get('/uploads/images/../../logs/laravel.log');
250+
$resp->assertStatus(500);
251+
}
252+
253+
public function test_secure_image_paths_traversal_on_non_secure_images_causes_404()
254+
{
255+
config()->set('filesystems.images', 'local');
256+
$this->asEditor();
257+
258+
$resp = $this->get('/uploads/images/../../logs/laravel.log');
259+
$resp->assertStatus(404);
260+
}
261+
262+
public function test_secure_image_paths_dont_serve_non_images()
263+
{
264+
config()->set('filesystems.images', 'local_secure');
265+
$this->asEditor();
266+
267+
$testFilePath = storage_path('/uploads/images/testing.txt');
268+
file_put_contents($testFilePath, 'hello from test_secure_image_paths_dont_serve_non_images');
269+
270+
$resp = $this->get('/uploads/images/testing.txt');
271+
$resp->assertStatus(404);
272+
}
273+
244274
public function test_secure_images_included_in_exports()
245275
{
246276
config()->set('filesystems.images', 'local_secure');

0 commit comments

Comments
 (0)