Skip to content

Commit 9246288

Browse files
committed
add a failing test for special chars while deleting a folder and fix it for the easy cases
1 parent 235ae80 commit 9246288

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/AsyncAwsS3/AsyncAwsS3Adapter.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public function deleteDirectory(string $path): void
188188
foreach ($result->getContents() as $item) {
189189
$key = $item->getKey();
190190
if (null !== $key) {
191-
$objects[] = new ObjectIdentifier(['Key' => $key]);
191+
$objects[] = $this->createObjectIdentifierForXmlRequest($key);
192192
}
193193
}
194194

@@ -574,4 +574,11 @@ public function temporaryUrl(string $path, DateTimeInterface $expiresAt, Config
574574
throw UnableToGenerateTemporaryUrl::dueToError($path, $exception);
575575
}
576576
}
577+
578+
private function createObjectIdentifierForXmlRequest(string $key): ObjectIdentifier
579+
{
580+
$key = htmlentities($key, ENT_XML1 | ENT_QUOTES, 'UTF-8');
581+
582+
return new ObjectIdentifier(['Key' => $key]);
583+
}
577584
}

src/AsyncAwsS3/AsyncAwsS3AdapterTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,63 @@ public function failing_to_delete_a_file(): void
205205
$adapter->delete('path.txt');
206206
}
207207

208+
/**
209+
* @test
210+
*/
211+
public function delete_a_directory_with_special_chars(): void
212+
{
213+
/* $xml = new \XMLWriter();
214+
$xml->openMemory();
215+
$xml->startDocument('1.0', 'UTF-8');
216+
217+
$xml->startElement('span');
218+
$xml->text('a " quote');
219+
$xml->endElement();
220+
221+
$xml->endDocument();
222+
223+
var_dump($xml->outputMemory());
224+
exit;
225+
226+
$document = new \DOMDocument('1.0', 'UTF-8');
227+
$document->appendChild($el = $document->createElement('span'));
228+
$el->appendChild($document->createEntityReference('quot'));
229+
$body = $document->saveXML();
230+
231+
file_put_contents('/tmp/xyz.txt', $body);
232+
var_dump($body);
233+
exit;*/
234+
235+
$adapter = $this->adapter();
236+
237+
$specials = [
238+
"'" => '&apos',
239+
'"' => '&quot',
240+
'&' => '&amp',
241+
'<' => '&lt',
242+
'>' => '&gt',
243+
//"\r" => '&#13', //; or &#x0D
244+
//"\n" => '&#10', // &#x0A
245+
];
246+
$config = new Config();
247+
248+
$counter = 1;
249+
foreach ($specials as $specialChar => $expectedReplacement) {
250+
$key = sprintf('folder/filename-%d-with-%s-special', $counter, $specialChar);
251+
$adapter->write($key, 'some contents', $config);
252+
$counter++;
253+
}
254+
255+
self::assertTrue($adapter->directoryExists('folder'));
256+
257+
$adapter->deleteDirectory('folder');
258+
259+
if ($adapter->directoryExists('folder')) {
260+
$contents = iterator_to_array($adapter->listContents('folder', true));
261+
self::fail('The directory should have been deleted. Files left are: '.print_r($contents, true));
262+
}
263+
}
264+
208265
/**
209266
* @test
210267
*/

0 commit comments

Comments
 (0)