phalcon/cphalcon · error · Phalcon\Http\Cookie\Exceptions\CryptServiceUnavailable

A dependency injection container is required to access the '

Error message

A dependency injection container is required to access the 'crypt' service

What it means

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.

Source

Thrown at phalcon/Http/Cookie.zep:208

    public function getValue(var filters = null, var defaultValue = null) -> var
    {
        var container, value, crypt, decryptedValue, filter, signKey, name;

        this->checkRestored();

        let container = null,
            name = this->name;

        if this->isRead === false {
            if !fetch value, _COOKIE[name] {
                return defaultValue;
            }

            if this->useEncryption {
                let container = <DiInterface> this->container;

                if container === null {
                    throw new CryptServiceUnavailable();
                }

                let crypt = <CryptInterface> container->getShared("crypt");

                if unlikely typeof crypt != "object" {
                    throw new CryptInterfaceRequired();
                }

                /**
                 * Verify the cookie's value if the sign key was set
                 */
                let signKey = this->signKey;

                if typeof signKey === "string" {
                    /**
                     * Decrypt the value also decoding it with base64
                     */
                    let decryptedValue = crypt->decryptBase64(

View on GitHub (pinned to b7419de9cd)

Solutions

  1. Attach a container before reading: $cookie->setDI($this->getDI()) (or Di::getDefault())
  2. Ensure the attached container actually has a 'crypt' service registered
  3. In non-HTTP contexts, decrypt the value manually with the Crypt service instead of relying on the cookie's lazy resolution

Example fix

// before
$cookie = new Cookie('token');
$cookie->useEncryption(true);
$value = $cookie->getValue(); // CryptServiceUnavailable

// after
$cookie = new Cookie('token');
$cookie->useEncryption(true);
$cookie->setDI(Di::getDefault());
$value = $cookie->getValue();
Defensive patterns

Strategy: validation

Validate before calling

if ($cookie->useEncryption() && null === Di::getDefault()) {
    throw new \RuntimeException(
        'A DI container with a crypt service is required before reading encrypted cookies'
    );
}

$value = $cookie->getValue();

Try / catch

try {
    $value = $cookie->getValue();
} catch (\Phalcon\Http\Cookie\Exceptions\CryptServiceUnavailable $e) {
    // attach the container and retry once
    $cookie->setDI(Di::getDefault());
    $value = $cookie->getValue();
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21). Data as JSON: /api/errors/8022d31f9ff9cc07. Report an issue: GitHub.