diff --git a/plugins/baser-core/src/Service/PluginsService.php b/plugins/baser-core/src/Service/PluginsService.php index 5b3787cfb2..5988255cff 100644 --- a/plugins/baser-core/src/Service/PluginsService.php +++ b/plugins/baser-core/src/Service/PluginsService.php @@ -701,7 +701,18 @@ public function add(array $postData) $name = $postData['file']->getClientFileName(); $postData['file']->moveTo(TMP . $name); $zip = new BcZip(); + $finfo = new \finfo(FILEINFO_MIME_TYPE); + $mimeType = $finfo->file(TMP . $name); + if (!in_array($mimeType, ['application/zip', 'application/x-zip-compressed'], true)) { + if (file_exists(TMP . $name)) { + unlink(TMP . $name); + } + throw new BcException(__d('baser_core', 'ZIPファイルをアップロードしてください。')); + } if (!$zip->extract(TMP . $name, TMP)) { + if (file_exists(TMP . $name)) { + unlink(TMP . $name); + } throw new BcException(__d('baser_core', 'アップロードしたZIPファイルの展開に失敗しました。')); } $srcDirName = $zip->topArchiveName; diff --git a/plugins/baser-core/tests/TestCase/Service/PluginsServiceTest.php b/plugins/baser-core/tests/TestCase/Service/PluginsServiceTest.php index d21c1117c3..79a35e390e 100644 --- a/plugins/baser-core/tests/TestCase/Service/PluginsServiceTest.php +++ b/plugins/baser-core/tests/TestCase/Service/PluginsServiceTest.php @@ -835,4 +835,39 @@ public function test_getCoreUpdate_vulnerability() $this->assertFalse(file_exists($rceFile), 'getCoreUpdate でOSコマンドインジェクションが発生しました'); } + + /** + * test add ZIP以外のファイルをアップロードした場合 + * @return void + */ + public function test_addRejectsNonZipFile() + { + $zipSrcPath = TMP . 'zip' . DS; + $folder = new BcFolder($zipSrcPath); + $folder->create(); + //架空のプラグイン名を指定して、ZIP以外のファイルを作成 + $plugin = 'NotZipPlugin'; + $testFile = $zipSrcPath . $plugin . '.zip'; + file_put_contents($testFile, 'This is not a zip file.'); + $size = filesize($testFile); + + $this->setUploadFileToRequest('file', $testFile); + $files = new UploadedFile( + $testFile, + $size, + UPLOAD_ERR_OK, + $plugin . '.zip', + 'text/plain' + ); + + $this->expectException("BaserCore\Error\BcException"); + $this->expectExceptionMessage("ZIPファイルをアップロードしてください。"); + try { + $this->Plugins->add(["file" => $files]); + } finally { + // アップロードされた一時ファイルが削除されていること + $this->assertFileDoesNotExist(TMP . $plugin . '.zip'); + $folder->delete(); + } + } }