A free open source IT asset/license management system
Imports + Uploads: Fixed FD-57827 / m5p2-gwww - clear tmp files from local filesystem
snipe3018806259a5
diff --git a/app/Http/Controllers/Api/ImportController.php b/app/Http/Controllers/Api/ImportController.phpindex 57fa5ea9c5..5cfc8d8047 100644--- a/app/Http/Controllers/Api/ImportController.php+++ b/app/Http/Controllers/Api/ImportController.php@@ -173,6 +173,15 @@ class ImportController extends Controller$tmpname = tempnam(sys_get_temp_dir(), '');$tmpresults = file_put_contents($tmpname, $transliterated);$transliterated = null; // save on memory?++ // Clean up the UTF-8 copy at request end so we don't+ // leave the transliterated bytes sitting in sys_get_temp_dir()+ register_shutdown_function(static function () use ($tmpname) {+ if (is_file($tmpname)) {+ @unlink($tmpname);+ }+ });+if ($tmpresults !== false) {$newfile = new UploadedFile($tmpname, $file->getClientOriginalName(), null, null, true); // WARNING: this is enabling 'test mode' - which is gross, but otherwise the file won't be treated as 'uploaded'if ($newfile->isValid()) {diff --git a/app/Http/Traits/ConvertsBase64ToFiles.php b/app/Http/Traits/ConvertsBase64ToFiles.phpindex 39d1ce09f9..40815aca08 100644--- a/app/Http/Traits/ConvertsBase64ToFiles.php+++ b/app/Http/Traits/ConvertsBase64ToFiles.php@@ -40,22 +40,27 @@ trait ConvertsBase64ToFilesreturn;}- // Generate a temporary path to store the Base64 contents- $tempFilePath = tempnam(sys_get_temp_dir(), $filename);+ // Validate the data URI envelope BEFORE allocating a+ // tempfile.+ if (! Str::startsWith($base64Contents, 'data:') || count(explode(',', $base64Contents)) <= 1) {+ throw new ValidationException("Need Base64 URL starting with 'data:'");+ }- // Store the contents using a stream, or throw an Error (which doesn't do anything?)- if (Str::startsWith($base64Contents, 'data:') && count(explode(',', $base64Contents)) > 1) {- $source = fopen($base64Contents, 'r'); // PHP has special processing for "data:" URL's- $destination = fopen($tempFilePath, 'w');+ $tempFilePath = tempnam(sys_get_temp_dir(), $filename);- stream_copy_to_stream($source, $destination);+ // Clean up the tempfile at the end of the request.+ register_shutdown_function(static function () use ($tempFilePath) {+ if (is_file($tempFilePath)) {+ @unlink($tempFilePath);+ }+ });- fclose($source);- fclose($destination);- } else {- // TODO - to get a better error message here, can we maybe do something with modifying the errorBag?- throw new ValidationException("Need Base64 URL starting with 'data:'"); // This doesn't actually throw?- }+ // PHP has special processing for "data:" URL's.+ $source = fopen($base64Contents, 'r');+ $destination = fopen($tempFilePath, 'w');+ stream_copy_to_stream($source, $destination);+ fclose($source);+ fclose($destination);$uploadedFile = new UploadedFile($tempFilePath, $filename, null, null, true);diff --git a/tests/Feature/Uploads/Base64TempFileLeakTest.php b/tests/Feature/Uploads/Base64TempFileLeakTest.phpnew file mode 100644index 0000000000..9c012bac9d--- /dev/null+++ b/tests/Feature/Uploads/Base64TempFileLeakTest.php@@ -0,0 +1,98 @@+<?php++namespace Tests\Feature\Uploads;++use App\Http\Traits\ConvertsBase64ToFiles;+use Illuminate\Foundation\Http\FormRequest;+use Tests\TestCase;++class Base64TempFileLeakTest extends TestCase+{+ public function test_invalid_base64_input_does_not_create_a_tempfile(): void+ {+ $prefix = 'snipeleak_'.bin2hex(random_bytes(6));++ $request = Base64LeakProbeRequest::make($prefix, 'not-a-data-uri');+ $request->triggerPrepareForValidation();++ $leftovers = glob(sys_get_temp_dir().'/'.$prefix.'*');+ $this->assertEmpty(+ $leftovers,+ 'Invalid base64 input must not create a tempfile. Leftovers: '.implode(', ', $leftovers ?: []),+ );+ }++ public function test_empty_input_does_not_create_a_tempfile(): void+ {+ // Early-return path: no base64 value in the field at all. This+ // already short-circuited pre-fix, but the test documents the+ // intended behavior now that the invalid-string path matches.+ $prefix = 'snipeleak_'.bin2hex(random_bytes(6));++ $request = Base64LeakProbeRequest::make($prefix, '');+ $request->triggerPrepareForValidation();++ $leftovers = glob(sys_get_temp_dir().'/'.$prefix.'*');+ $this->assertEmpty(+ $leftovers,+ 'Empty input must not create a tempfile. Leftovers: '.implode(', ', $leftovers ?: []),+ );+ }++ public function test_valid_data_uri_still_produces_a_tempfile_backed_upload(): void+ {+ // Companion: the reorder must not have broken the happy path.+ // A real data URI should still land as a tempfile wrapped in an+ // UploadedFile on the request. The register_shutdown_function+ // cleanup for this file fires at PHP shutdown, which happens+ // after this test method returns, so we only assert that the+ // tempfile exists while the request is live.+ $prefix = 'snipeleak_'.bin2hex(random_bytes(6));+ $dataUri = 'data:text/plain;base64,'.base64_encode('hello');++ $request = Base64LeakProbeRequest::make($prefix, $dataUri);+ $request->triggerPrepareForValidation();++ $leftovers = glob(sys_get_temp_dir().'/'.$prefix.'*');+ $this->assertCount(+ 1,+ $leftovers,+ 'Valid base64 input must still produce exactly one tempfile for the UploadedFile to wrap.',+ );++ // Clean up this test's own artifact so repeated runs do not pile+ // up before the real shutdown handler fires at process end.+ @unlink($leftovers[0]);+ }+}++/**+ * Minimal FormRequest that uses the trait so the test can drive+ * prepareForValidation directly without routing through a real+ * controller. The trait method is protected, so we expose a public+ * trigger here.+ */+class Base64LeakProbeRequest extends FormRequest+{+ use ConvertsBase64ToFiles;++ private string $filePrefix = 'probe';++ public static function make(string $prefix, string $value): self+ {+ $request = self::create('/probe', 'POST', ['probe' => $value]);+ $request->filePrefix = $prefix;++ return $request;+ }++ public function triggerPrepareForValidation(): void+ {+ $this->prepareForValidation();+ }++ protected function base64FileKeys(): array+ {+ return ['probe' => $this->filePrefix];+ }+}