|
3 | 3 | namespace Illuminate\Cookie\Middleware; |
4 | 4 |
|
5 | 5 | use Closure; |
| 6 | +use Illuminate\Support\Facades\Session; |
| 7 | +use Illuminate\Cookie\CookieValuePrefix; |
6 | 8 | use Symfony\Component\HttpFoundation\Cookie; |
7 | 9 | use Symfony\Component\HttpFoundation\Request; |
8 | 10 | use Symfony\Component\HttpFoundation\Response; |
@@ -74,13 +76,21 @@ public function handle($request, Closure $next) |
74 | 76 | */ |
75 | 77 | protected function decrypt(Request $request) |
76 | 78 | { |
77 | | - foreach ($request->cookies as $key => $c) { |
| 79 | + foreach ($request->cookies as $key => $cookie) { |
78 | 80 | if ($this->isDisabled($key)) { |
79 | 81 | continue; |
80 | 82 | } |
81 | 83 |
|
82 | 84 | try { |
83 | | - $request->cookies->set($key, $this->decryptCookie($key, $c)); |
| 85 | + $decryptedValue = $this->decryptCookie($key, $cookie); |
| 86 | + |
| 87 | + $value = CookieValuePrefix::getVerifiedValue($key, $decryptedValue, $this->encrypter->getKey()); |
| 88 | + |
| 89 | + if (empty($value) && $key === config('session.cookie') && Session::isValidId($decryptedValue)) { |
| 90 | + $value = $decryptedValue; |
| 91 | + } |
| 92 | + |
| 93 | + $request->cookies->set($key, $value); |
84 | 94 | } catch (DecryptException $e) { |
85 | 95 | $request->cookies->set($key, null); |
86 | 96 | } |
@@ -135,8 +145,14 @@ protected function encrypt(Response $response) |
135 | 145 | continue; |
136 | 146 | } |
137 | 147 |
|
| 148 | + $prefix = ''; |
| 149 | + |
| 150 | + if ($cookie->getName() !== 'XSRF-TOKEN') { |
| 151 | + $prefix = CookieValuePrefix::create($cookie->getName(), $this->encrypter->getKey()); |
| 152 | + } |
| 153 | + |
138 | 154 | $response->headers->setCookie($this->duplicate( |
139 | | - $cookie, $this->encrypter->encrypt($cookie->getValue(), static::serialized($cookie->getName())) |
| 155 | + $cookie, $this->encrypter->encrypt($prefix.$cookie->getValue(), static::serialized($cookie->getName())) |
140 | 156 | )); |
141 | 157 | } |
142 | 158 |
|
|
0 commit comments