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

Commit b762d04

Browse files
committed
Fixes #95: SetCookie::setMaxAge() null, negative, and non-numeric redefinition
1 parent 8c23aaa commit b762d04

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
@@ -141,7 +141,7 @@ public static function fromString($headerLine, $bypassHeaderFieldName = false)
141141
$header->setVersion((int) $headerValue);
142142
break;
143143
case 'maxage':
144-
$header->setMaxAge((int) $headerValue);
144+
$header->setMaxAge($headerValue);
145145
break;
146146
default:
147147
// Intentionally omitted
@@ -344,14 +344,19 @@ public function getVersion()
344344
* Set Max-Age
345345
*
346346
* @param int $maxAge
347-
* @throws Exception\InvalidArgumentException
348347
* @return SetCookie
349348
*/
350349
public function setMaxAge($maxAge)
351350
{
352-
if ($maxAge !== null && (! is_int($maxAge) || ($maxAge < 0))) {
353-
throw new Exception\InvalidArgumentException('Invalid Max-Age number specified');
351+
if ($maxAge === null || ! is_numeric($maxAge)) {
352+
return $this;
354353
}
354+
355+
$maxAge = (int) $maxAge;
356+
if ($maxAge < 0) {
357+
$maxAge = 0;
358+
}
359+
355360
$this->maxAge = $maxAge;
356361
return $this;
357362
}

test/Header/SetCookieTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,33 @@ public static function validCookieWithInfoProvider()
645645
],
646646
'emptykey=; Domain=docs.foo.com'
647647
],
648+
[
649+
'Set-Cookie: emptykey; Domain=docs.foo.com; Max-Age=foo;',
650+
[
651+
'name' => 'myname',
652+
'value' => '',
653+
'domain' => 'docs.foo.com',
654+
],
655+
'emptykey=; Domain=docs.foo.com'
656+
],
657+
[
658+
'Set-Cookie: emptykey; Domain=docs.foo.com; Max-Age=-1480312904;',
659+
[
660+
'name' => 'myname',
661+
'value' => '',
662+
'domain' => 'docs.foo.com',
663+
],
664+
'emptykey=; Max-Age=0; Domain=docs.foo.com'
665+
],
666+
[
667+
'Set-Cookie: emptykey; Domain=docs.foo.com; Max-Age=100;',
668+
[
669+
'name' => 'myname',
670+
'value' => '',
671+
'domain' => 'docs.foo.com',
672+
],
673+
'emptykey=; Max-Age=100; Domain=docs.foo.com'
674+
],
648675
];
649676
}
650677
}

0 commit comments

Comments
 (0)