Skip to content

Commit 655a9e4

Browse files
committed
Simplified changeset to only include 4.x compatibility
1 parent 5912351 commit 655a9e4

File tree

7 files changed

+27
-111
lines changed

7 files changed

+27
-111
lines changed

docs/en/02_Developer_Guides/09_Security/04_Secure_Coding.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -759,9 +759,7 @@ as this prevents an attacker who achieves cross-site scripting from accessing th
759759
```php
760760
use SilverStripe\Control\Cookie;
761761
762-
Cookie::set('cookie-name', 'chocolate-chip', $expiry = strtotime('+30 days'), $path = null, $domain = null, $secure = true,
763-
$httpOnly = false
764-
);
762+
Cookie::set('cookie-name', 'chocolate-chip', $expiry = 30, $path = null, $domain = null, $secure = true, $httpOnly = false);
765763
```
766764

767765
## Security Headers

docs/en/02_Developer_Guides/18_Cookies_And_Sessions/01_Cookies.md

+5-43
Original file line numberDiff line numberDiff line change
@@ -17,55 +17,17 @@ Sets the value of cookie with configuration.
1717
```php
1818
use SilverStripe\Control\Cookie;
1919

20-
Cookie::set($name, $value, $expiry = strtotime('+90 days'), $path = null, $domain = null, $secure = false, $httpOnly = false);
20+
Cookie::set($name, $value, $expiry = 90, $path = null, $domain = null, $secure = false, $httpOnly = false);
2121

2222
// Cookie::set('MyApplicationPreference', 'Yes');
2323
```
2424

2525
#### Expiry
2626

27-
Expiry value is a Unix timestamp of the time the cookie expires (0 expires at the end of the current session).
28-
29-
##### Default expiry
30-
31-
The default expiry is 90 days. This can be configured:
32-
33-
```yaml
34-
SilverStripe\Control\Cookie:
35-
default_cookie_expiry_days: 90
36-
```
37-
38-
##### Deprecation: `Cookie::set()` `$expiry` in days
39-
40-
In previous versions of SilverStripe, expiry was expressed in days e.g `Cookie::set($name, $value, $expiry = 90)`. This has been deprecated, and will invoke a deprecation notice. If you are experience the deprecation notice, but are unable to control or resolve the cause, the notice can be suppressed using the configuration:
41-
42-
```yaml
43-
SilverStripe\Control\Cookie:
44-
suppress_expiry_as_timestamp_notice: true
45-
```
46-
**WARNING Notice should only be suppressed if no further resolution can be met.**
47-
48-
49-
###### Customising legacy expiry support
50-
51-
The legacy expiry behaviour can be extended.
52-
53-
```yaml
54-
SilverStripe\Control\CookieJar:
55-
extensions:
56-
- CookieJarExtension
57-
```
58-
59-
```php
60-
class CookieJarExtension extends DataExtension
61-
{
62-
63-
public function updateLegacyExpiry($expiry)
64-
{
65-
// Customisation goes here
66-
}
67-
}
68-
```
27+
SilverStripe's default `Cookie_Backend` supports `$expiry` as
28+
- Days: The number of days until the cookie expires
29+
- Unix timestamp: The time the cookie expires
30+
- 0: Expires at the end of the current session.
6931

7032
### get
7133

src/Control/Cookie.php

+2-16
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ class Cookie
2121
*/
2222
private static $report_errors = true;
2323

