]> BookStack Code Mirror - bookstack/commitdiff
Merge pull request #5379 from BookStackApp/better_cleanup
authorDan Brown <redacted>
Sat, 4 Jan 2025 21:05:45 +0000 (21:05 +0000)
committerGitHub <redacted>
Sat, 4 Jan 2025 21:05:45 +0000 (21:05 +0000)
Export limits and cleanup

app/App/Providers/RouteServiceProvider.php
app/Exports/Controllers/BookExportController.php
app/Exports/Controllers/ChapterExportController.php
app/Exports/Controllers/PageExportController.php
app/Exports/PdfGenerator.php
app/Exports/ZipExports/ZipExportBuilder.php
app/Http/DownloadResponseFactory.php
tests/Exports/PdfExportTest.php
tests/Exports/ZipExportTest.php

index d7c1cb737569618d5e8eb022499ef0e9cb27fe0a..97c3e7c770d0aa74fd2f173b33b9d21b93e5c46b 100644 (file)
@@ -85,5 +85,12 @@ class RouteServiceProvider extends ServiceProvider
         RateLimiter::for('public', function (Request $request) {
             return Limit::perMinute(10)->by($request->ip());
         });
+
+        RateLimiter::for('exports', function (Request $request) {
+            $user = user();
+            $attempts = $user->isGuest() ? 4 : 10;
+            $key = $user->isGuest() ? $request->ip() : $user->id;
+            return Limit::perMinute($attempts)->by($key);
+        });
     }
 }
