Skip to content

Commit

Permalink
Make buildSetCookieHeaderFromBrowserCookie robust
Browse files Browse the repository at this point in the history
  • Loading branch information
szepeviktor authored Oct 23, 2024
1 parent 208b29f commit 85fe283
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions src/Loader/Http/Cookies/CookieJar.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,37 +123,42 @@ protected function getForDomainFromUrl(string|UriInterface|Url $url): ?string

protected function buildSetCookieHeaderFromBrowserCookie(BrowserCookie $cookie): string
{
$header = $cookie->getName() . '=' . $cookie->getValue();

if ($cookie->getDomain() !== null) {
$header .= '; Domain=' . $cookie->getDomain();
}

if ($cookie->offsetExists('expires') && $cookie->offsetGet('expires') !== -1) {
$header .= '; Expires=' . $this->formatExpiresValue($cookie->offsetGet('expires'));
}

if ($cookie->offsetExists('max-age') && !empty($cookie->offsetGet('max-age'))) {
$header .= '; Max-Age=' . $cookie->offsetGet('max-age');
}

if ($cookie->offsetExists('path') && !empty($cookie->offsetGet('path'))) {
$header .= '; Path=' . $cookie->offsetGet('path');
}
$attributes = [
'domain' => 'Domain',
'expires' => 'Expires',
'max-age' => 'Max-Age',
'path' => 'Path',
'secure' => 'Secure',
'httpOnly' => 'HttpOnly',
'sameSite' => 'SameSite',
];

$header = [sprintf('%s=%s', $cookie->getName(), $cookie->getValue())];

foreach ($attributes as $name => $setCookieName) {
$setCookieValue = $cookie->offsetGet($name);
if ($setCookieValue === null) {
continue;
}

if ($cookie->offsetExists('secure') && $cookie->offsetGet('secure') === true) {
$header .= '; Secure';
}
// "Expires" attribute
if ($name === 'expires')) {

Check failure on line 145 in src/Loader/Http/Cookies/CookieJar.php

View workflow job for this annotation

GitHub Actions / PHPStan

Syntax error, unexpected ')' on line 145
if ($setCookieValue !== -1) {
$header[] = sprintf('%s=%s', $setCookieName, $this->formatExpiresValue($setCookieValue));
}
continue;
}

if ($cookie->offsetExists('httpOnly') && $cookie->offsetGet('httpOnly') === true) {
$header .= '; HttpOnly';
}
// Flag attributes
if ($setCookieValue === true) {
$header[] = $setCookieName;
continue;
}

if ($cookie->offsetExists('sameSite') && !empty($cookie->offsetGet('sameSite'))) {
$header .= '; SameSite=' . $cookie->offsetGet('sameSite');
$header[] = sprintf('%s=%s', $setCookieName, $setCookieValue);
}

return $header;
return implode('; ', $header);
}

private function formatExpiresValue(mixed $value): string
Expand Down

0 comments on commit 85fe283

Please sign in to comment.