From 2c26cf9759d1ca59195bc1632b28044273835cb2 Mon Sep 17 00:00:00 2001 From: tinect Date: Wed, 27 Dec 2023 01:36:43 +0100 Subject: [PATCH] feat: [ZipArchive] prevent ZipArchive Adapter from deleting directories when using delete --- src/ZipArchive/ZipArchiveAdapter.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/ZipArchive/ZipArchiveAdapter.php b/src/ZipArchive/ZipArchiveAdapter.php index c73ca55cc..a31d53797 100644 --- a/src/ZipArchive/ZipArchiveAdapter.php +++ b/src/ZipArchive/ZipArchiveAdapter.php @@ -132,6 +132,20 @@ public function readStream(string $path) public function delete(string $path): void { + if (empty($path) || \str_ends_with($path, '/')) { + throw UnableToDeleteFile::atLocation($path); + } + + $fileExists = $this->fileExists($path); + + if ($fileExists === false) { + if ($this->directoryExists($path)) { + throw UnableToDeleteFile::atLocation($path); + } + + return; + } + $prefixedPath = $this->pathPrefixer->prefixPath($path); $zipArchive = $this->zipArchiveProvider->createZipArchive(); $success = $zipArchive->locateName($prefixedPath) === false || $zipArchive->deleteName($prefixedPath); @@ -155,7 +169,7 @@ public function deleteDirectory(string $path): void $itemPath = $stats['name']; - if (strpos($itemPath, $prefixedPath) !== 0) { + if (!str_starts_with($itemPath, $prefixedPath)) { continue; }