Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit 464c5c7

Browse files
samsonasikXerkus
authored andcommitted
Fixes #95: SetCookie::setMaxAge() null, negative, and non-numeric redefinition
1 parent c8e3397 commit 464c5c7

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/Header/SetCookie.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public static function fromString($headerLine, $bypassHeaderFieldName = false)
139139
$header->setVersion((int) $headerValue);
140140
break;
141141
case 'maxage':
142-
$header->setMaxAge((int) $headerValue);
142+
$header->setMaxAge($headerValue);
143143
break;
144144
default:
145145
// Intentionally omitted
@@ -342,14 +342,19 @@ public function getVersion()
342342
* Set Max-Age
343343
*
344344
* @param int $maxAge
345-
* @throws Exception\InvalidArgumentException
346345
* @return SetCookie
347346
*/
348347
public function setMaxAge($maxAge)
349348
{
350-
if ($maxAge !== null && (! is_int($maxAge) || ($maxAge < 0))) {
351-
throw new Exception\InvalidArgumentException('Invalid Max-Age number specified');
349+
if ($maxAge === null || ! is_numeric($maxAge)) {
350+
return $this;
352351
}
352+
353+
$maxAge = (int) $maxAge;
354+
if ($maxAge < 0) {
355+
$maxAge = 0;
356+
}
357+
353358
$this->maxAge = $maxAge;
354359
return $this;
355360
}

test/Header/SetCookieTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,33 @@ public static function validCookieWithInfoProvider()
661661
],
662662
'emptykey=; Domain=docs.foo.com',
663663
],
664+
[
665+
'Set-Cookie: emptykey; Domain=docs.foo.com; Max-Age=foo;',
666+
[
667+
'name' => 'myname',
668+
'value' => '',
669+
'domain' => 'docs.foo.com',
670+
],
671+
'emptykey=; Domain=docs.foo.com'
672+
],
673+
[
674+
'Set-Cookie: emptykey; Domain=docs.foo.com; Max-Age=-1480312904;',
675+
[
676+
'name' => 'myname',
677+
'value' => '',
678+
'domain' => 'docs.foo.com',
679+
],
680+
'emptykey=; Max-Age=0; Domain=docs.foo.com'
681+
],
682+
[
683+
'Set-Cookie: emptykey; Domain=docs.foo.com; Max-Age=100;',
684+
[
685+
'name' => 'myname',
686+
'value' => '',
687+
'domain' => 'docs.foo.com',
688+
],
689+
'emptykey=; Max-Age=100; Domain=docs.foo.com'
690+
],
664691
];
665692
}
666693
}

0 commit comments

Comments
 (0)