From a25e59f521fd4dd6a5a81f62b748e378495b3da3 Mon Sep 17 00:00:00 2001 From: tinect Date: Sun, 24 Dec 2023 00:35:57 +0100 Subject: [PATCH] feat: prevent delete operation from deleting all contents --- src/BunnyCDNAdapter.php | 5 ++++ tests/ClientTest.php | 4 ++++ tests/FlysystemAdapterTest.php | 43 ++++++++++++++++++++++++++++++++++ tests/PrefixTest.php | 4 ++++ 4 files changed, 56 insertions(+) diff --git a/src/BunnyCDNAdapter.php b/src/BunnyCDNAdapter.php index db40608..fe6ab7a 100644 --- a/src/BunnyCDNAdapter.php +++ b/src/BunnyCDNAdapter.php @@ -450,6 +450,11 @@ private function copyFile(string $source, string $destination, Config $config): */ public function delete($path): void { + // if path is empty or ends with /, it's a directory. + if (empty($path) || str_ends_with($path, '/')) { + throw UnableToDeleteFile::atLocation($path, 'Deletion of directories prevented.'); + } + try { $this->client->delete($path); // @codeCoverageIgnoreStart diff --git a/tests/ClientTest.php b/tests/ClientTest.php index e8bd48e..868ddc1 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -10,6 +10,10 @@ use PlatformCommunity\Flysystem\BunnyCDN\Exceptions\DirectoryNotEmptyException; use PlatformCommunity\Flysystem\BunnyCDN\Exceptions\NotFoundException; +if (\is_file(__DIR__.'/ClientDI.php')) { + require_once __DIR__.'/ClientDI.php'; +} + class ClientTest extends TestCase { public BunnyCDNClient $client; diff --git a/tests/FlysystemAdapterTest.php b/tests/FlysystemAdapterTest.php index cb57342..ddd1d2f 100644 --- a/tests/FlysystemAdapterTest.php +++ b/tests/FlysystemAdapterTest.php @@ -9,6 +9,7 @@ use League\Flysystem\FilesystemAdapter; use League\Flysystem\FilesystemException; use League\Flysystem\UnableToCopyFile; +use League\Flysystem\UnableToDeleteFile; use League\Flysystem\UnableToMoveFile; use League\Flysystem\UnableToProvideChecksum; use League\Flysystem\UnableToRetrieveMetadata; @@ -19,6 +20,10 @@ use PlatformCommunity\Flysystem\BunnyCDN\Util; use Throwable; +if (\is_file(__DIR__.'/ClientDI.php')) { + require_once __DIR__.'/ClientDI.php'; +} + class FlysystemAdapterTest extends FilesystemAdapterTestCase { public const DEMOURL = 'https://example.org.local'; @@ -70,6 +75,44 @@ public function generating_a_temporary_url(): void $this->markTestSkipped('No temporary URL support is provided for BunnyCDN'); } + /** + * @test + */ + public function delete_on_directory_throws_exception(): void + { + $this->runScenario(function () { + $adapter = $this->adapter(); + + $adapter->write( + 'test/text.txt', + 'contents', + new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC]) + ); + + $this->expectException(UnableToDeleteFile::class); + $adapter->delete('test/'); + }); + } + + /** + * @test + */ + public function delete_with_empty_path_throws_exception(): void + { + $this->runScenario(function () { + $adapter = $this->adapter(); + + $adapter->write( + 'test/text.txt', + 'contents', + new Config([Config::OPTION_VISIBILITY => Visibility::PUBLIC]) + ); + + $this->expectException(UnableToDeleteFile::class); + $adapter->delete(''); + }); + } + /** * @test */ diff --git a/tests/PrefixTest.php b/tests/PrefixTest.php index 32c1a00..c920ba3 100644 --- a/tests/PrefixTest.php +++ b/tests/PrefixTest.php @@ -13,6 +13,10 @@ use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNAdapter; use PlatformCommunity\Flysystem\BunnyCDN\BunnyCDNClient; +if (\is_file(__DIR__.'/ClientDI.php')) { + require_once __DIR__.'/ClientDI.php'; +} + class PrefixTest extends FilesystemAdapterTestCase { /**