]> BookStack Code Mirror - bookstack/commitdiff
Ensured base64 images are read from image upload folder
authorDan Brown <redacted>
Sun, 6 Dec 2020 15:34:18 +0000 (15:34 +0000)
committerDan Brown <redacted>
Sun, 6 Dec 2020 15:34:18 +0000 (15:34 +0000)
Also removed unused storage systems and updated testing.

app/Config/filesystems.php
app/Uploads/ImageService.php
tests/Entity/ExportTest.php

index bd7d28300abae17112857ead07d3d000c4fd823b..30a5c53691d3a514d852d9d7fbe8f697d7d9321d 100644 (file)
@@ -42,13 +42,6 @@ return [
             'root'   => storage_path(),
         ],
 
-        'ftp' => [
-            'driver'   => 'ftp',
-            'host'     => 'ftp.example.com',
-            'username' => 'your-username',
-            'password' => 'your-password',
-        ],
-
         's3' => [
             'driver' => 's3',
             'key'    => env('STORAGE_S3_KEY', 'your-key'),
@@ -59,16 +52,6 @@ return [
             'use_path_style_endpoint' => env('STORAGE_S3_ENDPOINT', null) !== null,
         ],
 
-        'rackspace' => [
-            'driver'    => 'rackspace',
-            'username'  => 'your-username',
-            'key'       => 'your-key',
-            'container' => 'your-container',
-            'endpoint'  => 'https://identity.api.rackspacecloud.com/v2.0/',
-            'region'    => 'IAD',
-            'url_type'  => 'publicURL',
-        ],
-
     ],
 
 ];
index 9d38b0b120e30f9a6115f9ee2c1d07c1821cac0d..1e5ad8aa198131e0816e196eafca818fe0eba17b 100644 (file)
@@ -450,28 +450,32 @@ class ImageService
 
     /**
      * Get a storage path for the given image URL.
+     * Ensures the path will start with "uploads/images".
      * Returns null if the url cannot be resolved to a local URL.
      */
     private function imageUrlToStoragePath(string $url): ?string
     {
-        $url = trim($url);
+        $url = ltrim(trim($url), '/');
 
         // Handle potential relative paths
         $isRelative = strpos($url, 'http') !== 0;
         if ($isRelative) {
-            return trim($url, '/');
+            if (strpos(strtolower($url), 'uploads/images') === 0) {
+                return trim($url, '/');
+            }
+            return null;
         }
 
         // Handle local images based on paths on the same domain
         $potentialHostPaths = [
-            url('/'),
-            $this->getPublicUrl('/'),
+            url('uploads/images/'),
+            $this->getPublicUrl('/uploads/images/'),
         ];
 
         foreach ($potentialHostPaths as $potentialBasePath) {
             $potentialBasePath = strtolower($potentialBasePath);
             if (strpos(strtolower($url), $potentialBasePath) === 0) {
-                return trim(substr($url, strlen($potentialBasePath)), '/');
+                return 'uploads/images/' . trim(substr($url, strlen($potentialBasePath)), '/');
             }
         }
 
index 5a94adac91c4b8d8dc46866f897e45f7057c3808..b1e6eb5fb712f673ab29d362218d64c4ce2d9ba0 100644 (file)
@@ -1,9 +1,8 @@
 <?php namespace Tests\Entity;
 
-
 use BookStack\Entities\Chapter;
 use BookStack\Entities\Page;
-use BookStack\Uploads\HttpFetcher;
+use Illuminate\Support\Facades\Storage;
 use Illuminate\Support\Str;
 use Tests\TestCase;
 
@@ -154,14 +153,39 @@ class ExportTest extends TestCase
     public function test_page_export_sets_right_data_type_for_svg_embeds()
     {
         $page = Page::first();
-        $page->html = '<img src="http://example.com/image.svg">';
+        Storage::disk('local')->makeDirectory('uploads/images/gallery');
+        Storage::disk('local')->put('uploads/images/gallery/svg_test.svg', '<svg></svg>');
+        $page->html = '<img src="http://localhost/uploads/images/gallery/svg_test.svg">';
         $page->save();
 
         $this->asEditor();
-        $this->mockHttpFetch('<svg></svg>');
         $resp = $this->get($page->getUrl('/export/html'));
+        Storage::disk('local')->delete('uploads/images/gallery/svg_test.svg');
+
         $resp->assertStatus(200);
         $resp->assertSee('<img src="data:image/svg+xml;base64');
     }
 
+    public function test_page_export_contained_html_image_fetches_only_run_when_url_points_to_image_upload_folder()
+    {
+        $page = Page::first();
+        $page->html = '<img src="http://localhost/uploads/images/gallery/svg_test.svg"/>'
+            ."\n".'<img src="http://localhost/uploads/svg_test.svg"/>'
+            ."\n".'<img src="/uploads/svg_test.svg"/>';
+        $storageDisk = Storage::disk('local');
+        $storageDisk->makeDirectory('uploads/images/gallery');
+        $storageDisk->put('uploads/images/gallery/svg_test.svg', '<svg>good</svg>');
+        $storageDisk->put('uploads/svg_test.svg', '<svg>bad</svg>');
+        $page->save();
+
+        $resp = $this->asEditor()->get($page->getUrl('/export/html'));
+
+        $storageDisk->delete('uploads/images/gallery/svg_test.svg');
+        $storageDisk->delete('uploads/svg_test.svg');
+
+        $resp->assertDontSee('http://localhost/uploads/images/gallery/svg_test.svg');
+        $resp->assertSee('http://localhost/uploads/svg_test.svg');
+        $resp->assertSee('src="/uploads/svg_test.svg"');
+    }
+
 }
\ No newline at end of file