Skip to content

AsyncAws S3: Replace special characters by XML entity codes #1868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/AsyncAwsS3/AsyncAwsS3Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function deleteDirectory(string $path): void
foreach ($result->getContents() as $item) {
$key = $item->getKey();
if (null !== $key) {
$objects[] = new ObjectIdentifier(['Key' => $key]);
$objects[] = $this->createObjectIdentifierForXmlRequest($key);
}
}

Expand Down Expand Up @@ -527,6 +527,17 @@ private function readObject(string $path): ResultStream
}
}

private function createObjectIdentifierForXmlRequest(string $key): ObjectIdentifier
{
$escapedKey = htmlentities($key, ENT_XML1 | ENT_QUOTES, 'UTF-8');

if ($escapedKey === '') {
throw new \RuntimeException(sprintf('Cannot escape key "%s" for XML request, htmlentities() returned an empty string.', $key));
}

return new ObjectIdentifier(['Key' => $escapedKey]);
}

public function publicUrl(string $path, Config $config): string
{
if ( ! $this->client instanceof SimpleS3Client) {
Expand Down
57 changes: 57 additions & 0 deletions src/AsyncAwsS3/AsyncAwsS3AdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use AsyncAws\Core\Test\Http\SimpleMockedResponse;
use AsyncAws\Core\Test\ResultMockFactory;
use AsyncAws\S3\Result\HeadObjectOutput;
use AsyncAws\S3\Result\ListObjectsV2Output;
use AsyncAws\S3\Result\PutObjectOutput;
use AsyncAws\S3\S3Client;
use AsyncAws\S3\ValueObject\AwsObject;
use AsyncAws\SimpleS3\SimpleS3Client;
use Exception;
use League\Flysystem\AdapterTestUtilities\FilesystemAdapterTestCase;
Expand All @@ -21,6 +23,7 @@
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\StorageAttributes;
use League\Flysystem\UnableToCheckFileExistence;
use League\Flysystem\UnableToDeleteDirectory;
use League\Flysystem\UnableToDeleteFile;
use League\Flysystem\UnableToListContents;
use League\Flysystem\UnableToMoveFile;
Expand Down Expand Up @@ -205,6 +208,60 @@ public function failing_to_delete_a_file(): void
$adapter->delete('path.txt');
}

/**
* @test
*/
public function delete_directory_replaces_special_characters_by_xml_entity_codes(): void
{
$this->runScenario(function () {
$directory = 'to-delete';
$object = sprintf('/%s/\'\"&<>.txt', $directory);

$adapter = $this->adapter();
$adapter->write(
$object,
'',
new Config()
);

$adapter->deleteDirectory($directory);

$this->assertFalse($adapter->fileExists($object));
$this->assertFalse($adapter->directoryExists($directory));
});
}

/**
* @test
*/
public function delete_directory_throws_exception_if_object_key_can_not_be_escaped_correctly(): void
{
$listObjectsMock = $this->getMockBuilder(ListObjectsV2Output::class)
->disableOriginalConstructor()
->onlyMethods(['getContents'])
->getMock();

$listObjectsMock->expects(self::once())
->method('getContents')
->willReturn([new AwsObject(['Key' => "\x8F.txt"])]);

$s3Client = $this->getMockBuilder(S3Client::class)
->disableOriginalConstructor()
->onlyMethods(['ListObjectsV2'])
->getMock();

$s3Client->expects(self::once())
->method('ListObjectsV2')
->willReturn($listObjectsMock);

$filesystem = new AsyncAwsS3Adapter($s3Client, 'my-bucket');

$this->expectException(UnableToDeleteDirectory::class);
$this->expectExceptionMessageMatches('/htmlentities\(\) returned an empty string/');

$filesystem->deleteDirectory('directory/containing/objects/with/un-escapable/key');
}

/**
* @test
*/
Expand Down
Loading