Skip to content

Commit

Permalink
dont serialize csrf cookie / header (#25121)
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell authored Aug 7, 2018
1 parent 9ed650c commit c0eb907
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
22 changes: 17 additions & 5 deletions src/Illuminate/Cookie/Middleware/EncryptCookies.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ class EncryptCookies
*/
protected $except = [];

/**
* The cookies that should not be serialized.
*
* @var array
*/
protected $serialization = [
'XSRF-TOKEN' => false,
];

/**
* Create a new CookieGuard instance.
*
Expand Down Expand Up @@ -73,7 +82,7 @@ protected function decrypt(Request $request)
}

try {
$request->cookies->set($key, $this->decryptCookie($cookie));
$request->cookies->set($key, $this->decryptCookie($key, $cookie));
} catch (DecryptException $e) {
$request->cookies->set($key, null);
}
Expand All @@ -85,14 +94,15 @@ protected function decrypt(Request $request)
/**
* Decrypt the given cookie and return the value.
*
* @param string $name
* @param string|array $cookie
* @return string|array
*/
protected function decryptCookie($cookie)
protected function decryptCookie($name, $cookie)
{
return is_array($cookie)
? $this->decryptArray($cookie)
: $this->encrypter->decrypt($cookie);
: $this->encrypter->decrypt($cookie, $this->serialization[$name] ?? true);
}

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

foreach ($cookie as $key => $value) {
if (is_string($value)) {
$decrypted[$key] = $this->encrypter->decrypt($value);
$decrypted[$key] = $this->encrypter->decrypt($value, $this->serialization[$key] ?? true);
}
}

Expand All @@ -127,8 +137,10 @@ protected function encrypt(Response $response)
continue;
}

$serialize = $this->serialization[$cookie->getName()] ?? true;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,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);
$token = $this->encrypter->decrypt($header, false);
}

return $token;
Expand Down

0 comments on commit c0eb907

Please sign in to comment.