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;
11 class AttachmentTest extends TestCase
13 public function test_file_upload()
15 $page = $this->entities->page();
17 $admin = $this->users->admin();
18 $fileName = 'upload_test_file.txt';
22 'uploaded_to' => $page->id,
25 'created_by' => $admin->id,
26 'updated_by' => $admin->id,
29 $upload = $this->files->uploadAttachmentFile($this, $fileName, $page->id);
30 $upload->assertStatus(200);
32 $attachment = Attachment::query()->orderBy('id', 'desc')->first();
33 $upload->assertJson($expectedResp);
35 $expectedResp['path'] = $attachment->path;
36 $this->assertDatabaseHas('attachments', $expectedResp);
38 $this->files->deleteAllAttachmentFiles();
41 public function test_file_upload_does_not_use_filename()
43 $page = $this->entities->page();
44 $fileName = 'upload_test_file.txt';
47 $upload = $this->files->uploadAttachmentFile($this, $fileName, $page->id);
48 $upload->assertStatus(200);
50 $attachment = Attachment::query()->orderBy('id', 'desc')->first();
51 $this->assertStringNotContainsString($fileName, $attachment->path);
52 $this->assertStringEndsWith('-txt', $attachment->path);
53 $this->files->deleteAllAttachmentFiles();
56 public function test_file_display_and_access()
58 $page = $this->entities->page();
60 $fileName = 'upload_test_file.txt';
62 $upload = $this->files->uploadAttachmentFile($this, $fileName, $page->id);
63 $upload->assertStatus(200);
64 $attachment = Attachment::orderBy('id', 'desc')->take(1)->first();
66 $pageGet = $this->get($page->getUrl());
67 $pageGet->assertSeeText($fileName);
68 $pageGet->assertSee($attachment->getUrl());
70 $attachmentGet = $this->get($attachment->getUrl());
71 $content = $attachmentGet->streamedContent();
72 $this->assertStringContainsString('Hi, This is a test file for testing the upload process.', $content);
74 $this->files->deleteAllAttachmentFiles();
77 public function test_attaching_link_to_page()
79 $page = $this->entities->page();
80 $admin = $this->users->admin();
83 $linkReq = $this->call('POST', 'attachments/link', [
84 'attachment_link_url' => 'https://example.com',
85 'attachment_link_name' => 'Example Attachment Link',
86 'attachment_link_uploaded_to' => $page->id,
90 'path' => 'https://example.com',
91 'name' => 'Example Attachment Link',
92 'uploaded_to' => $page->id,
93 'created_by' => $admin->id,
94 'updated_by' => $admin->id,
100 $linkReq->assertStatus(200);
101 $this->assertDatabaseHas('attachments', $expectedData);
102 $attachment = Attachment::orderBy('id', 'desc')->take(1)->first();
104 $pageGet = $this->get($page->getUrl());
105 $pageGet->assertSeeText('Example Attachment Link');
106 $pageGet->assertSee($attachment->getUrl());
108 $attachmentGet = $this->get($attachment->getUrl());
109 $attachmentGet->assertRedirect('https://example.com');
111 $this->files->deleteAllAttachmentFiles();
114 public function test_attaching_long_links_to_a_page()
116 $page = $this->entities->page();
118 $link = 'https://example.com?query=' . str_repeat('catsIScool', 195);
119 $linkReq = $this->asAdmin()->post('attachments/link', [
120 'attachment_link_url' => $link,
121 'attachment_link_name' => 'Example Attachment Link',
122 'attachment_link_uploaded_to' => $page->id,
125 $linkReq->assertStatus(200);
126 $this->assertDatabaseHas('attachments', [
127 'uploaded_to' => $page->id,
132 $attachment = $page->attachments()->where('external', '=', true)->first();
133 $resp = $this->get($attachment->getUrl());
134 $resp->assertRedirect($link);
137 public function test_attachment_updating()
139 $page = $this->entities->page();
142 $attachment = Attachment::factory()->create(['uploaded_to' => $page->id]);
143 $update = $this->call('PUT', 'attachments/' . $attachment->id, [
144 'attachment_edit_name' => 'My new attachment name',
145 'attachment_edit_url' => 'https://test.example.com',
149 'id' => $attachment->id,
150 'path' => 'https://test.example.com',
151 'name' => 'My new attachment name',
152 'uploaded_to' => $page->id,
155 $update->assertStatus(200);
156 $this->assertDatabaseHas('attachments', $expectedData);
158 $this->files->deleteAllAttachmentFiles();
161 public function test_file_deletion()
163 $page = $this->entities->page();
165 $fileName = 'deletion_test.txt';
166 $this->files->uploadAttachmentFile($this, $fileName, $page->id);
168 $attachment = Attachment::query()->orderBy('id', 'desc')->first();
169 $filePath = storage_path($attachment->path);
170 $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist');
172 $attachment = Attachment::first();
173 $this->delete($attachment->getUrl());
175 $this->assertDatabaseMissing('attachments', [
178 $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
180 $this->files->deleteAllAttachmentFiles();
183 public function test_attachment_deletion_on_page_deletion()
185 $page = $this->entities->page();
187 $fileName = 'deletion_test.txt';
188 $this->files->uploadAttachmentFile($this, $fileName, $page->id);
190 $attachment = Attachment::query()->orderBy('id', 'desc')->first();
191 $filePath = storage_path($attachment->path);
193 $this->assertTrue(file_exists($filePath), 'File at path ' . $filePath . ' does not exist');
194 $this->assertDatabaseHas('attachments', [
198 app(PageRepo::class)->destroy($page);
199 app(TrashCan::class)->empty();
201 $this->assertDatabaseMissing('attachments', [
204 $this->assertFalse(file_exists($filePath), 'File at path ' . $filePath . ' was not deleted as expected');
206 $this->files->deleteAllAttachmentFiles();
209 public function test_attachment_access_without_permission_shows_404()
211 $admin = $this->users->admin();
212 $viewer = $this->users->viewer();
213 $page = $this->entities->page(); /** @var Page $page */
214 $this->actingAs($admin);
215 $fileName = 'permission_test.txt';
216 $this->files->uploadAttachmentFile($this, $fileName, $page->id);
217 $attachment = Attachment::orderBy('id', 'desc')->take(1)->first();
219 $this->permissions->setEntityPermissions($page, [], []);
221 $this->actingAs($viewer);
222 $attachmentGet = $this->get($attachment->getUrl());
223 $attachmentGet->assertStatus(404);
224 $attachmentGet->assertSee('Attachment not found');
226 $this->files->deleteAllAttachmentFiles();
229 public function test_data_and_js_links_cannot_be_attached_to_a_page()
231 $page = $this->entities->page();
235 'javascript:alert("bunny")',
236 ' javascript:alert("bunny")',
237 'JavaScript:alert("bunny")',
238 "\t\n\t\nJavaScript:alert(\"bunny\")",
239 'data:text/html;<a></a>',
240 'Data:text/html;<a></a>',
241 'Data:text/html;<a></a>',
244 foreach ($badLinks as $badLink) {
245 $linkReq = $this->post('attachments/link', [
246 'attachment_link_url' => $badLink,
247 'attachment_link_name' => 'Example Attachment Link',
248 'attachment_link_uploaded_to' => $page->id,
250 $linkReq->assertStatus(422);
251 $this->assertDatabaseMissing('attachments', [
256 $attachment = Attachment::factory()->create(['uploaded_to' => $page->id]);
258 foreach ($badLinks as $badLink) {
259 $linkReq = $this->put('attachments/' . $attachment->id, [
260 'attachment_edit_url' => $badLink,
261 'attachment_edit_name' => 'Example Attachment Link',
263 $linkReq->assertStatus(422);
264 $this->assertDatabaseMissing('attachments', [
270 public function test_file_access_with_open_query_param_provides_inline_response_with_correct_content_type()
272 $page = $this->entities->page();
274 $fileName = 'upload_test_file.txt';
276 $upload = $this->files->uploadAttachmentFile($this, $fileName, $page->id);
277 $upload->assertStatus(200);
278 $attachment = Attachment::query()->orderBy('id', 'desc')->take(1)->first();
280 $attachmentGet = $this->get($attachment->getUrl(true));
281 // http-foundation/Response does some 'fixing' of responses to add charsets to text responses.
282 $attachmentGet->assertHeader('Content-Type', 'text/plain; charset=UTF-8');
283 $attachmentGet->assertHeader('Content-Disposition', 'inline; filename="upload_test_file.txt"');
284 $attachmentGet->assertHeader('X-Content-Type-Options', 'nosniff');
286 $this->files->deleteAllAttachmentFiles();
289 public function test_html_file_access_with_open_forces_plain_content_type()
291 $page = $this->entities->page();
294 $attachment = $this->files->uploadAttachmentDataToPage($this, $page, 'test_file.html', '<html></html><p>testing</p>', 'text/html');
296 $attachmentGet = $this->get($attachment->getUrl(true));
297 // http-foundation/Response does some 'fixing' of responses to add charsets to text responses.
298 $attachmentGet->assertHeader('Content-Type', 'text/plain; charset=UTF-8');
299 $attachmentGet->assertHeader('Content-Disposition', 'inline; filename="test_file.html"');
301 $this->files->deleteAllAttachmentFiles();
304 public function test_file_upload_works_when_local_secure_restricted_is_in_use()
306 config()->set('filesystems.attachments', 'local_secure_restricted');
308 $page = $this->entities->page();
309 $fileName = 'upload_test_file.txt';
312 $upload = $this->files->uploadAttachmentFile($this, $fileName, $page->id);
313 $upload->assertStatus(200);
315 $attachment = Attachment::query()->orderBy('id', 'desc')->where('uploaded_to', '=', $page->id)->first();
316 $this->assertFileExists(storage_path($attachment->path));
317 $this->files->deleteAllAttachmentFiles();
320 public function test_file_get_range_access()
322 $page = $this->entities->page();
324 $attachment = $this->files->uploadAttachmentDataToPage($this, $page, 'my_text.txt', 'abc123456', 'text/plain');
327 $resp = $this->get($attachment->getUrl(), ['Range' => 'bytes=3-5']);
328 $resp->assertStatus(206);
329 $resp->assertStreamedContent('123');
330 $resp->assertHeader('Content-Length', '3');
331 $resp->assertHeader('Content-Range', 'bytes 3-5/9');
334 $resp = $this->get($attachment->getUrl(true), ['Range' => 'bytes=5-7']);
335 $resp->assertStatus(206);
336 $resp->assertStreamedContent('345');
337 $resp->assertHeader('Content-Length', '3');
338 $resp->assertHeader('Content-Range', 'bytes 5-7/9');
340 $this->files->deleteAllAttachmentFiles();
343 public function test_file_head_range_returns_no_content()
345 $page = $this->entities->page();
347 $attachment = $this->files->uploadAttachmentDataToPage($this, $page, 'my_text.txt', 'abc123456', 'text/plain');
349 $resp = $this->head($attachment->getUrl(), ['Range' => 'bytes=0-9']);
350 $resp->assertStreamedContent('');
351 $resp->assertHeader('Content-Length', '9');
352 $resp->assertStatus(200);
354 $this->files->deleteAllAttachmentFiles();
357 public function test_file_head_range_edge_cases()
359 $page = $this->entities->page();
362 // Mime-type "sniffing" happens on first 2k bytes, hence this content (2005 bytes)
363 $content = '01234' . str_repeat('a', 1990) . '0123456789';
364 $attachment = $this->files->uploadAttachmentDataToPage($this, $page, 'my_text.txt', $content, 'text/plain');
366 // Test for both inline and download attachment serving
367 foreach ([true, false] as $isInline) {
369 $resp = $this->get($attachment->getUrl($isInline), ['Range' => 'bytes=5-']);
370 $resp->assertStreamedContent(substr($content, 5));
371 $resp->assertHeader('Content-Length', '2000');
372 $resp->assertHeader('Content-Range', 'bytes 5-2004/2005');
373 $resp->assertStatus(206);
376 $resp = $this->get($attachment->getUrl($isInline), ['Range' => 'bytes=-10']);
377 $resp->assertStreamedContent('0123456789');
378 $resp->assertHeader('Content-Length', '10');
379 $resp->assertHeader('Content-Range', 'bytes 1995-2004/2005');
380 $resp->assertStatus(206);
382 // Range across sniff point
383 $resp = $this->get($attachment->getUrl($isInline), ['Range' => 'bytes=1997-2002']);
384 $resp->assertStreamedContent('234567');
385 $resp->assertHeader('Content-Length', '6');
386 $resp->assertHeader('Content-Range', 'bytes 1997-2002/2005');
387 $resp->assertStatus(206);
389 // Range up to sniff point
390 $resp = $this->get($attachment->getUrl($isInline), ['Range' => 'bytes=0-1997']);
391 $resp->assertHeader('Content-Length', '1998');
392 $resp->assertHeader('Content-Range', 'bytes 0-1997/2005');
393 $resp->assertStreamedContent(substr($content, 0, 1998));
394 $resp->assertStatus(206);
396 // Range beyond sniff point
397 $resp = $this->get($attachment->getUrl($isInline), ['Range' => 'bytes=2001-2003']);
398 $resp->assertStreamedContent('678');
399 $resp->assertHeader('Content-Length', '3');
400 $resp->assertHeader('Content-Range', 'bytes 2001-2003/2005');
401 $resp->assertStatus(206);
403 // Range beyond content
404 $resp = $this->get($attachment->getUrl($isInline), ['Range' => 'bytes=0-2010']);
405 $resp->assertStreamedContent($content);
406 $resp->assertHeader('Content-Length', '2005');
407 $resp->assertHeaderMissing('Content-Range');
408 $resp->assertStatus(200);
410 // Range start before end
411 $resp = $this->get($attachment->getUrl($isInline), ['Range' => 'bytes=50-10']);
412 $resp->assertStreamedContent($content);
413 $resp->assertHeader('Content-Length', '2005');
414 $resp->assertHeader('Content-Range', 'bytes */2005');
415 $resp->assertStatus(416);
418 $this->files->deleteAllAttachmentFiles();