Skip to content

Commit 240d904

Browse files
committed
adjust cookie serialization
1 parent d1fc8eb commit 240d904

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/Illuminate/Cookie/Middleware/EncryptCookies.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class EncryptCookies
3030
*
3131
* @var bool
3232
*/
33-
protected $serialize = false;
33+
protected static $serialize = false;
3434

3535
/**
3636
* Create a new CookieGuard instance.
@@ -100,7 +100,7 @@ protected function decryptCookie($name, $cookie)
100100
{
101101
return is_array($cookie)
102102
? $this->decryptArray($cookie)
103-
: $this->encrypter->decrypt($cookie, $this->serialize);
103+
: $this->encrypter->decrypt($cookie, static::serialized($name));
104104
}
105105

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

116116
foreach ($cookie as $key => $value) {
117117
if (is_string($value)) {
118-
$decrypted[$key] = $this->encrypter->decrypt($value, $this->serialize);
118+
$decrypted[$key] = $this->encrypter->decrypt($value, static::serialized($key));
119119
}
120120
}
121121

@@ -136,7 +136,7 @@ protected function encrypt(Response $response)
136136
}
137137

138138
$response->headers->setCookie($this->duplicate(
139-
$cookie, $this->encrypter->encrypt($cookie->getValue(), $this->serialize)
139+
$cookie, $this->encrypter->encrypt($cookie->getValue(), static::serialized($cookie->getName()))
140140
));
141141
}
142142

@@ -169,4 +169,15 @@ public function isDisabled($name)
169169
{
170170
return in_array($name, $this->except);
171171
}
172+
173+
/**
174+
* Determine if the cookie contents should be serialized.
175+
*
176+
* @param string $name
177+
* @return bool
178+
*/
179+
public static function serialized($name)
180+
{
181+
return static::$serialize;
182+
}
172183
}

src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\HttpFoundation\Cookie;
99
use Illuminate\Contracts\Encryption\Encrypter;
1010
use Illuminate\Session\TokenMismatchException;
11+
use Illuminate\Cookie\Middleware\EncryptCookies;
1112

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

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

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

165166
return $response;
166167
}
168+
169+
/**
170+
* Determine if the cookie contents should be serialized.
171+
*
172+
* @return bool
173+
*/
174+
public static function serialized()
175+
{
176+
return EncryptCookies::serialized('XSRF-TOKEN');
177+
}
167178
}

0 commit comments

Comments
 (0)