symfony/http-foundation · error · InvalidArgumentException
The cookie name " " uses the "__Host-" prefix, which…
Error message
The cookie name "%s" uses the "__Host-" prefix, which requires the cookie to have no "domain" attribute.
What it means
Per RFC6265bis, a cookie named with the '__Host-' prefix must not carry a Domain attribute — it must be host-only so it can never be shared across subdomains. Symfony's validateNamePrefix() throws an InvalidArgumentException when a __Host- cookie is given a non-empty domain.
Solutions
- Pass null as the $domain (or omit it) for cookies named with the __Host- prefix
- Remove or guard the ->withDomain() call so it is skipped for __Host- cookies
- Use the '__Secure-' prefix instead if you genuinely need a shared Domain attribute
- Catch \InvalidArgumentException during config boot and fail fast with a clear message about the reserved prefix
Example fix
// before
$cookie = Cookie::create('__Host-session', $v, 0, '/', 'example.com', true);
// after
$cookie = Cookie::create('__Host-session', $v, 0, '/', null, true); Defensive patterns
Strategy: validation
Validate before calling
if (str_starts_with($name, '__Host-') && null !== $domain && '' !== $domain) {
throw new \InvalidArgumentException('__Host- cookies must not set a domain');
} Type guard
function isValidHostCookie(string $name, ?string $domain): bool {
return !str_starts_with($name, '__Host-') || null === $domain || '' === $domain;
} Try / catch
try {
$cookie = Cookie::create($name, $value, 0, '/', $domain, true);
} catch (\InvalidArgumentException $e) {
if (str_contains($e->getMessage(), '"__Host-" prefix, which requires the cookie to have no "domain"')) {
$cookie = Cookie::create($name, $value, 0, '/', null, true);
} else {
throw $e;
}
} Prevention
- Do not pipe a global APP_COOKIE_DOMAIN config into cookies that use the __Host- prefix
- Decide per-cookie between host-only (__Host-) and domain-shared (__Secure-) semantics
- Add a config-boot assertion that reserved-prefix cookies carry no domain
- Review cookie definitions when migrating legacy cookies to __Host- names
When it happens
Trigger: Creating a cookie named '__Host-...' with a non-null, non-empty $domain argument, or calling ->withDomain('example.com') (or any non-empty string) on an existing __Host- cookie.
Common situations: Sharing cookie-creation config between host-only and subdomain cookies; code that unconditionally sets the domain from a config value like APP_COOKIE_DOMAIN; renaming a legacy cookie to __Host- while keeping the old domain attribute.
Understand the failure class
Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.
Related errors
- The cookie name " " uses the "__Host-" prefix, which…
- The cookie name " " uses a reserved prefix, which requires…
- The "sameSite" parameter value is not valid.
- You cannot guess the extension as the Mime component is not…
- The disposition must be either
AI-assisted analysis of symfony/http-foundation@5aea19cd67 (2026-09-13).
Data as JSON: /api/errors/2213e2e2ed3249b5.
Report an issue: GitHub.
Appendix: source
Thrown at Cookie.php:447
}
/**
* Rejects a "__Host-" prefixed name combined with attributes that make browsers discard the cookie.
*
* @see https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#section-4.1.3
*/
private static function validateNamePrefix(string $name, ?bool $secure, ?string $domain, string $path): void
{
if (false === $secure && (str_starts_with($name, '__Secure-') || str_starts_with($name, '__Host-'))) {
throw new \InvalidArgumentException(\sprintf('The cookie name "%s" uses a reserved prefix, which requires the "secure" flag to be enabled.', $name));
}
if (!str_starts_with($name, '__Host-')) {
return;
}
if ('' !== (string) $domain) {
throw new \InvalidArgumentException(\sprintf('The cookie name "%s" uses the "__Host-" prefix, which requires the cookie to have no "domain" attribute.', $name));
}
if ('/' !== $path) {
throw new \InvalidArgumentException(\sprintf('The cookie name "%s" uses the "__Host-" prefix, which requires the cookie path to be "/".', $name));
}
}
}
View on GitHub (pinned to 5aea19cd67)