Skip to content

Commit

Permalink
Updated showImage file serving to not be traversable
Browse files Browse the repository at this point in the history
For #3030
  • Loading branch information
ssddanbrown committed Oct 31, 2021
1 parent ae155d6 commit 43830a3
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 14 deletions.
13 changes: 9 additions & 4 deletions app/Http/Controllers/Images/ImageController.php
Expand Up @@ -7,25 +7,31 @@
use BookStack\Http\Controllers\Controller;
use BookStack\Uploads\Image;
use BookStack\Uploads\ImageRepo;
use BookStack\Uploads\ImageService;
use Exception;
use Illuminate\Filesystem\Filesystem as File;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\ValidationException;
use League\Flysystem\Util;

class ImageController extends Controller
{
protected $image;
protected $file;
protected $imageRepo;
protected $imageService;

/**
* ImageController constructor.
*/
public function __construct(Image $image, File $file, ImageRepo $imageRepo)
public function __construct(Image $image, File $file, ImageRepo $imageRepo, ImageService $imageService)
{
$this->image = $image;
$this->file = $file;
$this->imageRepo = $imageRepo;
$this->imageService = $imageService;
}

/**
Expand All @@ -35,14 +41,13 @@ public function __construct(Image $image, File $file, ImageRepo $imageRepo)
*/
public function showImage(string $path)
{
$path = storage_path('uploads/images/' . $path);
if (!file_exists($path)) {
if (!$this->imageService->pathExistsInLocalSecure($path)) {
throw (new NotFoundException(trans('errors.image_not_found')))
->setSubtitle(trans('errors.image_not_found_subtitle'))
->setDetails(trans('errors.image_not_found_details'));
}

return response()->file($path);
return $this->imageService->streamImageFromStorageResponse('gallery', $path);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions app/Uploads/AttachmentService.php
Expand Up @@ -27,7 +27,7 @@ public function __construct(FileSystem $fileSystem)
/**
* Get the storage that will be used for storing files.
*/
protected function getStorage(): FileSystemInstance
protected function getStorageDisk(): FileSystemInstance
{
return $this->fileSystem->disk($this->getStorageDiskName());
}
Expand Down Expand Up @@ -70,7 +70,7 @@ protected function adjustPathForStorageDisk(string $path): string
*/
public function getAttachmentFromStorage(Attachment $attachment): string
{
return $this->getStorage()->get($this->adjustPathForStorageDisk($attachment->path));
return $this->getStorageDisk()->get($this->adjustPathForStorageDisk($attachment->path));
}

/**
Expand Down Expand Up @@ -195,7 +195,7 @@ public function deleteFile(Attachment $attachment)
*/
protected function deleteFileInStorage(Attachment $attachment)
{
$storage = $this->getStorage();
$storage = $this->getStorageDisk();
$dirPath = $this->adjustPathForStorageDisk(dirname($attachment->path));

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

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

$uploadFileName = Str::random(16) . '.' . $uploadedFile->getClientOriginalExtension();
Expand Down
47 changes: 41 additions & 6 deletions app/Uploads/ImageService.php
Expand Up @@ -16,6 +16,7 @@
use Intervention\Image\ImageManager;
use League\Flysystem\Util;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\StreamedResponse;

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

/**
* Check if local secure image storage (Fetched behind authentication)
* is currently active in the instance.
*/
protected function usingSecureImages(): bool
{
return $this->getStorageDiskName('gallery') === 'local_secure_images';
}

/**
* Change the originally provided path to fit any disk-specific requirements.
* This also ensures the path is kept to the expected root folders.
Expand Down Expand Up @@ -126,7 +136,7 @@ public function saveNewFromBase64Uri(string $base64Uri, string $name, string $ty
*/
public function saveNew(string $imageName, string $imageData, string $type, int $uploadedTo = 0): Image
{
$storage = $this->getStorage($type);
$storage = $this->getStorageDisk($type);
$secureUploads = setting('app-secure-images');
$fileName = $this->cleanImageFileName($imageName);

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

$storage = $this->getStorage($image->type);
$storage = $this->getStorageDisk($image->type);
if ($storage->exists($this->adjustPathForStorageDisk($thumbFilePath, $image->type))) {
return $this->getPublicUrl($thumbFilePath);
}
Expand Down Expand Up @@ -307,7 +317,7 @@ protected function resizeImage(string $imageData, $width = 220, $height = null,
*/
public function getImageData(Image $image): string
{
$storage = $this->getStorage();
$storage = $this->getStorageDisk();

return $storage->get($this->adjustPathForStorageDisk($image->path, $image->type));
}
Expand All @@ -330,7 +340,7 @@ public function destroy(Image $image)
protected function destroyImagesFromPath(string $path, string $imageType): bool
{
$path = $this->adjustPathForStorageDisk($path, $imageType);
$storage = $this->getStorage($imageType);
$storage = $this->getStorageDisk($imageType);

$imageFolder = dirname($path);
$imageFileName = basename($path);
Expand Down Expand Up @@ -417,7 +427,7 @@ public function imageUriToBase64(string $uri): ?string
}

$storagePath = $this->adjustPathForStorageDisk($storagePath);
$storage = $this->getStorage();
$storage = $this->getStorageDisk();
$imageData = null;
if ($storage->exists($storagePath)) {
$imageData = $storage->get($storagePath);
Expand All @@ -435,6 +445,31 @@ public function imageUriToBase64(string $uri): ?string
return 'data:image/' . $extension . ';base64,' . base64_encode($imageData);
}

/**
* Check if the given path exists in the local secure image system.
* Returns false if local_secure is not in use.
*/
public function pathExistsInLocalSecure(string $imagePath): bool
{
$disk = $this->getStorageDisk('gallery');

// Check local_secure is active
return $this->usingSecureImages()
// Check the image file exists
&& $disk->exists($imagePath)
// Check the file is likely an image file
&& strpos($disk->getMimetype($imagePath), 'image/') === 0;
}

/**
* For the given path, if existing, provide a response that will stream the image contents.
*/
public function streamImageFromStorageResponse(string $imageType, string $path): StreamedResponse
{
$disk = $this->getStorageDisk($imageType);
return $disk->response($path);
}

/**
* Get a storage path for the given image URL.
* Ensures the path will start with "uploads/images".
Expand Down
30 changes: 30 additions & 0 deletions tests/Uploads/ImageTest.php
Expand Up @@ -241,6 +241,36 @@ public function test_secure_images_uploads_to_correct_place()
}
}

public function test_secure_image_paths_traversal_causes_500()
{
config()->set('filesystems.images', 'local_secure');
$this->asEditor();

$resp = $this->get('/uploads/images/../../logs/laravel.log');
$resp->assertStatus(500);
}

public function test_secure_image_paths_traversal_on_non_secure_images_causes_404()
{
config()->set('filesystems.images', 'local');
$this->asEditor();

$resp = $this->get('/uploads/images/../../logs/laravel.log');
$resp->assertStatus(404);
}

public function test_secure_image_paths_dont_serve_non_images()
{
config()->set('filesystems.images', 'local_secure');
$this->asEditor();

$testFilePath = storage_path('/uploads/images/testing.txt');
file_put_contents($testFilePath, 'hello from test_secure_image_paths_dont_serve_non_images');

$resp = $this->get('/uploads/images/testing.txt');
$resp->assertStatus(404);
}

public function test_secure_images_included_in_exports()
{
config()->set('filesystems.images', 'local_secure');
Expand Down

0 comments on commit 43830a3

Please sign in to comment.