X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/f84bf8e883e8e5bd9a24b908e2f90a2742d36d19..refs/pull/2393/head:/tests/Uploads/AttachmentTest.php diff --git a/tests/Uploads/AttachmentTest.php b/tests/Uploads/AttachmentTest.php index e98a90b35..1ca9ea23b 100644 --- a/tests/Uploads/AttachmentTest.php +++ b/tests/Uploads/AttachmentTest.php @@ -1,42 +1,56 @@ getTestFile($name); return $this->call('POST', '/attachments/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []); } + /** + * Create a new attachment + */ + protected function createAttachment(Page $page): Attachment + { + $this->post('attachments/link', [ + 'attachment_link_url' => 'https://example.com', + 'attachment_link_name' => 'Example Attachment Link', + 'attachment_link_uploaded_to' => $page->id, + ]); + + return Attachment::query()->latest()->first(); + } + /** * Delete all uploaded files. * To assist with cleanup. */ protected function deleteUploads() { - $fileService = $this->app->make(\BookStack\Uploads\AttachmentService::class); - foreach (\BookStack\Uploads\Attachment::all() as $file) { + $fileService = $this->app->make(AttachmentService::class); + foreach (Attachment::all() as $file) { $fileService->deleteFile($file); } } @@ -110,12 +124,12 @@ class AttachmentTest extends TestCase $this->asAdmin(); $linkReq = $this->call('POST', 'attachments/link', [ - 'link' => 'https://example.com', - 'name' => 'Example Attachment Link', - 'uploaded_to' => $page->id, + 'attachment_link_url' => 'https://example.com', + 'attachment_link_name' => 'Example Attachment Link', + 'attachment_link_uploaded_to' => $page->id, ]); - $expectedResp = [ + $expectedData = [ 'path' => 'https://example.com', 'name' => 'Example Attachment Link', 'uploaded_to' => $page->id, @@ -127,8 +141,7 @@ class AttachmentTest extends TestCase ]; $linkReq->assertStatus(200); - $linkReq->assertJson($expectedResp); - $this->assertDatabaseHas('attachments', $expectedResp); + $this->assertDatabaseHas('attachments', $expectedData); $attachment = Attachment::orderBy('id', 'desc')->take(1)->first(); $pageGet = $this->get($page->getUrl()); @@ -146,29 +159,21 @@ class AttachmentTest extends TestCase $page = Page::first(); $this->asAdmin(); - $this->call('POST', 'attachments/link', [ - 'link' => 'https://example.com', - 'name' => 'Example Attachment Link', - 'uploaded_to' => $page->id, + $attachment = $this->createAttachment($page); + $update = $this->call('PUT', 'attachments/' . $attachment->id, [ + 'attachment_edit_name' => 'My new attachment name', + 'attachment_edit_url' => 'https://test.example.com' ]); - $attachmentId = \BookStack\Uploads\Attachment::first()->id; - - $update = $this->call('PUT', 'attachments/' . $attachmentId, [ - 'uploaded_to' => $page->id, - 'name' => 'My new attachment name', - 'link' => 'https://test.example.com' - ]); - - $expectedResp = [ + $expectedData = [ + 'id' => $attachment->id, 'path' => 'https://test.example.com', 'name' => 'My new attachment name', 'uploaded_to' => $page->id ]; $update->assertStatus(200); - $update->assertJson($expectedResp); - $this->assertDatabaseHas('attachments', $expectedResp); + $this->assertDatabaseHas('attachments', $expectedData); $this->deleteUploads(); } @@ -184,7 +189,7 @@ class AttachmentTest extends TestCase $filePath = storage_path($attachment->path); $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist'); - $attachment = \BookStack\Uploads\Attachment::first(); + $attachment = Attachment::first(); $this->delete($attachment->getUrl()); $this->assertDatabaseMissing('attachments', [ @@ -210,7 +215,8 @@ class AttachmentTest extends TestCase 'name' => $fileName ]); - $this->call('DELETE', $page->getUrl()); + app(PageRepo::class)->destroy($page); + app(TrashCan::class)->empty(); $this->assertDatabaseMissing('attachments', [ 'name' => $fileName @@ -244,4 +250,45 @@ class AttachmentTest extends TestCase $this->deleteUploads(); } + + public function test_data_and_js_links_cannot_be_attached_to_a_page() + { + $page = Page::first(); + $this->asAdmin(); + + $badLinks = [ + 'javascript:alert("bunny")', + ' javascript:alert("bunny")', + 'JavaScript:alert("bunny")', + "\t\n\t\nJavaScript:alert(\"bunny\")", + "data:text/html;", + "Data:text/html;", + "Data:text/html;", + ]; + + foreach ($badLinks as $badLink) { + $linkReq = $this->post('attachments/link', [ + 'attachment_link_url' => $badLink, + 'attachment_link_name' => 'Example Attachment Link', + 'attachment_link_uploaded_to' => $page->id, + ]); + $linkReq->assertStatus(422); + $this->assertDatabaseMissing('attachments', [ + 'path' => $badLink, + ]); + } + + $attachment = $this->createAttachment($page); + + foreach ($badLinks as $badLink) { + $linkReq = $this->put('attachments/' . $attachment->id, [ + 'attachment_edit_url' => $badLink, + 'attachment_edit_name' => 'Example Attachment Link', + ]); + $linkReq->assertStatus(422); + $this->assertDatabaseMissing('attachments', [ + 'path' => $badLink, + ]); + } + } }