Skip to content

Commit

Permalink
Fixed Session::setFlashCookieObject() to use the same options as th…
Browse files Browse the repository at this point in the history
…e main session cookie
  • Loading branch information
mahagr committed Sep 14, 2021
1 parent 3bd9e44 commit c51fb17
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@
* Fixed validation of `number` type [#3433](https://github.com/getgrav/grav/issues/3433)
* Fixed excessive `security.yaml` file creation [#3432](https://github.com/getgrav/grav/issues/3432)
* Fixed incorrect port :0 with nginx unix socket setup [#3439](https://github.com/getgrav/grav/issues/3439)
* Fixed `Session::setFlashCookieObject()` to use the same options as the main session cookie

# v1.7.20
## 09/01/2021
Expand Down
12 changes: 8 additions & 4 deletions system/src/Grav/Common/Session.php
Expand Up @@ -12,6 +12,7 @@
use Grav\Common\Form\FormFlash;
use Grav\Events\SessionStartEvent;
use Grav\Plugin\Form\Forms;
use JsonException;
use function is_string;

/**
Expand Down Expand Up @@ -148,10 +149,11 @@ public function getFlashObject($name)
* @param mixed $object
* @param int $time
* @return $this
* @throws JsonException
*/
public function setFlashCookieObject($name, $object, $time = 60)
{
setcookie($name, json_encode($object), time() + $time, '/');
setcookie($name, json_encode($object, JSON_THROW_ON_ERROR), $this->getCookieOptions($time));

return $this;
}
Expand All @@ -161,13 +163,15 @@ public function setFlashCookieObject($name, $object, $time = 60)
*
* @param string $name
* @return mixed|null
* @throws JsonException
*/
public function getFlashCookieObject($name)
{
if (isset($_COOKIE[$name])) {
$object = json_decode($_COOKIE[$name], false);
setcookie($name, '', time() - 3600, '/');
return $object;
$cookie = $_COOKIE[$name];
setcookie($name, '', $this->getCookieOptions(-42000));

return json_decode($cookie, false, 512, JSON_THROW_ON_ERROR);
}

return null;
Expand Down
34 changes: 16 additions & 18 deletions system/src/Grav/Framework/Session/Session.php
Expand Up @@ -338,23 +338,12 @@ public function invalidate()
{
$name = $this->getName();
if (null !== $name) {
$params = session_get_cookie_params();

$cookie_options = array (
'expires' => time() - 42000,
'path' => $params['path'],
'domain' => $params['domain'],
'secure' => $params['secure'],
'httponly' => $params['httponly'],
'samesite' => $params['samesite']
);

$this->removeCookie();

setcookie(
session_name(),
'',
$cookie_options
$this->getCookieOptions(-42000)
);
}

Expand Down Expand Up @@ -463,27 +452,36 @@ protected function onSessionStart(): void
}

/**
* @return void
* Store something in cookie temporarily.
*
* @param int|null $lifetime
* @return array
*/
protected function setCookie(): void
public function getCookieOptions(int $lifetime = null): array
{
$params = session_get_cookie_params();

$cookie_options = array (
'expires' => time() + $params['lifetime'],
return [
'expires' => time() + ($lifetime ?? $params['lifetime']),
'path' => $params['path'],
'domain' => $params['domain'],
'secure' => $params['secure'],
'httponly' => $params['httponly'],
'samesite' => $params['samesite']
);
];
}

/**
* @return void
*/
protected function setCookie(): void
{
$this->removeCookie();

setcookie(
session_name(),
session_id(),
$cookie_options
$this->getCookieOptions()
);
}

Expand Down

0 comments on commit c51fb17

Please sign in to comment.