Skip to content

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

Merged
frankdejonge merged 2 commits intothephpleague:3.xfrom
packagist:use-XML-entity-codes
Jun 25, 2025
Merged

AsyncAws S3: Replace special characters by XML entity codes#1868
frankdejonge merged 2 commits intothephpleague:3.xfrom
packagist:use-XML-entity-codes

Conversation

@stevenrombauts
Copy link
Copy Markdown
Contributor

When calling deleteDirectory(), we want to make sure that special characters (', ", &, < and >) are replaced by their XML entity code. If not, the following code will result in the error below:

$client = new AsyncAws\S3\S3Client([
    'accessKeyId' => '...',
    'accessKeySecret' => '...',
    'region' => 'eu-west-1',
]);

$adapter = new League\Flysystem\AsyncAwsS3\AsyncAwsS3Adapter($client, 'urlencode-test');
$filesystem = new League\Flysystem\Filesystem($adapter);

$filesystem->write('dir-to-delete/\'"&<>.json', '{"foo": "bar"}');
$filesystem->deleteDirectory('dir-to-delete');

This will result in:

Warning: DOMDocument::createElement(): unterminated entity reference         <>.json in flysystem/vendor/async-aws/s3/src/ValueObject/ObjectIdentifier.php on line 124

Fatal error: Uncaught AsyncAws\Core\Exception\Http\ClientException: HTTP 400 returned for "https://urlencode-test.s3.eu-west-3.amazonaws.com/?delete=".

Code:    UserKeyMustBeSpecified
Message: User key must be specified.

Also see AWS docs here: https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html#object-key-xml-related-constraints

@stevenrombauts stevenrombauts changed the title AsyncS3: Replace special characters by XML entity codes AsyncAws S3: Replace special characters by XML entity codes May 19, 2025
@frankdejonge
Copy link
Copy Markdown
Member

@stevenrombauts can you add a test case that verifies the error so we prevent regressions? The rest looks good 👍

@stevenrombauts
Copy link
Copy Markdown
Contributor Author

stevenrombauts commented May 21, 2025

@frankdejonge Do you mean to verify the exception that can be thrown by this new code, or do you mean something else?

@frankdejonge
Copy link
Copy Markdown
Member

@stevenrombauts yes, verify that the guard works. Essentially a test that fails when the check is removed.

@stevenrombauts
Copy link
Copy Markdown
Contributor Author

@frankdejonge Thanks, got it. I've added that test now, let me know if that works!

@frankdejonge frankdejonge merged commit a5b7369 into thephpleague:3.x Jun 25, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants