Skip to content

Commit 2c759f2

Browse files
authored
[5.5] Port Cookie prefixing (#33891)
* Port Cookie prefixing * Apply fixes from StyleCI (#33890)
1 parent 40579ba commit 2c759f2

2 files changed

Lines changed: 69 additions & 3 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Illuminate\Cookie;
4+
5+
use Illuminate\Support\Str;
6+
7+
class CookieValuePrefix
8+
{
9+
/**
10+
* Create a new cookie value prefix for the given cookie name.
11+
*
12+
* @param string $cookieName
13+
* @param string $key
14+
* @return string
15+
*/
16+
public static function create($cookieName, $key)
17+
{
18+
return hash_hmac('sha1', $cookieName.'v2', $key).'|';
19+
}
20+
21+
/**
22+
* Remove the cookie value prefix.
23+
*
24+
* @param string $cookieValue
25+
* @return string
26+
*/
27+
public static function remove($cookieValue)
28+
{
29+
return substr($cookieValue, 41);
30+
}
31+
32+
/**
33+
* Verify the provided cookie's value.
34+
*
35+
* @param string $name
36+
* @param string $value
37+
* @param string $key
38+
* @return string|null
39+
*/
40+
public static function getVerifiedValue($name, $value, $key)
41+
{
42+
$verifiedValue = null;
43+
44+
if (Str::startsWith($value, static::create($name, $key))) {
45+
$verifiedValue = static::remove($value);
46+
}
47+
48+
return $verifiedValue;
49+
}
50+
}

src/Illuminate/Cookie/Middleware/EncryptCookies.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Illuminate\Cookie\Middleware;
44

55
use Closure;
6+
use Illuminate\Support\Facades\Session;
7+
use Illuminate\Cookie\CookieValuePrefix;
68
use Symfony\Component\HttpFoundation\Cookie;
79
use Symfony\Component\HttpFoundation\Request;
810
use Symfony\Component\HttpFoundation\Response;
@@ -74,13 +76,21 @@ public function handle($request, Closure $next)
7476
*/
7577
protected function decrypt(Request $request)
7678
{
77-
foreach ($request->cookies as $key => $c) {
79+
foreach ($request->cookies as $key => $cookie) {
7880
if ($this->isDisabled($key)) {
7981
continue;
8082
}
8183

8284
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);
8494
} catch (DecryptException $e) {
8595
$request->cookies->set($key, null);
8696
}
@@ -135,8 +145,14 @@ protected function encrypt(Response $response)
135145
continue;
136146
}
137147

148+
$prefix = '';
149+
150+
if ($cookie->getName() !== 'XSRF-TOKEN') {
151+
$prefix = CookieValuePrefix::create($cookie->getName(), $this->encrypter->getKey());
152+
}
153+
138154
$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()))
140156
));
141157
}
142158

0 commit comments

Comments
 (0)