index f726175a086acab697ce6e05575f69f90b58be8f..b6b1006bd61371b633cfb1a9c7f9f80a710345f2 100644 (file)
@@ -16,6 +16,7 @@ class BookExportController extends Controller
         protected ExportFormatter $exportFormatter,
     ) {
         $this->middleware('can:content-export');
+        $this->middleware('throttle:exports');
     }
 
     /**
@@ -75,6 +76,6 @@ class BookExportController extends Controller
         $book = $this->queries->findVisibleBySlugOrFail($bookSlug);
         $zip = $builder->buildForBook($book);
 
-        return $this->download()->streamedDirectly(fopen($zip, 'r'), $bookSlug . '.zip', filesize($zip));
+        return $this->download()->streamedFileDirectly($zip, $bookSlug . '.zip', filesize($zip), true);
     }
 }
index 0d7a5c0d195ec94181cbacca251a632d2008e7c0..de2385bb11fff1ba0fa9b0d30c3353702ebef541 100644 (file)
@@ -16,6 +16,7 @@ class ChapterExportController extends Controller
         protected ExportFormatter $exportFormatter,
     ) {
         $this->middleware('can:content-export');
+        $this->middleware('throttle:exports');
     }
 
     /**
@@ -81,6 +82,6 @@ class ChapterExportController extends Controller
         $chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
         $zip = $builder->buildForChapter($chapter);
 
-        return $this->download()->streamedDirectly(fopen($zip, 'r'), $chapterSlug . '.zip', filesize($zip));
+        return $this->download()->streamedFileDirectly($zip, $chapterSlug . '.zip', filesize($zip), true);
     }
 }
index 34e67ffcf7075ede6b30a2842789ef8fa204c7f0..d7145411eaad52c3c4ef8e5a5c5381864bb7e690 100644 (file)
@@ -17,6 +17,7 @@ class PageExportController extends Controller
         protected ExportFormatter $exportFormatter,
     ) {
         $this->middleware('can:content-export');
+        $this->middleware('throttle:exports');
     }
 
     /**
@@ -85,6 +86,6 @@ class PageExportController extends Controller
         $page = $this->queries->findVisibleBySlugsOrFail($bookSlug, $pageSlug);
         $zip = $builder->buildForPage($page);
 
-        return $this->download()->streamedDirectly(fopen($zip, 'r'), $pageSlug . '.zip', filesize($zip));
+        return $this->download()->streamedFileDirectly($zip, $pageSlug . '.zip', filesize($zip), true);
     }
 }
index 0524e063fb259ca22e8e9a6e3c9a22352bb6e2ac..f31d8aad078bd60440c25324443c53d958b35730 100644 (file)
@@ -90,18 +90,28 @@ class PdfGenerator
         $process = Process::fromShellCommandline($command);
         $process->setTimeout($timeout);
 
+        $cleanup = function () use ($inputHtml, $outputPdf) {
+            foreach ([$inputHtml, $outputPdf] as $file) {
+                if (file_exists($file)) {
+                    unlink($file);
+                }
+            }
+        };
+
         try {
             $process->run();
         } catch (ProcessTimedOutException $e) {
+            $cleanup();
             throw new PdfExportException("PDF Export via command failed due to timeout at {$timeout} second(s)");
         }
 
         if (!$process->isSuccessful()) {
+            $cleanup();
             throw new PdfExportException("PDF Export via command failed with exit code {$process->getExitCode()}, stdout: {$process->getOutput()}, stderr: {$process->getErrorOutput()}");
         }
 
         $pdfContents = file_get_contents($outputPdf);
-        unlink($outputPdf);
+        $cleanup();
 
         if ($pdfContents === false) {
             throw new PdfExportException("PDF Export via command failed, unable to read PDF output file");
index 4c5c638f59193da085734b1294f67acf20228c7d..9a52c9a3cb8d021b8e6f9962edbba75484f76907 100644 (file)
@@ -84,10 +84,27 @@ class ZipExportBuilder
         $zip->addEmptyDir('files');
 
         $toRemove = [];
-        $this->files->extractEach(function ($filePath, $fileRef) use ($zip, &$toRemove) {
-            $zip->addFile($filePath, "files/$fileRef");
-            $toRemove[] = $filePath;
-        });
+        $addedNames = [];
+
+        try {
+            $this->files->extractEach(function ($filePath, $fileRef) use ($zip, &$toRemove, &$addedNames) {
+                $entryName = "files/$fileRef";
+                $zip->addFile($filePath, $entryName);
+                $toRemove[] = $filePath;
+                $addedNames[] = $entryName;
+            });
+        } catch (\Exception $exception) {
+            // Cleanup the files we've processed so far and respond back with error
+            foreach ($toRemove as $file) {
+                unlink($file);
+            }
+            foreach ($addedNames as $name) {
+                $zip->deleteName($name);
+            }
+            $zip->close();
+            unlink($zipFile);
+            throw new ZipExportException("Failed to add files for ZIP export, received error: " . $exception->getMessage());
+        }
 
         $zip->close();
 
index f29aaa2e45a131e466a240aa8931d72e9287af6a..d06e2bac44d99ef37640149bb30cf63fc1faa42a 100644 (file)
@@ -9,7 +9,7 @@ use Symfony\Component\HttpFoundation\StreamedResponse;
 class DownloadResponseFactory
 {
     public function __construct(
-        protected Request $request
+        protected Request $request,
     ) {
     }
 
@@ -35,6 +35,32 @@ class DownloadResponseFactory
         );
     }
 
+    /**
+     * Create a response that downloads the given file via a stream.
+     * Has the option to delete the provided file once the stream is closed.
+     */
+    public function streamedFileDirectly(string $filePath, string $fileName, int $fileSize, bool $deleteAfter = false): StreamedResponse
+    {
+        $stream = fopen($filePath, 'r');
+
+        if ($deleteAfter) {
+            // Delete the given file if it still exists after the app terminates
+            $callback = function () use ($filePath) {
+                if (file_exists($filePath)) {
+                    unlink($filePath);
+                }
+            };
+
+            // We watch both app terminate and php shutdown to cover both normal app termination
+            // as well as other potential scenarios (connection termination).
+            app()->terminating($callback);
+            register_shutdown_function($callback);
+        }
+
+        return $this->streamedDirectly($stream, $fileName, $fileSize);
+    }
+
+
     /**
      * Create a file download response that provides the file with a content-type
      * correct for the file, in a way so the browser can show the content in browser,
index 9d85c69e23a0b8ede1c62cecb2cd8c4b49f41db6..e4de87d0d2fdb55f26b1da4ef88416ff2aa4b811 100644 (file)
@@ -5,6 +5,7 @@ namespace Tests\Exports;
 use BookStack\Entities\Models\Page;
 use BookStack\Exceptions\PdfExportException;
 use BookStack\Exports\PdfGenerator;
+use FilesystemIterator;
 use Tests\TestCase;
 
 class PdfExportTest extends TestCase
@@ -128,7 +129,7 @@ class PdfExportTest extends TestCase
         }, PdfExportException::class);
     }
 
-    public function test_pdf_command_timout_option_limits_export_time()
+    public function test_pdf_command_timeout_option_limits_export_time()
     {
         $page = $this->entities->page();
         $command = 'php -r \'sleep(4);\'';
@@ -143,4 +144,19 @@ class PdfExportTest extends TestCase
         }, PdfExportException::class,
             "PDF Export via command failed due to timeout at 1 second(s)");
     }
+
+    public function test_pdf_command_option_does_not_leave_temp_files()
+    {
+        $tempDir = sys_get_temp_dir();
+        $startTempFileCount = iterator_count((new FileSystemIterator($tempDir, FilesystemIterator::SKIP_DOTS)));
+
+        $page = $this->entities->page();
+        $command = 'cp {input_html_path} {output_pdf_path}';
+        config()->set('exports.pdf_command', $command);
+
+        $this->asEditor()->get($page->getUrl('/export/pdf'));
+
+        $afterTempFileCount = iterator_count((new FileSystemIterator($tempDir, FilesystemIterator::SKIP_DOTS)));
+        $this->assertEquals($startTempFileCount, $afterTempFileCount);
+    }
 }
index 163828c1b6e9785cc74ca8364ffcabeb0300bdac..1434c013f7310116029318cb0d082492d935d7e7 100644 (file)
@@ -7,6 +7,7 @@ use BookStack\Entities\Repos\BookRepo;
 use BookStack\Entities\Tools\PageContent;
 use BookStack\Uploads\Attachment;
 use BookStack\Uploads\Image;
+use FilesystemIterator;
 use Illuminate\Support\Carbon;
 use Illuminate\Testing\TestResponse;
 use Tests\TestCase;
@@ -60,6 +61,24 @@ class ZipExportTest extends TestCase
         $this->assertEquals($instanceId, $zipInstanceId);
     }
 
+    public function test_export_leaves_no_temp_files()
+    {
+        $tempDir = sys_get_temp_dir();
+        $startTempFileCount = iterator_count((new FileSystemIterator($tempDir, FilesystemIterator::SKIP_DOTS)));
+
+        $page = $this->entities->pageWithinChapter();
+        $this->asEditor();
+        $pageResp = $this->get($page->getUrl("/export/zip"));
+        $pageResp->streamedContent();
+        $pageResp->assertOk();
+        $this->get($page->chapter->getUrl("/export/zip"))->assertOk();
+        $this->get($page->book->getUrl("/export/zip"))->assertOk();
+
+        $afterTempFileCount = iterator_count((new FileSystemIterator($tempDir, FilesystemIterator::SKIP_DOTS)));
+
+        $this->assertEquals($startTempFileCount, $afterTempFileCount);
+    }
+
     public function test_page_export()
     {
         $page = $this->entities->page();
@@ -404,6 +423,28 @@ class ZipExportTest extends TestCase
         $this->assertStringContainsString("[Link to chapter]([[bsexport:chapter:{$chapter->id}]])", $pageData['markdown']);
     }
 
+    public function test_exports_rate_limited_low_for_guest_viewers()
+    {
+        $this->setSettings(['app-public' => 'true']);
+
+        $page = $this->entities->page();
+        for ($i = 0; $i < 4; $i++) {
+            $this->get($page->getUrl("/export/zip"))->assertOk();
+        }
+        $this->get($page->getUrl("/export/zip"))->assertStatus(429);
+    }
+
+    public function test_exports_rate_limited_higher_for_logged_in_viewers()
+    {
+        $this->asAdmin();
+
+        $page = $this->entities->page();
+        for ($i = 0; $i < 10; $i++) {
+            $this->get($page->getUrl("/export/zip"))->assertOk();
+        }
+        $this->get($page->getUrl("/export/zip"))->assertStatus(429);
+    }
+
     protected function extractZipResponse(TestResponse $response): ZipResultData
     {
         $zipData = $response->streamedContent();