From 5ab98ce829819f2ef66b0922d20a00e3aee9d30c Mon Sep 17 00:00:00 2001 From: otsch Date: Tue, 22 Oct 2024 12:04:55 +0200 Subject: [PATCH] Fix converting chrome-php cookie objects Fixes issue when converting cookie objects received from the chrome-php library. --- CHANGELOG.md | 4 ++ src/Loader/Http/Cookies/CookieJar.php | 7 ++-- tests/Loader/Http/Cookies/CookieJarTest.php | 42 +++++++++++++++++++-- 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f680e0..64f7b6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [2.1.2] - 2024-10-22 +### Fixed +* Issue when converting cookie objects received from the chrome-php library. + ## [2.1.1] - 2024-10-21 ### Fixed * Also add cookies, set during headless browser usage, to the cookie jar. When switching back to the (guzzle) HTTP client the cookies should also be sent. diff --git a/src/Loader/Http/Cookies/CookieJar.php b/src/Loader/Http/Cookies/CookieJar.php index 35b6b5f..3a8d529 100644 --- a/src/Loader/Http/Cookies/CookieJar.php +++ b/src/Loader/Http/Cookies/CookieJar.php @@ -6,6 +6,7 @@ use Crwlr\Url\Url; use DateTime; use Exception; +use HeadlessChromium\Cookies\Cookie as BrowserCookie; use HeadlessChromium\Cookies\CookiesCollection; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; @@ -120,7 +121,7 @@ protected function getForDomainFromUrl(string|UriInterface|Url $url): ?string return $forDomain; } - protected function buildSetCookieHeaderFromBrowserCookie(\HeadlessChromium\Cookies\Cookie $cookie): string + protected function buildSetCookieHeaderFromBrowserCookie(BrowserCookie $cookie): string { $header = $cookie->getName() . '=' . $cookie->getValue(); @@ -140,8 +141,8 @@ protected function buildSetCookieHeaderFromBrowserCookie(\HeadlessChromium\Cooki $header .= '; Path=' . $cookie->offsetGet('path'); } - if ($cookie->offsetExists('secure') && !empty($cookie->offsetGet('secure'))) { - $header .= '; Secure=' . $cookie->offsetGet('path'); + if ($cookie->offsetExists('secure') && $cookie->offsetGet('secure') === true) { + $header .= '; Secure'; } if ($cookie->offsetExists('httpOnly') && $cookie->offsetGet('httpOnly') === true) { diff --git a/tests/Loader/Http/Cookies/CookieJarTest.php b/tests/Loader/Http/Cookies/CookieJarTest.php index 03b734e..19a6c14 100644 --- a/tests/Loader/Http/Cookies/CookieJarTest.php +++ b/tests/Loader/Http/Cookies/CookieJarTest.php @@ -48,16 +48,52 @@ $jar = new CookieJar(); $jar->addFrom(Url::parse('https://www.crwl.io'), new CookiesCollection([ - new Cookie(['name' => 'foo', 'value' => 'one', 'domain' => '.www.crwl.io', 'expires' => '1745068860']), - new Cookie(['name' => 'bar', 'value' => 'two', 'domain' => '.www.crwl.io', 'expires' => '1729603260.5272']), - new Cookie(['name' => 'baz', 'value' => 'three', 'domain' => '.www.crwl.io', 'expires' => '1764076860.878']), + new Cookie([ + 'name' => 'foo', + 'value' => 'one', + 'domain' => '.www.crwl.io', + 'expires' => '1745068860', + 'max-age' => '86400', + 'secure' => true, + 'httpOnly' => true, + 'sameSite' => 'Strict', + ]), + new Cookie([ + 'name' => 'bar', + 'value' => 'two', + 'domain' => '.www.crwl.io', + 'expires' => '1729603260.5272', + 'path' => '/bar', + ]), + new Cookie([ + 'name' => 'baz', + 'value' => 'three', + 'domain' => '.www.crwl.io', + 'expires' => '1764076860.878', + ]), ])); $allCookiesForDomain = $jar->allByDomain('crwl.io'); expect($allCookiesForDomain)->toHaveCount(3) ->and($allCookiesForDomain['foo']->expires()?->dateTime()->format('Y-m-d H:i'))->toBe('2025-04-19 13:21') + ->and($allCookiesForDomain['foo']->name())->toBe('foo') + ->and($allCookiesForDomain['foo']->value())->toBe('one') + ->and($allCookiesForDomain['foo']->domain())->toBe('www.crwl.io') + ->and($allCookiesForDomain['foo']->maxAge())->toBe(86400) + ->and($allCookiesForDomain['foo']->path())->toBeNull() + ->and($allCookiesForDomain['foo']->secure())->toBeTrue() + ->and($allCookiesForDomain['foo']->httpOnly())->toBeTrue() + ->and($allCookiesForDomain['foo']->sameSite())->toBe('Strict') ->and($allCookiesForDomain['bar']->expires()?->dateTime()->format('Y-m-d H:i'))->toBe('2024-10-22 13:21') + ->and($allCookiesForDomain['bar']->name())->toBe('bar') + ->and($allCookiesForDomain['bar']->value())->toBe('two') + ->and($allCookiesForDomain['bar']->domain())->toBe('www.crwl.io') + ->and($allCookiesForDomain['bar']->maxAge())->toBeNull() + ->and($allCookiesForDomain['bar']->path())->toBe('/bar') + ->and($allCookiesForDomain['bar']->secure())->toBeFalse() + ->and($allCookiesForDomain['bar']->httpOnly())->toBeFalse() + ->and($allCookiesForDomain['bar']->sameSite())->toBe('Lax') ->and($allCookiesForDomain['baz']->expires()?->dateTime()->format('Y-m-d H:i'))->toBe('2025-11-25 13:21'); });