3 namespace Tests\Uploads;
5 use BookStack\Entities\Models\Page;
6 use BookStack\Entities\Repos\PageRepo;
7 use BookStack\Entities\Tools\TrashCan;
8 use BookStack\Uploads\Attachment;
9 use BookStack\Uploads\AttachmentService;
10 use Illuminate\Http\UploadedFile;
13 class AttachmentTest extends TestCase
16 * Get a test file that can be uploaded.
18 protected function getTestFile(string $fileName): UploadedFile
20 return new UploadedFile(base_path('tests/test-data/test-file.txt'), $fileName, 'text/plain', null, true);
24 * Uploads a file with the given name.
26 protected function uploadFile(string $name, int $uploadedTo = 0): \Illuminate\Testing\TestResponse
28 $file = $this->getTestFile($name);
30 return $this->call('POST', '/attachments/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
34 * Create a new attachment.
36 protected function createAttachment(Page $page): Attachment
38 $this->post('attachments/link', [
39 'attachment_link_url' => 'https://example.com',
40 'attachment_link_name' => 'Example Attachment Link',
41 'attachment_link_uploaded_to' => $page->id,
44 return Attachment::query()->latest()->first();
48 * Create a new upload attachment from the given data.
50 protected function createUploadAttachment(Page $page, string $filename, string $content, string $mimeType): Attachment
53 $filePath = stream_get_meta_data($file)['uri'];
54 file_put_contents($filePath, $content);
55 $upload = new UploadedFile($filePath, $filename, $mimeType, null, true);
57 $this->call('POST', '/attachments/upload', ['uploaded_to' => $page->id], [], ['file' => $upload], []);
59 return $page->attachments()->latest()->firstOrFail();
63 * Delete all uploaded files.
64 * To assist with cleanup.
66 protected function deleteUploads()
68 $fileService = $this->app->make(AttachmentService::class);
69 foreach (Attachment::all() as $file) {
70 $fileService->deleteFile($file);
74 public function test_file_upload()
76 $page = $this->entities->page();
78 $admin = $this->getAdmin();
79 $fileName = 'upload_test_file.txt';
83 'uploaded_to' => $page->id,
86 'created_by' => $admin->id,
87 'updated_by' => $admin->id,
90 $upload = $this->uploadFile($fileName, $page->id);
91 $upload->assertStatus(200);
93 $attachment = Attachment::query()->orderBy('id', 'desc')->first();
94 $upload->assertJson($expectedResp);
96 $expectedResp['path'] = $attachment->path;
97 $this->assertDatabaseHas('attachments', $expectedResp);
99 $this->deleteUploads();
102 public function test_file_upload_does_not_use_filename()
104 $page = $this->entities->page();
105 $fileName = 'upload_test_file.txt';
107 $upload = $this->asAdmin()->uploadFile($fileName, $page->id);
108 $upload->assertStatus(200);
110 $attachment = Attachment::query()->orderBy('id', 'desc')->first();
111 $this->assertStringNotContainsString($fileName, $attachment->path);
112 $this->assertStringEndsWith('-txt', $attachment->path);
113 $this->deleteUploads();
116 public function test_file_display_and_access()
118 $page = $this->entities->page();
120 $fileName = 'upload_test_file.txt';
122 $upload = $this->uploadFile($fileName, $page->id);
123 $upload->assertStatus(200);
124 $attachment = Attachment::orderBy('id', 'desc')->take(1)->first();
126 $pageGet = $this->get($page->getUrl());
127 $pageGet->assertSeeText($fileName);
128 $pageGet->assertSee($attachment->getUrl());
130 $attachmentGet = $this->get($attachment->getUrl());
131 $content = $attachmentGet->streamedContent();
132 $this->assertStringContainsString('Hi, This is a test file for testing the upload process.', $content);
134 $this->deleteUploads();
137 public function test_attaching_link_to_page()
139 $page = $this->entities->page();
140 $admin = $this->getAdmin();
143 $linkReq = $this->call('POST', 'attachments/link', [
144 'attachment_link_url' => 'https://example.com',
145 'attachment_link_name' => 'Example Attachment Link',
146 'attachment_link_uploaded_to' => $page->id,
150 'path' => 'https://example.com',
151 'name' => 'Example Attachment Link',
152 'uploaded_to' => $page->id,
153 'created_by' => $admin->id,
154 'updated_by' => $admin->id,
160 $linkReq->assertStatus(200);
161 $this->assertDatabaseHas('attachments', $expectedData);
162 $attachment = Attachment::orderBy('id', 'desc')->take(1)->first();
164 $pageGet = $this->get($page->getUrl());
165 $pageGet->assertSeeText('Example Attachment Link');
166 $pageGet->assertSee($attachment->getUrl());
168 $attachmentGet = $this->get($attachment->getUrl());
169 $attachmentGet->assertRedirect('https://example.com');
171 $this->deleteUploads();
174 public function test_attachment_updating()
176 $page = $this->entities->page();
179 $attachment = $this->createAttachment($page);
180 $update = $this->call('PUT', 'attachments/' . $attachment->id, [
181 'attachment_edit_name' => 'My new attachment name',
182 'attachment_edit_url' => 'https://test.example.com',
186 'id' => $attachment->id,
187 'path' => 'https://test.example.com',
188 'name' => 'My new attachment name',
189 'uploaded_to' => $page->id,
192 $update->assertStatus(200);
193 $this->assertDatabaseHas('attachments', $expectedData);
195 $this->deleteUploads();
198 public function test_file_deletion()
200 $page = $this->entities->page();
202 $fileName = 'deletion_test.txt';
203 $this->uploadFile($fileName, $page->id);
205 $attachment = Attachment::query()->orderBy('id', 'desc')->first();
206 $filePath = storage_path($attachment->path);
207 $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist');
209 $attachment = Attachment::first();
210 $this->delete($attachment->getUrl());
212 $this->assertDatabaseMissing('attachments', [
215 $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
217 $this->deleteUploads();
220 public function test_attachment_deletion_on_page_deletion()
222 $page = $this->entities->page();
224 $fileName = 'deletion_test.txt';
225 $this->uploadFile($fileName, $page->id);
227 $attachment = Attachment::query()->orderBy('id', 'desc')->first();
228 $filePath = storage_path($attachment->path);
230 $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist');
231 $this->assertDatabaseHas('attachments', [
235 app(PageRepo::class)->destroy($page);
236 app(TrashCan::class)->empty();
238 $this->assertDatabaseMissing('attachments', [
241 $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
243 $this->deleteUploads();
246 public function test_attachment_access_without_permission_shows_404()
248 $admin = $this->getAdmin();
249 $viewer = $this->getViewer();
250 $page = $this->entities->page(); /** @var Page $page */
251 $this->actingAs($admin);
252 $fileName = 'permission_test.txt';
253 $this->uploadFile($fileName, $page->id);
254 $attachment = Attachment::orderBy('id', 'desc')->take(1)->first();
256 $this->entities->setPermissions($page, [], []);
258 $this->actingAs($viewer);
259 $attachmentGet = $this->get($attachment->getUrl());
260 $attachmentGet->assertStatus(404);
261 $attachmentGet->assertSee('Attachment not found');
263 $this->deleteUploads();
266 public function test_data_and_js_links_cannot_be_attached_to_a_page()
268 $page = $this->entities->page();
272 'javascript:alert("bunny")',
273 ' javascript:alert("bunny")',
274 'JavaScript:alert("bunny")',
275 "\t\n\t\nJavaScript:alert(\"bunny\")",
276 'data:text/html;<a></a>',
277 'Data:text/html;<a></a>',
278 'Data:text/html;<a></a>',
281 foreach ($badLinks as $badLink) {
282 $linkReq = $this->post('attachments/link', [
283 'attachment_link_url' => $badLink,
284 'attachment_link_name' => 'Example Attachment Link',
285 'attachment_link_uploaded_to' => $page->id,
287 $linkReq->assertStatus(422);
288 $this->assertDatabaseMissing('attachments', [
293 $attachment = $this->createAttachment($page);
295 foreach ($badLinks as $badLink) {
296 $linkReq = $this->put('attachments/' . $attachment->id, [
297 'attachment_edit_url' => $badLink,
298 'attachment_edit_name' => 'Example Attachment Link',
300 $linkReq->assertStatus(422);
301 $this->assertDatabaseMissing('attachments', [
307 public function test_file_access_with_open_query_param_provides_inline_response_with_correct_content_type()
309 $page = $this->entities->page();
311 $fileName = 'upload_test_file.txt';
313 $upload = $this->uploadFile($fileName, $page->id);
314 $upload->assertStatus(200);
315 $attachment = Attachment::query()->orderBy('id', 'desc')->take(1)->first();
317 $attachmentGet = $this->get($attachment->getUrl(true));
318 // http-foundation/Response does some 'fixing' of responses to add charsets to text responses.
319 $attachmentGet->assertHeader('Content-Type', 'text/plain; charset=UTF-8');
320 $attachmentGet->assertHeader('Content-Disposition', 'inline; filename="upload_test_file.txt"');
321 $attachmentGet->assertHeader('X-Content-Type-Options', 'nosniff');
323 $this->deleteUploads();
326 public function test_html_file_access_with_open_forces_plain_content_type()
328 $page = $this->entities->page();
331 $attachment = $this->createUploadAttachment($page, 'test_file.html', '<html></html><p>testing</p>', 'text/html');
333 $attachmentGet = $this->get($attachment->getUrl(true));
334 // http-foundation/Response does some 'fixing' of responses to add charsets to text responses.
335 $attachmentGet->assertHeader('Content-Type', 'text/plain; charset=UTF-8');
336 $attachmentGet->assertHeader('Content-Disposition', 'inline; filename="test_file.html"');
338 $this->deleteUploads();
341 public function test_file_upload_works_when_local_secure_restricted_is_in_use()
343 config()->set('filesystems.attachments', 'local_secure_restricted');
345 $page = $this->entities->page();
346 $fileName = 'upload_test_file.txt';
348 $upload = $this->asAdmin()->uploadFile($fileName, $page->id);
349 $upload->assertStatus(200);
351 $attachment = Attachment::query()->orderBy('id', 'desc')->where('uploaded_to', '=', $page->id)->first();
352 $this->assertFileExists(storage_path($attachment->path));
353 $this->deleteUploads();