Skip to content

Commit fddeb90

Browse files
committed
Attachments: Added page access check to attachment delete
Thanks to github.com/404-pkj for reporting.
1 parent 99a7046 commit fddeb90

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

app/Uploads/Controllers/AttachmentController.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ public function sortForPage(Request $request, int $pageId)
195195
$this->validate($request, [
196196
'order' => ['required', 'array'],
197197
]);
198+
198199
$page = $this->pageQueries->findVisibleByIdOrFail($pageId);
199200
$this->checkOwnablePermission(Permission::PageUpdate, $page);
200201

@@ -221,8 +222,6 @@ public function get(Request $request, string $attachmentId)
221222
throw new NotFoundException(trans('errors.attachment_not_found'));
222223
}
223224

224-
$this->checkOwnablePermission(Permission::PageView, $page);
225-
226225
if ($attachment->external) {
227226
return redirect($attachment->path);
228227
}
@@ -247,6 +246,13 @@ public function delete(string $attachmentId)
247246
{
248247
/** @var Attachment $attachment */
249248
$attachment = Attachment::query()->findOrFail($attachmentId);
249+
250+
try {
251+
$this->pageQueries->findVisibleByIdOrFail($attachment->uploaded_to);
252+
} catch (NotFoundException $exception) {
253+
throw new NotFoundException(trans('errors.attachment_not_found'));
254+
}
255+
250256
$this->checkOwnablePermission(Permission::AttachmentDelete, $attachment);
251257
$this->attachmentService->deleteFile($attachment);
252258

tests/Uploads/AttachmentTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use BookStack\Entities\Models\Page;
66
use BookStack\Entities\Repos\PageRepo;
77
use BookStack\Entities\Tools\TrashCan;
8+
use BookStack\Permissions\Permission;
89
use BookStack\Uploads\Attachment;
910
use Tests\TestCase;
1011

@@ -206,6 +207,21 @@ public function test_attachment_deletion_on_page_deletion()
206207
$this->files->deleteAllAttachmentFiles();
207208
}
208209

210+
public function test_attachment_deletion_requires_page_access()
211+
{
212+
$page = $this->entities->page();
213+
$attachment = Attachment::factory()->create(['uploaded_to' => $page->id]);
214+
$editor = $this->users->editor();
215+
216+
$this->permissions->disableEntityInheritedPermissions($page);
217+
$this->permissions->grantUserRolePermissions($editor, [Permission::AttachmentDeleteAll]);
218+
219+
$resp = $this->actingAs($editor)->delete($attachment->getUrl());
220+
$resp->assertNotFound();
221+
222+
$this->assertDatabaseHas('attachments', ['id' => $attachment->id]);
223+
}
224+
209225
public function test_attachment_access_without_permission_shows_404()
210226
{
211227
$admin = $this->users->admin();

0 commit comments

Comments
 (0)