From 0b3016c7fb6d2c33df4fd7bc88a7cf1e60bf820f Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 25 Oct 2026 01:30:25 +0000 Subject: [PATCH 1/5] Drop expired cookies instead of storing them (#3987) --- CHANGELOG.md | 7 ++++ src/Cookie/CookieJar.php | 2 +- tests/Cookie/CookieJarTest.php | 58 ++++++++++++++++++++++++++++++++-- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b17d20cfe..1e39e13ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,13 @@ Please refer to [UPGRADING](UPGRADING.md) guide for upgrading to a major version. +## 8.3.1 - Upcoming + +### Fixed + +- Fix `CookieJar` storing already expired cookies instead of removing the stored cookie + + ## 8.3.0 - 2026-10-18 ### Changed diff --git a/src/Cookie/CookieJar.php b/src/Cookie/CookieJar.php index c6255d7a5..946aed0ba 100644 --- a/src/Cookie/CookieJar.php +++ b/src/Cookie/CookieJar.php @@ -187,7 +187,7 @@ public function setCookie( } $maxAge = $cookie->getMaxAge(); - if ($maxAge !== null && $maxAge <= 0) { + if (($maxAge !== null && $maxAge <= 0) || $cookie->isExpired()) { if ($cookie->getDomain() !== null) { $this->removeCookie($cookie); } diff --git a/tests/Cookie/CookieJarTest.php b/tests/Cookie/CookieJarTest.php index 0f907ec05..7badbabd0 100644 --- a/tests/Cookie/CookieJarTest.php +++ b/tests/Cookie/CookieJarTest.php @@ -31,7 +31,7 @@ protected function getTestCookies(): array { return [ new SetCookie(['Name' => 'foo', 'Value' => 'bar', 'Domain' => 'foo.com', 'Path' => '/', 'Discard' => true]), - new SetCookie(['Name' => 'test', 'Value' => '123', 'Domain' => 'baz.com', 'Path' => '/foo', 'Expires' => 2]), + new SetCookie(['Name' => 'test', 'Value' => '123', 'Domain' => 'baz.com', 'Path' => '/foo', 'Expires' => 2139722880]), new SetCookie(['Name' => 'you', 'Value' => '123', 'Domain' => 'bar.com', 'Path' => '/boo', 'Expires' => \time() + 1000]), ]; } @@ -661,7 +661,7 @@ public function testNullValueCookieStillDeletesMatchingCookie(): void public function testAddsCookiesFromResponseWithRequest(): void { $response = new Response(200, [ - 'Set-Cookie' => 'fpc=d=.Hm.yh4.1XmJWjJfs4orLQzKzPImxklQoxXSHOZATHUSEFciRueW_7704iYUtsXNEXq0M92Px2glMdWypmJ7HIQl6XIUvrZimWjQ3vIdeuRbI.FNQMAfcxu_XN1zSx7l.AcPdKL6guHc2V7hIQFhnjRW0rxm2oHY1P4bGQxFNz7f.tHm12ZD3DbdMDiDy7TBXsuP4DM-&v=2; expires=Fri, 02-Mar-2019 02:17:40 GMT;', + 'Set-Cookie' => 'fpc=d=.Hm.yh4.1XmJWjJfs4orLQzKzPImxklQoxXSHOZATHUSEFciRueW_7704iYUtsXNEXq0M92Px2glMdWypmJ7HIQl6XIUvrZimWjQ3vIdeuRbI.FNQMAfcxu_XN1zSx7l.AcPdKL6guHc2V7hIQFhnjRW0rxm2oHY1P4bGQxFNz7f.tHm12ZD3DbdMDiDy7TBXsuP4DM-&v=2; expires=Wed, 21-Oct-2037 07:28:00 GMT;', ]); $request = new Request('GET', 'http://www.example.com'); $this->jar->extractCookies($request, $response); @@ -980,6 +980,60 @@ public function testMaxAgeZeroCookieFromResponseOverridesFutureExpires(): void self::assertCount(0, $this->jar); } + public function testMaxAgeZeroSetAfterConstructionRemovesExistingCookie(): void + { + $data = ['Name' => 'sid', 'Value' => 'abc', 'Domain' => 'example.com', 'Path' => '/', 'Expires' => 2139722880]; + self::assertTrue($this->jar->setCookie(new SetCookie($data))); + + $deletion = new SetCookie($data); + $deletion->setMaxAge(0); + + self::assertFalse($this->jar->setCookie($deletion)); + self::assertCount(0, $this->jar); + } + + public function testDoesNotStoreExpiredCookieFromResponse(): void + { + $this->jar->extractCookies( + new Request('GET', 'https://example.com/'), + new Response(200, ['Set-Cookie' => 'sid=abc; Expires=Thu, 01 Jan 1970 00:00:01 GMT; Path=/']) + ); + + self::assertCount(0, $this->jar); + } + + /** + * @dataProvider expiredCookieProvider + */ + public function testExpiredCookieFromResponseRemovesExistingCookie(string $header): void + { + $request = new Request('GET', 'https://example.com/'); + $this->jar->extractCookies($request, new Response(200, ['Set-Cookie' => 'sid=abc; Expires=Wed, 21 Oct 2037 07:28:00 GMT; Path=/'])); + self::assertCount(1, $this->jar); + + $this->jar->extractCookies($request, new Response(200, ['Set-Cookie' => $header])); + + self::assertCount(0, $this->jar); + self::assertFalse($this->jar->withCookieHeader($request)->hasHeader('Cookie')); + } + + public static function expiredCookieProvider(): array + { + return [ + 'same value' => ['sid=abc; Expires=Thu, 01 Jan 1970 00:00:01 GMT; Path=/'], + 'changed value' => ['sid=deleted; Expires=Thu, 01 Jan 1970 00:00:01 GMT; Path=/'], + ]; + } + + public function testConstructorDropsExpiredCookies(): void + { + $jar = new CookieJar(false, [ + ['Name' => 'sid', 'Value' => 'abc', 'Domain' => 'example.com', 'Path' => '/', 'Expires' => 1], + ]); + + self::assertCount(0, $jar); + } + public function testDoesNotSendHostOnlyCookieToSubdomain(): void { $this->jar->extractCookies( From 3236dc48e80dc43af94ddf6fc6461ac0c01c4999 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 25 Oct 2026 01:35:34 +0000 Subject: [PATCH 2/5] Replace same-value cookies whose expiry or flags change (#3990) --- CHANGELOG.md | 1 + src/Cookie/CookieJar.php | 23 +++++++---------------- tests/Cookie/CookieJarTest.php | 27 +++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e39e13ce..c2b760a2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Please refer to [UPGRADING](UPGRADING.md) guide for upgrading to a major version ### Fixed - Fix `CookieJar` storing already expired cookies instead of removing the stored cookie +- Fix `CookieJar` ignoring a same-value cookie that changes its expiry or its `Discard`, `Secure`, or `HttpOnly` flag ## 8.3.0 - 2026-10-18 diff --git a/src/Cookie/CookieJar.php b/src/Cookie/CookieJar.php index 946aed0ba..e3ed1d1e3 100644 --- a/src/Cookie/CookieJar.php +++ b/src/Cookie/CookieJar.php @@ -207,22 +207,13 @@ public function setCookie( continue; } - // The previously set cookie is a discard cookie and this one is - // not so allow the new cookie to be set - if (!$cookie->getDiscard() && $c->getDiscard()) { - unset($this->cookies[$i]); - continue; - } - - // If the new cookie's expiration is further into the future, then - // replace the old cookie - if ($cookie->getExpires() > $c->getExpires()) { - unset($this->cookies[$i]); - continue; - } - - // If the value has changed, we better change it - if ($cookie->getValue() !== $c->getValue()) { + // Replace the old cookie when its value, expiry or flags changed + if ($cookie->getValue() !== $c->getValue() + || $cookie->getExpires() !== $c->getExpires() + || $cookie->getDiscard() !== $c->getDiscard() + || $cookie->getSecure() !== $c->getSecure() + || $cookie->getHttpOnly() !== $c->getHttpOnly() + ) { unset($this->cookies[$i]); continue; } diff --git a/tests/Cookie/CookieJarTest.php b/tests/Cookie/CookieJarTest.php index 7badbabd0..2667b8add 100644 --- a/tests/Cookie/CookieJarTest.php +++ b/tests/Cookie/CookieJarTest.php @@ -592,6 +592,33 @@ public function testOverwritesCookiesThatHaveChanged(): void self::assertSame('zoo', $c[0]->getValue()); } + /** + * @dataProvider changedAttributeProvider + * + * @param int|bool|null $expected + */ + public function testReplacesCookieWhenAttributeChanges(string $header, string $attribute, $expected): void + { + $request = new Request('GET', 'https://example.com/'); + $this->jar->extractCookies($request, new Response(200, ['Set-Cookie' => 'sid=abc; Expires=Wed, 21 Oct 2037 07:28:00 GMT; Path=/'])); + + $this->jar->extractCookies($request, new Response(200, ['Set-Cookie' => $header])); + + self::assertCount(1, $this->jar); + self::assertSame($expected, $this->jar->toArray()[0][$attribute]); + } + + public static function changedAttributeProvider(): array + { + return [ + 'earlier expiry' => ['sid=abc; Expires=Tue, 21 Oct 2036 07:28:00 GMT; Path=/', 'Expires', 2108186880], + 'session cookie' => ['sid=abc; Path=/', 'Expires', null], + 'discard' => ['sid=abc; Expires=Wed, 21 Oct 2037 07:28:00 GMT; Path=/; Discard', 'Discard', true], + 'secure' => ['sid=abc; Expires=Wed, 21 Oct 2037 07:28:00 GMT; Path=/; Secure', 'Secure', true], + 'http only' => ['sid=abc; Expires=Wed, 21 Oct 2037 07:28:00 GMT; Path=/; HttpOnly', 'HttpOnly', true], + ]; + } + public function testDistinctNumericStringCookieNamesCoexist(): void { $this->jar->setCookie(new SetCookie([ From 89f429318d528d8f5364118b8d16f5136c2d0078 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 25 Oct 2026 01:43:47 +0000 Subject: [PATCH 3/5] Keep the stored expiry when rebuilding Max-Age cookies (#3991) --- CHANGELOG.md | 1 + UPGRADING.md | 10 +++++---- src/Cookie/SetCookie.php | 9 ++++++-- tests/Cookie/FileCookieJarTest.php | 21 ++++++++++++++++++ tests/Cookie/SetCookieTest.php | 35 +++++++++++++++++++----------- 5 files changed, 57 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2b760a2e..019430383 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Please refer to [UPGRADING](UPGRADING.md) guide for upgrading to a major version - Fix `CookieJar` storing already expired cookies instead of removing the stored cookie - Fix `CookieJar` ignoring a same-value cookie that changes its expiry or its `Discard`, `Secure`, or `HttpOnly` flag +- Fix `SetCookie` renewing the lifetime of `Max-Age` cookies rebuilt from an array, so persisted cookies never expired ## 8.3.0 - 2026-10-18 diff --git a/UPGRADING.md b/UPGRADING.md index ea289a62c..65bc70754 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -1205,10 +1205,12 @@ Cookies parsed from normal `Set-Cookie` headers continue to be normalized by #### SetCookie Max-Age Precedence -`SetCookie` now follows RFC cookie precedence when both `Max-Age` and `Expires` -are present. A valid `Max-Age` value controls the effective expiration time, -including `Max-Age=0` expiring the cookie immediately, even when `Expires` is a -future date. +`SetCookie::fromString()` now follows RFC cookie precedence when a `Set-Cookie` +header carries both `Max-Age` and `Expires`. A valid `Max-Age` value controls +the effective expiration time, including `Max-Age=0` expiring the cookie +immediately, even when `Expires` is a future date. Cookies built from an array +keep a supplied `Expires`, as in Guzzle 7, so persisted cookies do not restart +their lifetime when a jar is loaded. #### Cookie Max-Age Parsing diff --git a/src/Cookie/SetCookie.php b/src/Cookie/SetCookie.php index c5a6f2711..6ce2213de 100644 --- a/src/Cookie/SetCookie.php +++ b/src/Cookie/SetCookie.php @@ -96,6 +96,11 @@ public static function fromString( } } + // Max-Age takes precedence over Expires, RFC 6265 section 5.3 + if ($data['Max-Age'] !== null) { + $data['Expires'] = null; + } + return new self($data); } @@ -155,9 +160,9 @@ public function __construct( $this->data[$key] = $data[$key]; } - // Extract the Expires value and turn it into a UNIX timestamp if needed + // Derive Expires from Max-Age unless the data already carries one $maxAge = $this->getMaxAge(); - if ($maxAge !== null) { + if ($maxAge !== null && $this->getExpires() === null) { $this->setExpires(self::maxAgeToExpires($maxAge, \time())); } } diff --git a/tests/Cookie/FileCookieJarTest.php b/tests/Cookie/FileCookieJarTest.php index 5197ecf79..ebade2149 100644 --- a/tests/Cookie/FileCookieJarTest.php +++ b/tests/Cookie/FileCookieJarTest.php @@ -6,6 +6,7 @@ use GuzzleHttp\Cookie\FileCookieJar; use GuzzleHttp\Cookie\SetCookie; +use GuzzleHttp\Psr7\Request; use PHPUnit\Framework\TestCase; /** @@ -112,6 +113,26 @@ public function testLoadDoesNotChangeJarWhenLaterRecordIsInvalid(): void } } + public function testLoadDoesNotExtendMaxAgeCookies(): void + { + \file_put_contents($this->file, \json_encode([[ + 'Name' => 'sid', + 'Value' => 'abc', + 'Domain' => 'example.com', + 'Path' => '/', + 'Max-Age' => 3600, + 'Expires' => 1, + 'Secure' => false, + 'Discard' => false, + 'HttpOnly' => false, + 'HostOnly' => true, + ]])); + + $jar = new FileCookieJar($this->file); + + self::assertFalse($jar->withCookieHeader(new Request('GET', 'https://example.com/'))->hasHeader('Cookie')); + } + public function testRejectsCookieDataThatCannotBeEncoded(): void { $jar = new FileCookieJar($this->file, true); diff --git a/tests/Cookie/SetCookieTest.php b/tests/Cookie/SetCookieTest.php index 7ece88320..5d3187699 100644 --- a/tests/Cookie/SetCookieTest.php +++ b/tests/Cookie/SetCookieTest.php @@ -59,6 +59,24 @@ public function testAddsExpiresBasedOnMaxAge(): void self::assertLessThanOrEqual($after + 100, $expires); } + public function testKeepsExpiresFromDataWithMaxAge(): void + { + $cookie = new SetCookie(['Max-Age' => 60, 'Expires' => 1]); + + self::assertSame(1, $cookie->getExpires()); + self::assertTrue($cookie->isExpired()); + } + + public function testMaxAgeOverridesExpiresInSetCookieString(): void + { + $before = \time(); + $cookie = SetCookie::fromString('sid=abc; Expires=Wed, 21 Oct 2037 07:28:00 GMT; Max-Age=60'); + $after = \time(); + + self::assertGreaterThanOrEqual($before + 60, $cookie->getExpires()); + self::assertLessThanOrEqual($after + 60, $cookie->getExpires()); + } + public function testMaxAgeZeroExpiresCookie(): void { $cookie = SetCookie::fromString('sid=abc; Max-Age=0'); @@ -119,13 +137,13 @@ public function testFromStringIgnoresValuelessTypedAttributes(): void public function testHoldsValues(): void { - $before = \time(); + $t = \time(); $data = [ 'Name' => 'foo', 'Value' => 'baz', 'Path' => '/bar', 'Domain' => 'baz.com', - 'Expires' => $before, + 'Expires' => $t, 'Max-Age' => 100, 'Secure' => true, 'Discard' => true, @@ -135,22 +153,13 @@ public function testHoldsValues(): void ]; $cookie = new SetCookie($data); - $after = \time(); - $expires = $cookie->getExpires(); - - self::assertIsInt($expires); - self::assertGreaterThanOrEqual($before + 100, $expires); - self::assertLessThanOrEqual($after + 100, $expires); - - $expected = $data; - $expected['Expires'] = $expires; - self::assertEquals($expected, $cookie->toArray()); + self::assertEquals($data, $cookie->toArray()); self::assertSame('foo', $cookie->getName()); self::assertSame('baz', $cookie->getValue()); self::assertSame('baz.com', $cookie->getDomain()); self::assertSame('/bar', $cookie->getPath()); - self::assertSame($expires, $cookie->getExpires()); + self::assertSame($t, $cookie->getExpires()); self::assertSame(100, $cookie->getMaxAge()); self::assertTrue($cookie->getSecure()); self::assertTrue($cookie->getDiscard()); From 2567cb48392b8827cd73243a443a7fd3e79b27a7 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 25 Oct 2026 15:43:16 +0000 Subject: [PATCH 4/5] Time only the attach in the ticking delayed request test (#3994) --- tests/Handler/CurlMultiHandlerTest.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/Handler/CurlMultiHandlerTest.php b/tests/Handler/CurlMultiHandlerTest.php index ef77baf6d..15121a4ea 100644 --- a/tests/Handler/CurlMultiHandlerTest.php +++ b/tests/Handler/CurlMultiHandlerTest.php @@ -323,16 +323,18 @@ public function testDelayedRequestAttachesBeforeSiblingSelectTimeoutWhenTicking( $delayed = $handler(new Request('GET', Server::$url), ['delay' => 100]); + // Stop at the attach: the tick that completes the delayed transfer may + // still select for the sibling's full timeout. $start = \microtime(true); $deadline = $start + 10; - while (P\State::isPending($delayed) && \microtime(true) < $deadline) { + while (self::readMultiProperty($handler, 'delays') !== [] && \microtime(true) < $deadline) { $handler->tick(); } $elapsed = \microtime(true) - $start; - self::assertTrue(P\State::isFulfilled($delayed)); - self::assertSame(200, $delayed->wait()->getStatusCode()); + self::assertSame([], self::readMultiProperty($handler, 'delays'), 'Timed out waiting for the delayed request to attach.'); self::assertLessThan(2.5, $elapsed, 'The delayed request waited for an unrelated transfer before attaching.'); + self::assertSame(200, $delayed->wait()->getStatusCode()); self::assertTrue(P\State::isPending($sibling)); } finally { $sibling->cancel(); From 7d9b1f430bb65ac664e120af227bf9a795ad6333 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Sun, 25 Oct 2026 21:35:34 +0000 Subject: [PATCH 5/5] Release 8.3.1 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 019430383..f411b2793 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ Please refer to [UPGRADING](UPGRADING.md) guide for upgrading to a major version. -## 8.3.1 - Upcoming +## 8.3.1 - 2026-10-25 ### Fixed