Skip to content

Optional cookie validation #68

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
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
12 changes: 7 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@

## Unreleased

## Added
### Added

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

## Fixed
### Fixed

- FilteredStream::getSize returns null because the contents size is unknown.
- FilteredStream::getSize returns null because the contents size is unknown.

### Deprecated

- FilteredStream::getReadFilter The read filter is internal and should never be used by consuming code.
- 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.
- FilteredStream::getReadFilter The read filter is internal and should never be used by consuming code.
- 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.


## 1.4.1 - 2016-12-16
Expand Down
26 changes: 26 additions & 0 deletions spec/CookieSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,32 @@ function it_matches_other_cookies()
$this->match($notMatches)->shouldReturn(false);
}

function it_validates_itself()
{
$this->isValid()->shouldReturn(true);
}

function it_can_be_constructed_without_name_validation()
{
$this->beConstructedThrough('createWithoutValidation', ["\x20"]);

$this->isValid()->shouldReturn(false);
}

function it_can_be_constructed_without_value_validation()
{
$this->beConstructedThrough('createWithoutValidation', ['name', "\x20"]);

$this->isValid()->shouldReturn(false);
}

function it_can_be_constructed_without_max_age_validation()
{
$this->beConstructedThrough('createWithoutValidation', ['name', 'value', '-1']);

$this->isValid()->shouldReturn(false);
}

/**
* Provides examples for invalid characers in names and values.
*
Expand Down
48 changes: 48 additions & 0 deletions src/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,36 @@ public function __construct(
$this->httpOnly = (bool) $httpOnly;
}

/**
* Creates a new cookie without any attribute validation.
*
* @param string $name
* @param string|null $value
* @param int $maxAge
* @param string|null $domain
* @param string|null $path
* @param bool $secure
* @param bool $httpOnly
* @param \DateTime|null $expires Expires attribute is HTTP 1.0 only and should be avoided.
*/
public static function createWithoutValidation(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add phpdoc?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be deprecated as well? That would be weird, but still...

$name,
$value = null,
$maxAge = null,
$domain = null,
$path = null,
$secure = false,
$httpOnly = false,
\DateTime $expires = null
) {
$cookie = new self('name', null, null, $domain, $path, $secure, $httpOnly, $expires);
$cookie->name = $name;
$cookie->value = $value;
$cookie->maxAge = $maxAge;

return $cookie;
}

/**
* Returns the name.
*
Expand Down Expand Up @@ -380,6 +410,24 @@ public function match(Cookie $cookie)
return $this->name === $cookie->name && $this->domain === $cookie->domain and $this->path === $cookie->path;
}

/**
* Validates cookie attributes.
*
* @return bool
*/
public function isValid()
{
try {
$this->validateName($this->name);
$this->validateValue($this->value);
$this->validateMaxAge($this->maxAge);
} catch (\InvalidArgumentException $e) {
return false;
}

return true;
}

/**
* Validates the name attribute.
*
Expand Down