]> BookStack Code Mirror - bookstack/blobdiff - tests/Entity/PageContentTest.php
Images: Prevented base64 extraction without permission
[bookstack] / tests / Entity / PageContentTest.php
index e8838ae0bb0198b6da7cfe655644a3c33e42ee11..78b739512ba1115d6b800d4a05d47fb4a87e7bf1 100644 (file)
@@ -5,13 +5,10 @@ namespace Tests\Entity;
 use BookStack\Entities\Models\Page;
 use BookStack\Entities\Tools\PageContent;
 use Tests\TestCase;
-use Tests\Uploads\UsesImages;
 
 class PageContentTest extends TestCase
 {
-    use UsesImages;
-
-    protected $base64Jpeg = '/9j/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/yQALCAABAAEBAREA/8wABgAQEAX/2gAIAQEAAD8A0s8g/9k=';
+    protected string $base64Jpeg = '/9j/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/yQALCAABAAEBAREA/8wABgAQEAX/2gAIAQEAAD8A0s8g/9k=';
 
     public function test_page_includes()
     {
@@ -111,6 +108,18 @@ class PageContentTest extends TestCase
         $htmlContent->assertSee('my cat is awesome and scratchy');
     }
 
+    public function test_page_includes_can_be_nested_up_to_three_times()
+    {
+        $page = $this->entities->page();
+        $tag = "{{@{$page->id}#bkmrk-test}}";
+        $page->html = '<p id="bkmrk-test">Hello Barry ' . $tag . '</p>';
+        $page->save();
+
+        $pageResp = $this->asEditor()->get($page->getUrl());
+        $this->withHtml($pageResp)->assertElementContains('#bkmrk-test', 'Hello Barry Hello Barry Hello Barry Hello Barry ' . $tag);
+        $this->withHtml($pageResp)->assertElementNotContains('#bkmrk-test', 'Hello Barry Hello Barry Hello Barry Hello Barry Hello Barry ' . $tag);
+    }
+
     public function test_page_content_scripts_removed_by_default()
     {
         $this->asEditor();
@@ -591,7 +600,7 @@ class PageContentTest extends TestCase
         $imageFile = public_path($imagePath);
         $this->assertEquals(base64_decode($this->base64Jpeg), file_get_contents($imageFile));
 
-        $this->deleteImage($imagePath);
+        $this->files->deleteAtRelativePath($imagePath);
     }
 
     public function test_base64_images_get_extracted_when_containing_whitespace()
@@ -615,7 +624,7 @@ class PageContentTest extends TestCase
         $imageFile = public_path($imagePath);
         $this->assertEquals(base64_decode($base64PngWithoutWhitespace), file_get_contents($imageFile));
 
-        $this->deleteImage($imagePath);
+        $this->files->deleteAtRelativePath($imagePath);
     }
 
     public function test_base64_images_within_html_blanked_if_not_supported_extension_for_extract()
@@ -640,6 +649,35 @@ class PageContentTest extends TestCase
         }
     }
 
+    public function test_base64_images_within_html_blanked_if_no_image_create_permission()
+    {
+        $editor = $this->users->editor();
+        $page = $this->entities->page();
+        $this->permissions->removeUserRolePermissions($editor, ['image-create-all']);
+
+        $this->actingAs($editor)->put($page->getUrl(), [
+            'name' => $page->name,
+            'html' => '<p>test<img src="data:image/jpeg;base64,' . $this->base64Jpeg . '"/></p>',
+        ]);
+
+        $page->refresh();
+        $this->assertStringMatchesFormat('%A<p%A>test<img src="">%A</p>%A', $page->html);
+    }
+
+    public function test_base64_images_within_html_blanked_if_content_does_not_appear_like_an_image()
+    {
+        $page = $this->entities->page();
+
+        $imgContent = base64_encode('file://test/a/b/c');
+        $this->asEditor()->put($page->getUrl(), [
+            'name' => $page->name,
+            'html' => '<p>test<img src="data:image/jpeg;base64,' . $imgContent . '"/></p>',
+        ]);
+
+        $page->refresh();
+        $this->assertStringMatchesFormat('%A<p%A>test<img src="">%A</p>%A', $page->html);
+    }
+
     public function test_base64_images_get_extracted_from_markdown_page_content()
     {
         $this->asEditor();
@@ -659,7 +697,7 @@ class PageContentTest extends TestCase
         $imageFile = public_path($imagePath);
         $this->assertEquals(base64_decode($this->base64Jpeg), file_get_contents($imageFile));
 
-        $this->deleteImage($imagePath);
+        $this->files->deleteAtRelativePath($imagePath);
     }
 
     public function test_markdown_base64_extract_not_limited_by_pcre_limits()
