]> BookStack Code Mirror - bookstack/blob - tests/Uploads/ImageTest.php
Prevent dbl exts. on img upload, Randomized attachment upload names
[bookstack] / tests / Uploads / ImageTest.php
1 <?php namespace Tests\Uploads;
2
3 use BookStack\Entities\Repos\PageRepo;
4 use BookStack\Uploads\Image;
5 use BookStack\Entities\Page;
6 use BookStack\Uploads\ImageService;
7 use Tests\TestCase;
8
9 class ImageTest extends TestCase
10 {
11
12     use UsesImages;
13
14     public function test_image_upload()
15     {
16         $page = Page::first();
17         $admin = $this->getAdmin();
18         $this->actingAs($admin);
19
20         $imageName = 'first-image.png';
21         $relPath = $this->getTestImagePath('gallery', $imageName);
22         $this->deleteImage($relPath);
23
24         $upload = $this->uploadImage($imageName, $page->id);
25         $upload->assertStatus(200);
26
27         $this->assertTrue(file_exists(public_path($relPath)), 'Uploaded image not found at path: '. public_path($relPath));
28
29         $this->deleteImage($relPath);
30
31         $this->assertDatabaseHas('images', [
32             'url' => $this->baseUrl . $relPath,
33             'type' => 'gallery',
34             'uploaded_to' => $page->id,
35             'path' => $relPath,
36             'created_by' => $admin->id,
37             'updated_by' => $admin->id,
38             'name' => $imageName
39         ]);
40     }
41
42     public function test_php_files_cannot_be_uploaded()
43     {
44         $page = Page::first();
45         $admin = $this->getAdmin();
46         $this->actingAs($admin);
47
48         $fileName = 'bad.php';
49         $relPath = $this->getTestImagePath('gallery', $fileName);
50         $this->deleteImage($relPath);
51
52         $file = $this->getTestImage($fileName);
53         $upload = $this->withHeader('Content-Type', 'image/jpeg')->call('POST', '/images/gallery/upload', ['uploaded_to' => $page->id], [], ['file' => $file], []);
54         $upload->assertStatus(302);
55
56         $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded php file was uploaded but should have been stopped');
57
58         $this->assertDatabaseMissing('images', [
59             'type' => 'gallery',
60             'name' => $fileName
61         ]);
62     }
63
64     public function test_php_like_files_cannot_be_uploaded()
65     {
66         $page = Page::first();
67         $admin = $this->getAdmin();
68         $this->actingAs($admin);
69
70         $fileName = 'bad.phtml';
71         $relPath = $this->getTestImagePath('gallery', $fileName);
72         $this->deleteImage($relPath);
73
74         $file = $this->getTestImage($fileName);
75         $upload = $this->withHeader('Content-Type', 'image/jpeg')->call('POST', '/images/gallery/upload', ['uploaded_to' => $page->id], [], ['file' => $file], []);
76         $upload->assertStatus(302);
77
78         $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded php file was uploaded but should have been stopped');
79     }
80
81     public function test_files_with_double_extensions_cannot_be_uploaded()
82     {
83         $page = Page::first();
84         $admin = $this->getAdmin();
85         $this->actingAs($admin);
86
87         $fileName = 'bad.phtml.png';
88         $relPath = $this->getTestImagePath('gallery', $fileName);
89         $this->deleteImage($relPath);
90
91         $file = $this->getTestImage($fileName);
92         $upload = $this->withHeader('Content-Type', 'image/png')->call('POST', '/images/gallery/upload', ['uploaded_to' => $page->id], [], ['file' => $file], []);
93         $upload->assertStatus(302);
94
95         $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded double extension file was uploaded but should have been stopped');
96     }
97
98     public function test_secure_images_uploads_to_correct_place()
99     {
100         config()->set('filesystems.default', 'local_secure');
101         $this->asEditor();
102         $galleryFile = $this->getTestImage('my-secure-test-upload.png');
103         $page = Page::first();
104         $expectedPath = storage_path('uploads/images/gallery/' . Date('Y-m-M') . '/my-secure-test-upload.png');
105
106         $upload = $this->call('POST', '/images/gallery/upload', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
107         $upload->assertStatus(200);
108
109         $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: '. $expectedPath);
110
111         if (file_exists($expectedPath)) {
112             unlink($expectedPath);
113         }
114     }
115
116     public function test_secure_images_included_in_exports()
117     {
118         config()->set('filesystems.default', 'local_secure');
119         $this->asEditor();
120         $galleryFile = $this->getTestImage('my-secure-test-upload.png');
121         $page = Page::first();
122         $expectedPath = storage_path('uploads/images/gallery/' . Date('Y-m-M') . '/my-secure-test-upload.png');
123
124         $upload = $this->call('POST', '/images/gallery/upload', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
125         $imageUrl = json_decode($upload->getContent(), true)['url'];
126         $page->html .= "<img src=\"{$imageUrl}\">";
127         $page->save();
128         $upload->assertStatus(200);
129
130         $encodedImageContent = base64_encode(file_get_contents($expectedPath));
131         $export = $this->get($page->getUrl('/export/html'));
132         $this->assertTrue(str_contains($export->getContent(), $encodedImageContent), 'Uploaded image in export content');
133
134         if (file_exists($expectedPath)) {
135             unlink($expectedPath);
136         }
137     }
138
139     public function test_system_images_remain_public()
140     {
141         config()->set('filesystems.default', 'local_secure');
142         $this->asEditor();
143         $galleryFile = $this->getTestImage('my-system-test-upload.png');
144         $page = Page::first();
145         $expectedPath = public_path('uploads/images/system/' . Date('Y-m-M') . '/my-system-test-upload.png');
146
147         $upload = $this->call('POST', '/images/system/upload', ['uploaded_to' => $page->id], [], ['file' => $galleryFile], []);
148         $upload->assertStatus(200);
149
150         $this->assertTrue(file_exists($expectedPath), 'Uploaded image not found at path: '. $expectedPath);
151
152         if (file_exists($expectedPath)) {
153             unlink($expectedPath);
154         }
155     }
156
157     public function test_image_delete()
158     {
159         $page = Page::first();
160         $this->asAdmin();
161         $imageName = 'first-image.png';
162
163         $this->uploadImage($imageName, $page->id);
164         $image = Image::first();
165         $relPath = $this->getTestImagePath('gallery', $imageName);
166
167         $delete = $this->delete( '/images/' . $image->id);
168         $delete->assertStatus(200);
169
170         $this->assertDatabaseMissing('images', [
171             'url' => $this->baseUrl . $relPath,
172             'type' => 'gallery'
173         ]);
174
175         $this->assertFalse(file_exists(public_path($relPath)), 'Uploaded image has not been deleted as expected');
176     }
177
178     public function testBase64Get()
179     {
180         $page = Page::first();
181         $this->asAdmin();
182         $imageName = 'first-image.png';
183
184         $this->uploadImage($imageName, $page->id);
185         $image = Image::first();
186
187         $imageGet = $this->getJson("/images/base64/{$image->id}");
188         $imageGet->assertJson([
189             'content' => 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gEcDCo5iYNs+gAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFElEQVQI12O0jN/KgASYGFABqXwAZtoBV6Sl3hIAAAAASUVORK5CYII='
190         ]);
191     }
192
193     public function test_drawing_base64_upload()
194     {
195         $page = Page::first();
196         $editor = $this->getEditor();
197         $this->actingAs($editor);
198
199         $upload = $this->postJson('images/drawing/upload', [
200             'uploaded_to' => $page->id,
201             'image' => 'image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gEcDCo5iYNs+gAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAAFElEQVQI12O0jN/KgASYGFABqXwAZtoBV6Sl3hIAAAAASUVORK5CYII='
202         ]);
203
204         $upload->assertStatus(200);
205         $upload->assertJson([
206             'type' => 'drawio',
207             'uploaded_to' => $page->id,
208             'created_by' => $editor->id,
209             'updated_by' => $editor->id,
210         ]);
211
212         $image = Image::where('type', '=', 'drawio')->first();
213         $this->assertTrue(file_exists(public_path($image->path)), 'Uploaded image not found at path: '. public_path($image->path));
214
215         $testImageData = file_get_contents($this->getTestImageFilePath());
216         $uploadedImageData = file_get_contents(public_path($image->path));
217         $this->assertTrue($testImageData === $uploadedImageData, "Uploaded image file data does not match our test image as expected");
218     }
219
220     public function test_user_images_deleted_on_user_deletion()
221     {
222         $editor = $this->getEditor();
223         $this->actingAs($editor);
224
225         $imageName = 'profile.png';
226         $relPath = $this->getTestImagePath('gallery', $imageName);
227         $this->deleteImage($relPath);
228
229         $file = $this->getTestImage($imageName);
230         $this->call('POST', '/images/user/upload', [], [], ['file' => $file], []);
231         $this->call('POST', '/images/user/upload', [], [], ['file' => $file], []);
232
233         $profileImages = Image::where('type', '=', 'user')->where('created_by', '=', $editor->id)->get();
234         $this->assertTrue($profileImages->count() === 2, "Found profile images does not match upload count");
235
236         $userDelete = $this->asAdmin()->delete("/settings/users/{$editor->id}");
237         $userDelete->assertStatus(302);
238         $this->assertDatabaseMissing('images', [
239             'type' => 'user',
240             'created_by' => $editor->id
241         ]);
242     }
243
244     public function test_deleted_unused_images()
245     {
246         $page = Page::first();
247         $admin = $this->getAdmin();
248         $this->actingAs($admin);
249
250         $imageName = 'unused-image.png';
251         $relPath = $this->getTestImagePath('gallery', $imageName);
252         $this->deleteImage($relPath);
253
254         $upload = $this->uploadImage($imageName, $page->id);
255         $upload->assertStatus(200);
256         $image = Image::where('type', '=', 'gallery')->first();
257
258         $pageRepo = app(PageRepo::class);
259         $pageRepo->updatePage($page, $page->book_id, [
260             'name' => $page->name,
261             'html' => $page->html . "<img src=\"{$image->url}\">",
262             'summary' => ''
263         ]);
264
265         // Ensure no images are reported as deletable
266         $imageService = app(ImageService::class);
267         $toDelete = $imageService->deleteUnusedImages(true, true);
268         $this->assertCount(0, $toDelete);
269
270         // Save a revision of our page without the image;
271         $pageRepo->updatePage($page, $page->book_id, [
272             'name' => $page->name,
273             'html' => "<p>Hello</p>",
274             'summary' => ''
275         ]);
276
277         // Ensure revision images are picked up okay
278         $imageService = app(ImageService::class);
279         $toDelete = $imageService->deleteUnusedImages(true, true);
280         $this->assertCount(0, $toDelete);
281         $toDelete = $imageService->deleteUnusedImages(false, true);
282         $this->assertCount(1, $toDelete);
283
284         // Check image is found when revisions are destroyed
285         $page->revisions()->delete();
286         $toDelete = $imageService->deleteUnusedImages(true, true);
287         $this->assertCount(1, $toDelete);
288
289         // Check the image is deleted
290         $absPath = public_path($relPath);
291         $this->assertTrue(file_exists($absPath), "Existing uploaded file at path {$absPath} exists");
292         $toDelete = $imageService->deleteUnusedImages(true, false);
293         $this->assertCount(1, $toDelete);
294         $this->assertFalse(file_exists($absPath));
295
296         $this->deleteImage($relPath);
297     }
298
299 }