Skip to content

[HttpFoundation] Expired cookies string representation consistency & tests #38369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/Symfony/Component/HttpFoundation/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ public static function fromString(string $cookie, bool $decode = false)
$value = isset($part[1]) ? ($decode ? urldecode($part[1]) : $part[1]) : null;

$data = HeaderUtils::combine($parts) + $data;
$data['expires'] = self::expiresTimestamp($data['expires']);

if (isset($data['max-age'])) {
if (isset($data['max-age']) && ($data['max-age'] > 0 || $data['expires'] > time())) {
$data['expires'] = time() + (int) $data['max-age'];
}

Expand Down Expand Up @@ -102,7 +103,7 @@ public function __construct(string $name, string $value = null, $expire = 0, ?st
$this->name = $name;
$this->value = $value;
$this->domain = $domain;
$this->expire = $this->withExpires($expire)->expire;
$this->expire = self::expiresTimestamp($expire);
$this->path = empty($path) ? '/' : $path;
$this->secure = $secure;
$this->httpOnly = $httpOnly;
Expand Down Expand Up @@ -144,6 +145,21 @@ public function withDomain(?string $domain): self
* @return static
*/
public function withExpires($expire = 0): self
{
$cookie = clone $this;
$cookie->expire = self::expiresTimestamp($expire);

return $cookie;
}

/**
* Converts expires formats to a unix timestamp.
*
* @param int|string|\DateTimeInterface $expire
*
* @return int
*/
private static function expiresTimestamp($expire = 0)
{
// convert expiration time to a Unix timestamp
if ($expire instanceof \DateTimeInterface) {
Expand All @@ -156,10 +172,7 @@ public function withExpires($expire = 0): self
}
}

$cookie = clone $this;
$cookie->expire = 0 < $expire ? (int) $expire : 0;

return $cookie;
return 0 < $expire ? (int) $expire : 0;
}

/**
Expand Down
29 changes: 29 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/CookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,33 @@ public function testSetSecureDefault()

$this->assertFalse($cookie->isSecure());
}

public function testMaxAge()
{
$futureDateOneHour = gmdate('D, d-M-Y H:i:s T', time() + 3600);

$cookie = Cookie::fromString('foo=bar; Max-Age=3600; path=/');
$this->assertEquals('foo=bar; expires='.$futureDateOneHour.'; Max-Age=3600; path=/', $cookie->__toString());

$cookie = Cookie::fromString('foo=bar; expires='.$futureDateOneHour.'; Max-Age=3600; path=/');
$this->assertEquals('foo=bar; expires='.$futureDateOneHour.'; Max-Age=3600; path=/', $cookie->__toString());

$futureDateHalfHour = gmdate('D, d-M-Y H:i:s T', time() + 1800);

// Max-Age value takes precedence before expires
$cookie = Cookie::fromString('foo=bar; expires='.$futureDateHalfHour.'; Max-Age=3600; path=/');
$this->assertEquals('foo=bar; expires='.$futureDateOneHour.'; Max-Age=3600; path=/', $cookie->__toString());
}

public function testExpiredWithMaxAge()
{
$cookie = Cookie::fromString('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; Max-Age=0; path=/');
$this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; Max-Age=0; path=/', $cookie->__toString());

$futureDate = gmdate('D, d-M-Y H:i:s T', time() + 864000);

$cookie = Cookie::fromString('foo=bar; expires='.$futureDate.'; Max-Age=0; path=/');
$this->assertEquals(time(), $cookie->getExpiresTime());
$this->assertEquals('foo=bar; expires='.gmdate('D, d-M-Y H:i:s T', $cookie->getExpiresTime()).'; Max-Age=0; path=/', $cookie->__toString());
}
}