Skip to content

The documentation states that invalid cache keys always throw an exception #28

Description

@njoubert-cleverage

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

  • Documentation updates
    • Reference
    • Changelog
  • Unit tests

Breaking changes

None (documentation only).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions