Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions app/Uploads/ImageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public function pathAccessible(string $imagePath): bool
return false;
}

if ($this->storage->usingSecureImages() && user()->isGuest()) {
if ($this->blockedBySecureImages()) {
return false;
}

Expand All @@ -280,13 +280,24 @@ public function imageAccessible(Image $image): bool
return false;
}

if ($this->storage->usingSecureImages() && user()->isGuest()) {
if ($this->blockedBySecureImages()) {
return false;
}

return $this->imageFileExists($image->path, $image->type);
}

/**
* Check if the current user should be blocked from accessing images based on if secure images are enabled
* and if public access is enabled for the application.
*/
protected function blockedBySecureImages(): bool
{
$enforced = $this->storage->usingSecureImages() && !setting('app-public');

return $enforced && user()->isGuest();
}

/**
* Check if the given image path exists for the given image type and that it is likely an image file.
*/
Expand Down
2 changes: 1 addition & 1 deletion app/Uploads/ImageStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected function getDiskName(string $imageType): string
return 'local';
}

// Rename local_secure options to get our image specific storage driver which
// Rename local_secure options to get our image-specific storage driver, which
// is scoped to the relevant image directories.
if ($localSecureInUse) {
return 'local_secure_images';
Expand Down
54 changes: 27 additions & 27 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions tests/Uploads/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use BookStack\Entities\Repos\PageRepo;
use BookStack\Uploads\Image;
use BookStack\Uploads\ImageService;
use BookStack\Uploads\UserAvatars;
use BookStack\Users\Models\Role;
use Illuminate\Support\Str;
use Tests\TestCase;

Expand Down Expand Up @@ -467,6 +469,26 @@ public function test_system_images_remain_public_with_local_secure_restricted()
}
}

public function test_avatar_images_visible_only_when_public_access_enabled_with_local_secure_restricted()
{
config()->set('filesystems.images', 'local_secure_restricted');
$user = $this->users->admin();
$avatars = $this->app->make(UserAvatars::class);
$avatars->assignToUserFromExistingData($user, $this->files->pngImageData(), 'png');

$avatarUrl = $user->getAvatar();

$resp = $this->get($avatarUrl);
$resp->assertRedirect('/login');

$this->permissions->makeAppPublic();

$resp = $this->get($avatarUrl);
$resp->assertOk();

$this->files->deleteAtRelativePath($user->avatar->path);
}

public function test_secure_restricted_images_inaccessible_without_relation_permission()
{
config()->set('filesystems.images', 'local_secure_restricted');
Expand All @@ -491,6 +513,38 @@ public function test_secure_restricted_images_inaccessible_without_relation_perm
}
}

public function test_secure_restricted_images_accessible_with_public_guest_access()
{
config()->set('filesystems.images', 'local_secure_restricted');
$this->permissions->makeAppPublic();

$this->asEditor();
$page = $this->entities->page();
$this->files->uploadGalleryImageToPage($this, $page);
$image = Image::query()->where('type', '=', 'gallery')
->where('uploaded_to', '=', $page->id)
->first();

$expectedUrl = url($image->path);
$expectedPath = storage_path($image->path);
auth()->logout();

$this->get($expectedUrl)->assertOk();

$this->permissions->setEntityPermissions($page, [], []);

$resp = $this->get($expectedUrl);
$resp->assertNotFound();

$this->permissions->setEntityPermissions($page, ['view'], [Role::getSystemRole('public')]);

$this->get($expectedUrl)->assertOk();

if (file_exists($expectedPath)) {
unlink($expectedPath);
}
}

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