Skip to content

Commit c0eb907

Browse files
authored
dont serialize csrf cookie / header (#25121)
1 parent 9ed650c commit c0eb907

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/Illuminate/Cookie/Middleware/EncryptCookies.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ class EncryptCookies
2525
*/
2626
protected $except = [];
2727

28+
/**
29+
* The cookies that should not be serialized.
30+
*
31+
* @var array
32+
*/
33+
protected $serialization = [
34+
'XSRF-TOKEN' => false,
35+
];
36+
2837
/**
2938
* Create a new CookieGuard instance.
3039
*
@@ -73,7 +82,7 @@ protected function decrypt(Request $request)
7382
}
7483

7584
try {
76-
$request->cookies->set($key, $this->decryptCookie($cookie));
85+
$request->cookies->set($key, $this->decryptCookie($key, $cookie));
7786
} catch (DecryptException $e) {
7887
$request->cookies->set($key, null);
7988
}
@@ -85,14 +94,15 @@ protected function decrypt(Request $request)
8594
/**
8695
* Decrypt the given cookie and return the value.
8796
*
97+
* @param string $name
8898
* @param string|array $cookie
8999
* @return string|array
90100
*/
91-
protected function decryptCookie($cookie)
101+
protected function decryptCookie($name, $cookie)
92102
{
93103
return is_array($cookie)
94104
? $this->decryptArray($cookie)
95-
: $this->encrypter->decrypt($cookie);
105+
: $this->encrypter->decrypt($cookie, $this->serialization[$name] ?? true);
96106
}
97107

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

108118
foreach ($cookie as $key => $value) {
109119
if (is_string($value)) {
110-
$decrypted[$key] = $this->encrypter->decrypt($value);
120+
$decrypted[$key] = $this->encrypter->decrypt($value, $this->serialization[$key] ?? true);
111121
}
112122
}
113123

@@ -127,8 +137,10 @@ protected function encrypt(Response $response)
127137
continue;
128138
}
129139

140+
$serialize = $this->serialization[$cookie->getName()] ?? true;
141+
130142
$response->headers->setCookie($this->duplicate(
131-
$cookie, $this->encrypter->encrypt($cookie->getValue())
143+
$cookie, $this->encrypter->encrypt($cookie->getValue(), $serialize)
132144
));
133145
}
134146

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ protected function getTokenFromRequest($request)
138138
$token = $request->input('_token') ?: $request->header('X-CSRF-TOKEN');
139139

140140
if (! $token && $header = $request->header('X-XSRF-TOKEN')) {
141-
$token = $this->encrypter->decrypt($header);
141+
$token = $this->encrypter->decrypt($header, false);
142142
}
143143

144144
return $token;

0 commit comments

Comments
 (0)