Skip to content

Allow omitting Domain attribute in SetCookie per RFC 6265#3316

Open
QDenka wants to merge 1 commit intoguzzle:7.10from
QDenka:fix/allow-omitting-cookie-domain
Open

Allow omitting Domain attribute in SetCookie per RFC 6265#3316
QDenka wants to merge 1 commit intoguzzle:7.10from
QDenka:fix/allow-omitting-cookie-domain

Conversation

@QDenka
Copy link

@QDenka QDenka commented Feb 8, 2026

Summary

Per RFC 6265 Section 4.1.2.3, when the Domain attribute is omitted from a Set-Cookie header, the cookie becomes a host-only cookie returned only to the origin server. This is distinct from explicitly setting the Domain to the origin, which also allows subdomains.

Problem

Currently, SetCookie::validate() rejects cookies with a null Domain, throwing:

RuntimeException: Invalid cookie: The cookie domain must not be empty

This makes it impossible to store host-only cookies in the CookieJar when creating cookies manually (without going through extractCookies()).

Solution

Allow null Domain in validate() while still rejecting empty string ('') domains. The matchesDomain() method already handles null domains correctly (returns true).

Changes

  • src/Cookie/SetCookie.php: Updated validate() to allow null domain (host-only cookie)
  • tests/Cookie/SetCookieTest.php: Added test case for null domain validation
  • tests/Cookie/CookieJarTest.php: Added test verifying CookieJar accepts cookies without Domain

Fixes #3315

Per RFC 6265 Section 4.1.2.3, when the Domain attribute is omitted from
a Set-Cookie header, the cookie becomes a host-only cookie that is
returned only to the origin server. This is distinct from explicitly
setting the Domain to the origin, which also allows subdomains.

Previously, SetCookie::validate() rejected cookies with a null Domain,
making it impossible to store host-only cookies in the CookieJar.

This change allows null Domain in validate() while still rejecting
empty string domains. The matchesDomain() method already handles null
domains correctly.

Fixes guzzle#3315
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SetCookie does not support omitting Domain attribute

1 participant