Skip to content

Commit 72d7e28

Browse files
ajgarlagsagikazarmark
authored andcommitted
Optional cookie validation (#68)
* Specification for optional cookie validation * Adds Cookie::createWithoutValidation and Cookie::isValid Adds a named constructor to avoid cookie attributes validation during instantination, and a method to check if cookie attributes are valid. * Creates new cookie instance with dummy name to bypass constructor validation * Remove deprecation of attribute validation in Cookie::__construct * Adds docblock to Cookie::createWithoutValidation * Drop use of dataProvider in Cookie::createWithoutValidation spec
1 parent 6ffe75a commit 72d7e28

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

CHANGELOG.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33

44
## Unreleased
55

6-
## Added
6+
### Added
77

88
- Check for empty string in Stream factories
9+
- Cookie::createWithoutValidation Static constructor to create a cookie. Will not perform any attribute validation during instantiation.
10+
- Cookie::isValid Method to check if cookie attributes are valid.
911

10-
## Fixed
12+
### Fixed
1113

12-
- FilteredStream::getSize returns null because the contents size is unknown.
14+
- FilteredStream::getSize returns null because the contents size is unknown.
1315

1416
### Deprecated
1517

16-
- FilteredStream::getReadFilter The read filter is internal and should never be used by consuming code.
17-
- FilteredStream::getWriteFilter We did not implement writing to the streams at all. And if we do, the filter is an internal information and should not be used by consuming code.
18+
- FilteredStream::getReadFilter The read filter is internal and should never be used by consuming code.
19+
- FilteredStream::getWriteFilter We did not implement writing to the streams at all. And if we do, the filter is an internal information and should not be used by consuming code.
1820

1921

2022
## 1.4.1 - 2016-12-16

spec/CookieSpec.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,32 @@ function it_matches_other_cookies()
239239
$this->match($notMatches)->shouldReturn(false);
240240
}
241241

242+
function it_validates_itself()
243+
{
244+
$this->isValid()->shouldReturn(true);
245+
}
246+
247+
function it_can_be_constructed_without_name_validation()
248+
{
249+
$this->beConstructedThrough('createWithoutValidation', ["\x20"]);
250+
251+
$this->isValid()->shouldReturn(false);
252+
}
253+
254+
function it_can_be_constructed_without_value_validation()
255+
{
256+
$this->beConstructedThrough('createWithoutValidation', ['name', "\x20"]);
257+
258+
$this->isValid()->shouldReturn(false);
259+
}
260+
261+
function it_can_be_constructed_without_max_age_validation()
262+
{
263+
$this->beConstructedThrough('createWithoutValidation', ['name', 'value', '-1']);
264+
265+
$this->isValid()->shouldReturn(false);
266+
}
267+
242268
/**
243269
* Provides examples for invalid characers in names and values.
244270
*

src/Cookie.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,36 @@ public function __construct(
8989
$this->httpOnly = (bool) $httpOnly;
9090
}
9191

92+
/**
93+
* Creates a new cookie without any attribute validation.
94+
*
95+
* @param string $name
96+
* @param string|null $value
97+
* @param int $maxAge
98+
* @param string|null $domain
99+
* @param string|null $path
100+
* @param bool $secure
101+
* @param bool $httpOnly
102+
* @param \DateTime|null $expires Expires attribute is HTTP 1.0 only and should be avoided.
103+
*/
104+
public static function createWithoutValidation(
105+
$name,
106+
$value = null,
107+
$maxAge = null,
108+
$domain = null,
109+
$path = null,
110+
$secure = false,
111+
$httpOnly = false,
112+
\DateTime $expires = null
113+
) {
114+
$cookie = new self('name', null, null, $domain, $path, $secure, $httpOnly, $expires);
115+
$cookie->name = $name;
116+
$cookie->value = $value;
117+
$cookie->maxAge = $maxAge;
118+
119+
return $cookie;
120+
}
121+
92122
/**
93123
* Returns the name.
94124
*
@@ -380,6 +410,24 @@ public function match(Cookie $cookie)
380410
return $this->name === $cookie->name && $this->domain === $cookie->domain and $this->path === $cookie->path;
381411
}
382412

413+
/**
414+
* Validates cookie attributes.
415+
*
416+
* @return bool
417+
*/
418+
public function isValid()
419+
{
420+
try {
421+
$this->validateName($this->name);
422+
$this->validateValue($this->value);
423+
$this->validateMaxAge($this->maxAge);
424+
} catch (\InvalidArgumentException $e) {
425+
return false;
426+
}
427+
428+
return true;
429+
}
430+
383431
/**
384432
* Validates the name attribute.
385433
*

0 commit comments

Comments
 (0)