From c158aa37aa7a0bc2eba2c4da471ad23624484ee4 Mon Sep 17 00:00:00 2001 From: Milos Stojanovic Date: Thu, 13 Jan 2022 11:25:05 +0100 Subject: [PATCH] issue with php8.1 and new full_path key found in $_FILES, fixes #295 --- backend/Controllers/UploadController.php | 12 +++++++++++- tests/backend/Feature/UploadTest.php | 25 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/backend/Controllers/UploadController.php b/backend/Controllers/UploadController.php index 147cf0f..c9b7568 100644 --- a/backend/Controllers/UploadController.php +++ b/backend/Controllers/UploadController.php @@ -63,10 +63,20 @@ class UploadController $total_size = (int) $request->input('resumableTotalSize'); $identifier = (string) preg_replace('/[^0-9a-zA-Z_]/', '', (string) $request->input('resumableIdentifier')); - $file = $request->files->get('file'); + $filebag = $request->files; + $file = $filebag->get('file'); $overwrite_on_upload = (bool) $this->config->get('overwrite_on_upload', false); + // php 8.1 fix + // remove new key 'full_path' so it can preserve compatibility with symfony FileBag + // see https://php.watch/versions/8.1/$_FILES-full-path + if ($file && is_array($file) && array_key_exists('full_path', $file)) { + unset($file['full_path']); + $filebag->set('file', $file); + $file = $filebag->get('file'); + } + if (! $file || ! $file->isValid() || $file->getSize() > $this->config->get('frontend_config.upload_max_size')) { return $response->json('Bad file', 422); } diff --git a/tests/backend/Feature/UploadTest.php b/tests/backend/Feature/UploadTest.php index 731dea0..a137827 100644 --- a/tests/backend/Feature/UploadTest.php +++ b/tests/backend/Feature/UploadTest.php @@ -156,6 +156,31 @@ class UploadTest extends TestCase ]); } + public function testUploadInvalidFile() + { + $this->signIn('john@example.com', 'john123'); + + $file = [ + 'tmp_name' => TEST_FILE, + 'full_path' => 'something', // new in php 8.1 + 'name' => 'something', + 'type' => 'application/octet-stream', + 'size' => 12345, + 'error' => 0, + ]; + + $files = ['file' => $file]; + $data = []; + + $this->sendRequest('GET', '/upload', $data, $files); + + $this->assertStatus(204); + + $this->sendRequest('POST', '/upload', $data, $files); + + $this->assertStatus(422); + } + public function testUploadFileBiggerThanAllowed() { $this->signIn('john@example.com', 'john123');