Skip to content

EspoCRM – Open Source CRM Application

Package unzip changes

Yurii02eb7159625d
diff --git a/application/Espo/Core/Upgrades/Actions/Base.php b/application/Espo/Core/Upgrades/Actions/Base.php
index 72ca12f10c..f84a436469 100644
--- a/application/Espo/Core/Upgrades/Actions/Base.php
+++ b/application/Espo/Core/Upgrades/Actions/Base.php
@@ -819,10 +819,18 @@ abstract class Base
$packageArchivePath = $this->getPackagePath(true);
if (!file_exists($packageArchivePath)) {
- $this->throwErrorAndRemovePackage('Package Archive does not exist.', false, false);
+ $this->throwErrorAndRemovePackage('Package archive does not exist.', false, false);
}
- $res = $this->getZipUtil()->unzip($packageArchivePath, $packagePath);
+ try {
+ $res = $this->getZipUtil()->unzip($packageArchivePath, $packagePath);
+ } catch (Throwable $e) {
+ $message = $e->getMessage();
+
+ $this->throwErrorAndRemovePackage("Unzip error. $packagePath. $message", false, false);
+
+ return;
+ }
if ($res === false) {
$this->throwErrorAndRemovePackage("Unable to unzip the file $packagePath.", false, false);
diff --git a/application/Espo/Core/Utils/File/ZipArchive.php b/application/Espo/Core/Utils/File/ZipArchive.php
index 3fde2fcef8..afcddd0b43 100644
--- a/application/Espo/Core/Utils/File/ZipArchive.php
+++ b/application/Espo/Core/Utils/File/ZipArchive.php
@@ -29,6 +29,7 @@
namespace Espo\Core\Utils\File;
+use Espo\Core\Utils\Util;
use RuntimeException;
class ZipArchive
@@ -47,11 +48,10 @@ class ZipArchive
/**
* Unzip archive.
*
- * @param string $file Path to .zip file.
- * @param string $destinationPath Destination path.
- * @return bool
+ * @param string $file A path to a zip file.
+ * @param string $destination A destination.
*/
- public function unzip($file, $destinationPath)
+ public function unzip(string $file, string $destination): bool
{
if (!class_exists('\ZipArchive')) {
throw new RuntimeException("php-zip extension is not installed. Cannot unzip the file.");
@@ -61,15 +61,33 @@ class ZipArchive
$res = $zip->open($file);
- if ($res === true) {
- $this->fileManager->mkdir($destinationPath);
+ if ($res !== true) {
+ return false;
+ }
+
+ $this->fileManager->mkdir($destination);
+
- $zip->extractTo($destinationPath);
- $zip->close();
+ for ($i = 0; $i < $zip->numFiles; $i ++) {
+ $filename = $zip->getNameIndex($i);
- return true;
+ if ($filename === false) {
+ continue;
+ }
+
+ if (
+ str_contains($filename, '..') ||
+ str_starts_with($filename, '/') ||
+ str_starts_with($filename, '\\')
+ ) {
+ throw new RuntimeException("No allowed path.");
+ }
+
+ $zip->extractTo($destination, $filename);
}
- return false;
+ $zip->close();
+
+ return true;
}
}