Skip to content

[HttpFoundation] Validate/cast cookie expire time #20925

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
Jan 3, 2017
Merged

[HttpFoundation] Validate/cast cookie expire time #20925

merged 1 commit into from
Jan 3, 2017

Conversation

ro0NL
Copy link
Contributor

@ro0NL ro0NL commented Dec 14, 2016

Q A
Branch? 2.7
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #...
License MIT
Doc PR symfony/symfony-docs#...

@@ -63,15 +63,19 @@ public function __construct($name, $value = null, $expire = 0, $path = '/', $dom
} elseif (!is_numeric($expire)) {
$expire = strtotime($expire);

if (false === $expire || -1 === $expire) {
Copy link
Member

Choose a reason for hiding this comment

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

👍 -1 was returned by strtotime on PHP 5.1

Copy link
Contributor Author

@ro0NL ro0NL Dec 15, 2016

Choose a reason for hiding this comment

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

Prior to 5.1.0 actually ;) do you want to remove it in 2.7? Im fine with it as is.

@@ -81,6 +89,13 @@ public function testGetExpiresTime()
$this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
}

public function testGetExpiresTimeIsCastedToInt()
Copy link
Member

Choose a reason for hiding this comment

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

testGetExpiresTimeIsCastToInt

{
$cookie = new Cookie('foo', 'bar', 3600.9);

$this->assertEquals(3600, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date as an integer');
Copy link
Member

Choose a reason for hiding this comment

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

assertSame

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch!

throw new \InvalidArgumentException('The cookie expiration time is not valid.');
}
}

if (0 > $expire) {
throw new \InvalidArgumentException('The cookie expiration time must be a positive number value.');
Copy link
Member

Choose a reason for hiding this comment

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

This is a bc break if anyone is doing this for some reason.
We could deprecate, but the behavior is just fine when negative numbers are used here.
Instead, I'd rather fix getMaxAge and turn 0 !== $this->expire into 0 >= $this->expire.
and merge this in a lower branch

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we should indeed settle with >=. Nice one.. didnt thought of it 👍

@ro0NL ro0NL changed the base branch from master to 2.7 December 27, 2016 23:09
@nicolas-grekas
Copy link
Member

👍

throw new \InvalidArgumentException('The cookie expiration time is not valid.');
}
}

$this->name = $name;
$this->value = $value;
$this->domain = $domain;
$this->expire = $expire;
$this->expire = 0 < $expire ? (int) $expire : 0;
Copy link
Member

Choose a reason for hiding this comment

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

$this->expire = max(0, (int) $expire) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure the extra function call is really worth it. Tend to keep it as is.

@nicolas-grekas nicolas-grekas added this to the 2.7 milestone Jan 2, 2017
@fabpot
Copy link
Member

fabpot commented Jan 3, 2017

Thank you @ro0NL.

@fabpot fabpot merged commit 8215dbd into symfony:2.7 Jan 3, 2017
fabpot added a commit that referenced this pull request Jan 3, 2017
This PR was merged into the 2.7 branch.

Discussion
----------

[HttpFoundation] Validate/cast cookie expire time

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #... <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!--highly recommended for new features-->

Commits
-------

8215dbd [HttpFoundation] Validate/cast cookie expire time
@ro0NL ro0NL deleted the http-foundation/unsigned-cookie-expire branch January 3, 2017 07:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants