{"record":{"id":"d0b9a55583bb677d","repo":"symfony/symfony","slug":"cache-key-s-contains-reserved-characters-s","errorCode":null,"errorMessage":"Cache key \"%s\" contains reserved characters \"%s\".","messagePattern":"Cache key \"(.+?)\" contains reserved characters \"(.+?)\"\\.","errorType":"exception","errorClass":"Symfony\\Component\\Cache\\Exception\\InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Symfony/Component/Cache/CacheItem.php","lineNumber":140,"sourceCode":"    }\n\n    /**\n     * Validates a cache key according to PSR-6.\n     *\n     * @param mixed $key The key to validate\n     *\n     * @throws InvalidArgumentException When $key is not valid\n     */\n    public static function validateKey($key): string\n    {\n        if (!\\is_string($key)) {\n            throw new InvalidArgumentException(\\sprintf('Cache key must be string, \"%s\" given.', get_debug_type($key)));\n        }\n        if ('' === $key) {\n            throw new InvalidArgumentException('Cache key length must be greater than zero.');\n        }\n        if (false !== strpbrk($key, self::RESERVED_CHARACTERS)) {\n            throw new InvalidArgumentException(\\sprintf('Cache key \"%s\" contains reserved characters \"%s\".', $key, self::RESERVED_CHARACTERS));\n        }\n\n        return $key;\n    }\n\n    /**\n     * Internal logging helper.\n     *\n     * @internal\n     */\n    public static function log(?LoggerInterface $logger, string $message, array $context = []): void\n    {\n        if ($logger) {\n            $logger->warning($message, $context);\n        } else {\n            $replace = [];\n            foreach ($context as $k => $v) {\n                if (\\is_scalar($v)) {","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/symfony/symfony/blob/698e28026c22cf35d032cdb6e800db48b1535790/src/Symfony/Component/Cache/CacheItem.php#L122-L158","documentation":"PSR-6 cache keys must not contain reserved characters defined in ItemInterface::RESERVED_CHARACTERS = '{}()/\\\\@:'. These characters are reserved because they are used as delimiters in Symfony's internal key normalization and namespace schemes, and they are also problematic for many storage backends (Redis, filesystem, Memcached). CacheItem::validateKey() throws an InvalidArgumentException when strpbrk() finds any of them in the key.","triggerScenarios":"Calling $pool->getItem('user:profile') (colon), $pool->getItem('app/data/key') (slash), $pool->getItem('cache@v2') (at), or any key containing { } ( ) \\ characters.","commonSituations":"Using natural keys like URIs ('/api/users/123'), class names with namespace separators ('App\\Entity\\User'), or email-style identifiers directly as cache keys. Common when caching by URL path or by FQCN.","solutions":["Replace reserved characters before use: str_replace(['{','}','(',')','/','\\\\','@',':'], '_', $key).","Hash complex keys: $safeKey = hash('xxh128', $rawKey) or md5($rawKey).","Design keys using only alphanumeric characters, underscores, dots, and dashes."],"exampleFix":"// before\n$pool->getItem('/api/users/123'); // slashes are reserved\n$pool->getItem('App\\\\Entity\\\\User'); // backslash is reserved\n\n// after\n$pool->getItem('api_users_123');\n$pool->getItem(hash('xxh128', $uri));","handlingStrategy":"validation","validationCode":"use Symfony\\Contracts\\Cache\\ItemInterface;\n\n$key = str_replace(str_split(ItemInterface::RESERVED_CHARACTERS), '_', $key);\n$item = $pool->getItem($key);","typeGuard":"function keyHasReservedChars(string $key): bool {\n    return false !== strpbrk($key, \\Symfony\\Contracts\\Cache\\ItemInterface::RESERVED_CHARACTERS);\n}","tryCatchPattern":"try {\n    $item = $pool->getItem($key);\n} catch (\\Symfony\\Component\\Cache\\Exception\\InvalidArgumentException $e) {\n    $key = str_replace(str_split(\\Symfony\\Contracts\\Cache\\ItemInterface::RESERVED_CHARACTERS), '_', $key);\n    $item = $pool->getItem($key);\n}","preventionTips":["Design cache keys with only [a-zA-Z0-9._-] characters.","Hash complex/unsafe keys with md5() or xxh128().","Sanitize URI paths and namespace strings before caching."],"tags":["cache","psr-6","validation","runtime"],"analyzedSha":"698e28026c22cf35d032cdb6e800db48b1535790","analyzedAt":"2026-08-06T23:40:49.025Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}