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

A dependency injection container is required to access the '

Error message

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

What it means

When Http\Cookie::getValue() is called with a non-null $filters argument and no FilterInterface object has been set on the cookie, it resolves the 'filter' service from the DI container. If no container is attached, FilterServiceUnavailable is thrown — the same injection-aware failure as the crypt variant, but on the sanitization path.

Source

Thrown at phalcon/Http/Cookie.zep:253

                }
            } else {
                let decryptedValue = value;
            }

            /**
             * Update the decrypted value
             */
            let this->value = decryptedValue;

            if filters !== null {
                let filter = this->filter;

                if typeof filter != "object" {
                    if container === null {
                        let container = <DiInterface> this->container;

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

                    let filter = <FilterInterface> container->getShared("filter"),
                        this->filter = filter;
                }

                return filter->sanitize(decryptedValue, filters);
            }

            /**
             * Return the value without filtering
             */
            return decryptedValue;
        }

        return this->value;
    }

View on GitHub (pinned to b7419de9cd)

Solutions

  1. Attach the container before reading: $cookie->setDI(Di::getDefault())
  2. Or set the filter explicitly: $cookie->setFilter($di->getShared('filter'))
  3. If sanitization happens elsewhere, call getValue() with no filters

Example fix

// before
$cookie = new Cookie('session_id');
$clean = $cookie->getValue('alnum'); // FilterServiceUnavailable

// after
$cookie = new Cookie('session_id');
$cookie->setDI(Di::getDefault());
$clean = $cookie->getValue('alnum');
Defensive patterns

Strategy: validation

Validate before calling

if ($filters !== null) {
    if (!is_object($cookie->getFilter() ?? null) && null === Di::getDefault()) {
        throw new \RuntimeException(
            "A DI container with a 'filter' service is required to filter cookie values"
        );
    }
}

$value = $cookie->getValue($filters);

Try / catch

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

Prevention

When it happens

Trigger: $cookie->getValue('string') (any non-null filter name) on a cookie whose setFilter() was never called and whose DI container was never attached via setDI(); reading and filtering cookies in CLI workers or tests that bypass full bootstrap.

Common situations: Background jobs and test harnesses constructing Cookie directly; early bootstrap code that filters cookies before the container exists; omitting setFilter() when the cookie is used outside the HTTP lifecycle.

Related errors


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