Description
The documentation states that an invalid cache key throws a Psr\Cache\InvalidArgumentException:
reference/adapter.md: "Cache keys must follow the PSR-6 rules: an empty key, or a key containing one of the reserved characters {}()/\@:, throws a Psr\Cache\InvalidArgumentException."
reference/tasks/set_task.md: "If the input does not override the placeholder key, the empty key throws a Psr\Cache\InvalidArgumentException."
This is only true when assertions are enabled. Symfony cache adapters (ArrayAdapter, and every adapter based on AbstractAdapterTrait: filesystem, Redis, APCu...) only validate the keys with assert(): with zend.assertions=-1 (production php.ini), an empty key or a key with reserved characters is silently accepted and the item is stored.
Reproduction
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
foreach ([new ArrayAdapter(), new FilesystemAdapter('test')] as $adapter) {
foreach (['', 'a/b'] as $key) {
$adapter->save($adapter->getItem($key)->set('value'));
}
}
$ php -d zend.assertions=1 test.php
Symfony\Component\Cache\Exception\InvalidArgumentException (for each adapter and key)
$ php -d zend.assertions=-1 test.php
(no exception, the items are stored and can be read back)
Found with the CI of #27 (setup-php uses the production php.ini): a test expecting the exception on an empty key failed.
Tested on main (e88cf4f), symfony/cache 7.4 / 8.1, PHP 8.5.
Proposed fix
Document that the key validation belongs to the adapter, and that Symfony adapters only reject invalid keys when assertions are enabled: in production, a placeholder key '' not overridden by the input stores / reads every item under the same empty key.
Requirements
Breaking changes
None (documentation only).
Description
The documentation states that an invalid cache key throws a
Psr\Cache\InvalidArgumentException:reference/adapter.md: "Cache keys must follow the PSR-6 rules: an empty key, or a key containing one of the reserved characters{}()/\@:, throws aPsr\Cache\InvalidArgumentException."reference/tasks/set_task.md: "If the input does not override the placeholder key, the empty key throws aPsr\Cache\InvalidArgumentException."This is only true when assertions are enabled. Symfony cache adapters (
ArrayAdapter, and every adapter based onAbstractAdapterTrait: filesystem, Redis, APCu...) only validate the keys withassert(): withzend.assertions=-1(productionphp.ini), an empty key or a key with reserved characters is silently accepted and the item is stored.Reproduction
Found with the CI of #27 (
setup-phpuses the productionphp.ini): a test expecting the exception on an empty key failed.Tested on
main(e88cf4f), symfony/cache 7.4 / 8.1, PHP 8.5.Proposed fix
Document that the key validation belongs to the adapter, and that Symfony adapters only reject invalid keys when assertions are enabled: in production, a placeholder key
''not overridden by the input stores / reads every item under the same empty key.Requirements
Breaking changes
None (documentation only).