Skip to content

Commit

Permalink
adjust cookie serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Aug 7, 2018
1 parent d1fc8eb commit 240d904
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
19 changes: 15 additions & 4 deletions src/Illuminate/Cookie/Middleware/EncryptCookies.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class EncryptCookies
*
* @var bool
*/
protected $serialize = false;
protected static $serialize = false;

/**
* Create a new CookieGuard instance.
Expand Down Expand Up @@ -100,7 +100,7 @@ protected function decryptCookie($name, $cookie)
{
return is_array($cookie)
? $this->decryptArray($cookie)
: $this->encrypter->decrypt($cookie, $this->serialize);
: $this->encrypter->decrypt($cookie, static::serialized($name));
}

/**
Expand All @@ -115,7 +115,7 @@ protected function decryptArray(array $cookie)

foreach ($cookie as $key => $value) {
if (is_string($value)) {
$decrypted[$key] = $this->encrypter->decrypt($value, $this->serialize);
$decrypted[$key] = $this->encrypter->decrypt($value, static::serialized($key));
}
}

Expand All @@ -136,7 +136,7 @@ protected function encrypt(Response $response)
}

$response->headers->setCookie($this->duplicate(
$cookie, $this->encrypter->encrypt($cookie->getValue(), $this->serialize)
$cookie, $this->encrypter->encrypt($cookie->getValue(), static::serialized($cookie->getName()))
));
}

Expand Down Expand Up @@ -169,4 +169,15 @@ public function isDisabled($name)
{
return in_array($name, $this->except);
}

/**
* Determine if the cookie contents should be serialized.
*
* @param string $name
* @return bool
*/
public static function serialized($name)
{
return static::$serialize;
}
}
13 changes: 12 additions & 1 deletion src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\HttpFoundation\Cookie;
use Illuminate\Contracts\Encryption\Encrypter;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Cookie\Middleware\EncryptCookies;

class VerifyCsrfToken
{
Expand Down Expand Up @@ -138,7 +139,7 @@ protected function getTokenFromRequest($request)
$token = $request->input('_token') ?: $request->header('X-CSRF-TOKEN');

if (! $token && $header = $request->header('X-XSRF-TOKEN')) {
$token = $this->encrypter->decrypt($header, false);
$token = $this->encrypter->decrypt($header, static::serialized());
}

return $token;
Expand All @@ -164,4 +165,14 @@ protected function addCookieToResponse($request, $response)

return $response;
}

/**
* Determine if the cookie contents should be serialized.
*
* @return bool
*/
public static function serialized()
{
return EncryptCookies::serialized('XSRF-TOKEN');
}
}

0 comments on commit 240d904

Please sign in to comment.