24-
/**
25-
* @var bool
26-
*/
27-
private static $suppress_expiry_as_timestamp_notice = false;
28-
2924
/**
3025
* @var int
3126
*/
@@ -44,7 +39,7 @@ public static function get_inst()
4439
/**
4540
* Set a cookie variable.
4641
*
47-
* Expiry time is a Unix timestamp; 0 expires at the end of the current session
42+
* Expiry time is set in days, and defaults to 90.
4843
*
4944
* @param string $name
5045
* @param mixed $value
@@ -59,21 +54,12 @@ public static function get_inst()
5954
public static function set(
6055
$name,
6156
$value,
62-
$expiry = 0,
57+
$expiry = 90,
6358
$path = null,
6459
$domain = null,
6560
$secure = false,
6661
$httpOnly = true
6762
) {
68-
if ($expiry > 0 && $expiry < DBDatetime::now()->getTimestamp()
69-
&& !Config::inst()->get(Cookie::class, 'suppress_expiry_as_timestamp_notice')
70-
) {
71-
Deprecation::notice(
72-
'5.0',
73-
'Cookie::set() requires $expiry to be a Unix timestamp of the time the cookie expires'
74-
);
75-
}
76-
7763
return self::get_inst()->set($name, $value, $expiry, $path, $domain, $secure, $httpOnly);
7864
}
7965

src/Control/CookieJar.php

+13-41
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
namespace SilverStripe\Control;
44

5-
use SilverStripe\Core\Config\Config;
5+
use LogicException;
66
use SilverStripe\Core\Extensible;
7-
use SilverStripe\Dev\Deprecation;
87
use SilverStripe\ORM\FieldType\DBDatetime;
9-
use LogicException;
108

119
/**
1210
* A default backend for the setting and getting of cookies
@@ -67,16 +65,23 @@ public function __construct($cookies = array())
6765
*
6866
* @param string $name The name of the cookie
6967
* @param string $value The value for the cookie to hold
70-
* @param int $expiry A Unix timestamp of the time the cookie expires; 0 expires at the end of the current session
68+
* @param int $expiry A Unix timestamp of, or the number of days until, the time the cookie expires;
69+
* 0 expires at the end of the current session
7170
* @param string $path The path to save the cookie on (falls back to site base)
7271
* @param string $domain The domain to make the cookie available on
7372
* @param boolean $secure Can the cookie only be sent over SSL?
7473
* @param boolean $httpOnly Prevent the cookie being accessible by JS
7574
*/
76-
public function set($name, $value, $expiry = -2, $path = null, $domain = null, $secure = false, $httpOnly = true)
75+
public function set($name, $value, $expiry = 90, $path = null, $domain = null, $secure = false, $httpOnly = true)
7776
{
78-
// Provide backwards compatibility support for 4.x
79-
$expiry = $this->legacyExpiry($expiry);
77+
// Treat null as 0
78+
$expiry = is_null($expiry) ? 0 : $expiry;
79+
80+
// Provide backwards compatibility for expiry in days (4.x)
81+
if ($expiry > 0 && $expiry < DBDatetime::now()->getTimestamp()) {
82+
// Convert days to supported expression
83+
$expiry = DBDatetime::now()->getTimestamp()+($expiry*86400);
84+
}
8085

8186
// Are we setting or clearing a cookie?
8287
$clear = $value === false || $value === '' || $expiry < 0;
@@ -86,11 +91,8 @@ public function set($name, $value, $expiry = -2, $path = null, $domain = null, $
8691
$expiry = -1;
8792
}
8893

89-
// Set the path up
90-
$path = $path ?: Director::baseURL();
91-
9294
// Send the cookie
93-
$this->outputCookie($name, $value, $expiry, $path, $domain, $secure, $httpOnly);
95+
$this->outputCookie($name, $value, $expiry, $path ?: Director::baseURL(), $domain, $secure, $httpOnly);
9496

9597
if ($clear) {
9698
// Clear cookie
@@ -103,36 +105,6 @@ public function set($name, $value, $expiry = -2, $path = null, $domain = null, $
103105
$this->new[$name] = $this->current[$name] = $value;
104106
}
105107

106-
/**
107-
* Support $expiry values that are supplied in days (SilverStripe 4.x)
108-
*
109-
* @param $expiry
110-
* @return float|int
111-
*/
112-
protected function legacyExpiry($expiry)
113-
{
114-
// Establish fallback
115-
$default = (int) Config::inst()->get(Cookie::class, 'default_cookie_expiry_days');
116-
117-
// Treat null as 0
118-
$expiry = (int) $expiry;
119-
120-
if ($expiry > 0 && $expiry < time()) {
121-
// Convert days to supported expression
122-
$expiry = DBDatetime::now()->getTimestamp()+($expiry*86400);
123-
}
124-
125-
if ($expiry <= -2) {
126-
// Supply default expiry
127-
$expiry = DBDatetime::now()->getTimestamp()+($default*86400);
128-
}
129-
130-
// Let's provide an extension hook to allow users to clarify legacy behavior as needed
131-
$this->extend('updateLegacyExpiry', $expiry);
132-
133-
return $expiry;
134-
}
135-
136108
/**
137109
* Get the cookie value by name
138110
*

src/Control/Cookie_Backend.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct($cookies = array());
3030
* @param boolean $secure Can the cookie only be sent over SSL?
3131
* @param boolean $httpOnly Prevent the cookie being accessible by JS
3232
*/
33-
public function set($name, $value, $expiry = 0, $path = null, $domain = null, $secure = false, $httpOnly = true);
33+
public function set($name, $value, $expiry = 90, $path = null, $domain = null, $secure = false, $httpOnly = true);
3434

3535
/**
3636
* Get the cookie value by name

src/Control/Session.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public function start(HTTPRequest $request)
332332
Cookie::set(
333333
session_name(),
334334
session_id(),
335-
DBDatetime::now()->getTimestamp()+$timeout,
335+
$timeout / 86400, // Convert seconds to days
336336
$path,
337337
$domain ?: null,
338338
$secure,

src/Security/MemberAuthenticator/CookieAuthenticationHandler.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,11 @@ public function logIn(Member $member, $persistent = false, HTTPRequest $request
217217
$rememberLoginHash = RememberLoginHash::generate($member);
218218
$tokenExpiryDays = RememberLoginHash::config()->uninherited('token_expiry_days');
219219
$deviceExpiryDays = RememberLoginHash::config()->uninherited('device_expiry_days');
220-
$tokenExpiry = DBDatetime::now()->getTimestamp()+($tokenExpiryDays*86400);
221-
$deviceExpiry = DBDatetime::now()->getTimestamp()+($deviceExpiryDays*86400);
222220
$secure = $this->getTokenCookieSecure();
223221
Cookie::set(
224222
$this->getTokenCookieName(),
225223
$member->ID . ':' . $rememberLoginHash->getToken(),
226-
$tokenExpiry,
224+
$tokenExpiryDays,
227225
null,
228226
null,
229227
$secure,
@@ -232,7 +230,7 @@ public function logIn(Member $member, $persistent = false, HTTPRequest $request
232230
Cookie::set(
233231
$this->getDeviceCookieName(),
234232
$rememberLoginHash->DeviceID,
235-
$deviceExpiry,
233+
$deviceExpiryDays,
236234
null,
237235
null,
238236
$secure,
@@ -268,8 +266,8 @@ public function logOut(HTTPRequest $request = null)
268266
protected function clearCookies()
269267
{
270268
$secure = $this->getTokenCookieSecure();
271-
Cookie::set($this->getTokenCookieName(), null, null, null, null, $secure);
272-
Cookie::set($this->getDeviceCookieName(), null, null, null, null, $secure);
269+
Cookie::set($this->getTokenCookieName(), null, 0, null, null, $secure);
270+
Cookie::set($this->getDeviceCookieName(), null, 0, null, null, $secure);
273271
Cookie::force_expiry($this->getTokenCookieName(), null, null, null, null, $secure);
274272
Cookie::force_expiry($this->getDeviceCookieName(), null, null, null, null, $secure);
275273
}

0 commit comments

Comments
 (0)