@@ -673,7 +711,7 @@ class PageContentTest extends TestCase
         ini_set('pcre.backtrack_limit', '500');
         ini_set('pcre.recursion_limit', '500');
 
-        $content = str_repeat('a', 5000);
+        $content = str_repeat(base64_decode($this->base64Jpeg), 50);
         $base64Content = base64_encode($content);
 
         $this->put($page->getUrl(), [
@@ -690,7 +728,7 @@ class PageContentTest extends TestCase
         $imageFile = public_path($imagePath);
         $this->assertEquals($content, file_get_contents($imageFile));
 
-        $this->deleteImage($imagePath);
+        $this->files->deleteAtRelativePath($imagePath);
         ini_set('pcre.backtrack_limit', $pcreBacktrackLimit);
         ini_set('pcre.recursion_limit', $pcreRecursionLimit);
     }
@@ -707,6 +745,34 @@ class PageContentTest extends TestCase
         $this->assertStringContainsString('<img src=""', $page->refresh()->html);
     }
 
+    public function test_base64_images_within_markdown_blanked_if_no_image_create_permission()
+    {
+        $editor = $this->users->editor();
+        $page = $this->entities->page();
+        $this->permissions->removeUserRolePermissions($editor, ['image-create-all']);
+
+        $this->actingAs($editor)->put($page->getUrl(), [
+            'name' => $page->name,
+            'markdown' => 'test ![test](data:image/jpeg;base64,' . $this->base64Jpeg . ')',
+        ]);
+
+        $this->assertStringContainsString('<img src=""', $page->refresh()->html);
+    }
+
+    public function test_base64_images_within_markdown_blanked_if_content_does_not_appear_like_an_image()
+    {
+        $page = $this->entities->page();
+
+        $imgContent = base64_encode('file://test/a/b/c');
+        $this->asEditor()->put($page->getUrl(), [
+            'name' => $page->name,
+            'markdown' => 'test ![test](data:image/jpeg;base64,' . $imgContent . ')',
+        ]);
+
+        $page->refresh();
+        $this->assertStringContainsString('<img src=""', $page->refresh()->html);
+    }
+
     public function test_nested_headers_gets_assigned_an_id()
     {
         $page = $this->entities->page();
@@ -735,4 +801,23 @@ class PageContentTest extends TestCase
 
         $this->assertStringContainsString('<p id="bkmrk-%C2%A0">&nbsp;</p>', $page->refresh()->html);
     }
+
+    public function test_page_save_with_many_headers_and_links_is_reasonable()
+    {
+        $page = $this->entities->page();
+
+        $content = '';
+        for ($i = 0; $i < 500; $i++) {
+            $content .= "<table><tbody><tr><td><h5 id='header-{$i}'>Simple Test</h5><a href='#header-{$i}'></a></td></tr></tbody></table>";
+        }
+
+        $time = time();
+        $this->asEditor()->put($page->getUrl(), [
+            'name'    => $page->name,
+            'html'    => $content,
+        ])->assertRedirect();
+
+        $timeElapsed = time() - $time;
+        $this->assertLessThan(3, $timeElapsed);
+    }
 }