{"record":{"id":"8022d31f9ff9cc07","repo":"phalcon/cphalcon","slug":"a-dependency-injection-container-is-required-to-ac-8022d3","errorCode":null,"errorMessage":"A dependency injection container is required to access the 'crypt' service","messagePattern":"A dependency injection container is required to access the 'crypt' service","errorType":"exception","errorClass":"Phalcon\\Http\\Cookie\\Exceptions\\CryptServiceUnavailable","httpStatus":null,"severity":"error","filePath":"phalcon/Http/Cookie.zep","lineNumber":208,"sourceCode":"    public function getValue(var filters = null, var defaultValue = null) -> var\n    {\n        var container, value, crypt, decryptedValue, filter, signKey, name;\n\n        this->checkRestored();\n\n        let container = null,\n            name = this->name;\n\n        if this->isRead === false {\n            if !fetch value, _COOKIE[name] {\n                return defaultValue;\n            }\n\n            if this->useEncryption {\n                let container = <DiInterface> this->container;\n\n                if container === null {\n                    throw new CryptServiceUnavailable();\n                }\n\n                let crypt = <CryptInterface> container->getShared(\"crypt\");\n\n                if unlikely typeof crypt != \"object\" {\n                    throw new CryptInterfaceRequired();\n                }\n\n                /**\n                 * Verify the cookie's value if the sign key was set\n                 */\n                let signKey = this->signKey;\n\n                if typeof signKey === \"string\" {\n                    /**\n                     * Decrypt the value also decoding it with base64\n                     */\n                    let decryptedValue = crypt->decryptBase64(","sourceCodeStart":190,"sourceCodeEnd":226,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Http/Cookie.zep#L190-L226","documentation":"Http\\Cookie::getValue() with useEncryption(true) must decrypt the raw cookie, which requires the 'crypt' service from the DI container. Cookie is injection-aware; if no container was ever attached (a standalone cookie used without setDI(), or a context where the container was never set), CryptServiceUnavailable is thrown before decryption starts.","triggerScenarios":"new Cookie('token') with useEncryption(true) and getValue() called before any container was attached via setDI(); reading encrypted cookies in CLI workers, queue jobs, or unit tests that skip the full application bootstrap.","commonSituations":"Background workers reading encrypted cookies passed in job payloads; test harnesses constructing Cookie directly; early bootstrap code that reads cookies before the DI container is configured.","solutions":["Attach a container before reading: $cookie->setDI($this->getDI()) (or Di::getDefault())","Ensure the attached container actually has a 'crypt' service registered","In non-HTTP contexts, decrypt the value manually with the Crypt service instead of relying on the cookie's lazy resolution"],"exampleFix":"// before\n$cookie = new Cookie('token');\n$cookie->useEncryption(true);\n$value = $cookie->getValue(); // CryptServiceUnavailable\n\n// after\n$cookie = new Cookie('token');\n$cookie->useEncryption(true);\n$cookie->setDI(Di::getDefault());\n$value = $cookie->getValue();","handlingStrategy":"validation","validationCode":"if ($cookie->useEncryption() && null === Di::getDefault()) {\n    throw new \\RuntimeException(\n        'A DI container with a crypt service is required before reading encrypted cookies'\n    );\n}\n\n$value = $cookie->getValue();","typeGuard":null,"tryCatchPattern":"try {\n    $value = $cookie->getValue();\n} catch (\\Phalcon\\Http\\Cookie\\Exceptions\\CryptServiceUnavailable $e) {\n    // attach the container and retry once\n    $cookie->setDI(Di::getDefault());\n    $value = $cookie->getValue();\n}","preventionTips":["Always setDI() on manually constructed Cookie objects before getValue()","Initialize the DI container (including 'crypt') at the very start of workers and bootstrap scripts","In tests, inject a container fixture carrying a Crypt service before touching cookies"],"tags":["phalcon","http","cookie","dependency-injection","crypt"],"backgroundTag":"missing-di-container","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}