Upgrade framework

This commit is contained in:
2023-11-14 16:54:35 +01:00
parent 1648a5cd42
commit 4fcf6fffcc
10548 changed files with 693138 additions and 466698 deletions

View File

@@ -2,13 +2,16 @@
namespace Illuminate\Cookie;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use Symfony\Component\HttpFoundation\Cookie;
use Illuminate\Contracts\Cookie\QueueingFactory as JarContract;
use Illuminate\Support\Arr;
use Illuminate\Support\InteractsWithTime;
use Illuminate\Support\Traits\Macroable;
use Symfony\Component\HttpFoundation\Cookie;
class CookieJar implements JarContract
{
use InteractsWithTime, Macroable;
/**
* The default path (if specified).
*
@@ -24,16 +27,23 @@ class CookieJar implements JarContract
protected $domain;
/**
* The default secure setting (defaults to false).
* The default secure setting (defaults to null).
*
* @var bool
* @var bool|null
*/
protected $secure = false;
protected $secure;
/**
* The default SameSite option (defaults to lax).
*
* @var string
*/
protected $sameSite = 'lax';
/**
* All of the cookies queued for sending.
*
* @var array
* @var \Symfony\Component\HttpFoundation\Cookie[]
*/
protected $queued = [];
@@ -42,44 +52,48 @@ class CookieJar implements JarContract
*
* @param string $name
* @param string $value
* @param int $minutes
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool $httpOnly
* @param int $minutes
* @param string|null $path
* @param string|null $domain
* @param bool|null $secure
* @param bool $httpOnly
* @param bool $raw
* @param string|null $sameSite
* @return \Symfony\Component\HttpFoundation\Cookie
*/
public function make($name, $value, $minutes = 0, $path = null, $domain = null, $secure = false, $httpOnly = true)
public function make($name, $value, $minutes = 0, $path = null, $domain = null, $secure = null, $httpOnly = true, $raw = false, $sameSite = null)
{
list($path, $domain, $secure) = $this->getPathAndDomain($path, $domain, $secure);
[$path, $domain, $secure, $sameSite] = $this->getPathAndDomain($path, $domain, $secure, $sameSite);
$time = ($minutes == 0) ? 0 : Carbon::now()->getTimestamp() + ($minutes * 60);
$time = ($minutes == 0) ? 0 : $this->availableAt($minutes * 60);
return new Cookie($name, $value, $time, $path, $domain, $secure, $httpOnly);
return new Cookie($name, $value, $time, $path, $domain, $secure, $httpOnly, $raw, $sameSite);
}
/**
* Create a cookie that lasts "forever" (five years).
* Create a cookie that lasts "forever" (400 days).
*
* @param string $name
* @param string $value
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool $httpOnly
* @param string|null $path
* @param string|null $domain
* @param bool|null $secure
* @param bool $httpOnly
* @param bool $raw
* @param string|null $sameSite
* @return \Symfony\Component\HttpFoundation\Cookie
*/
public function forever($name, $value, $path = null, $domain = null, $secure = false, $httpOnly = true)
public function forever($name, $value, $path = null, $domain = null, $secure = null, $httpOnly = true, $raw = false, $sameSite = null)
{
return $this->make($name, $value, 2628000, $path, $domain, $secure, $httpOnly);
return $this->make($name, $value, 576000, $path, $domain, $secure, $httpOnly, $raw, $sameSite);
}
/**
* Expire the given cookie.
*
* @param string $name
* @param string $path
* @param string $domain
* @param string|null $path
* @param string|null $domain
* @return \Symfony\Component\HttpFoundation\Cookie
*/
public function forget($name, $path = null, $domain = null)
@@ -91,23 +105,31 @@ class CookieJar implements JarContract
* Determine if a cookie has been queued.
*
* @param string $key
* @param string|null $path
* @return bool
*/
public function hasQueued($key)
public function hasQueued($key, $path = null)
{
return ! is_null($this->queued($key));
return ! is_null($this->queued($key, null, $path));
}
/**
* Get a queued cookie instance.
*
* @param string $key
* @param mixed $default
* @return \Symfony\Component\HttpFoundation\Cookie
* @param mixed $default
* @param string|null $path
* @return \Symfony\Component\HttpFoundation\Cookie|null
*/
public function queued($key, $default = null)
public function queued($key, $default = null, $path = null)
{
return Arr::get($this->queued, $key, $default);
$queued = Arr::get($this->queued, $key, $default);
if ($path === null) {
return Arr::last($queued, null, $default);
}
return Arr::get($queued, $path, $default);
}
/**
@@ -118,24 +140,52 @@ class CookieJar implements JarContract
*/
public function queue(...$parameters)
{
if (head($parameters) instanceof Cookie) {
$cookie = head($parameters);
if (isset($parameters[0]) && $parameters[0] instanceof Cookie) {
$cookie = $parameters[0];
} else {
$cookie = call_user_func_array([$this, 'make'], $parameters);
$cookie = $this->make(...array_values($parameters));
}
$this->queued[$cookie->getName()] = $cookie;
if (! isset($this->queued[$cookie->getName()])) {
$this->queued[$cookie->getName()] = [];
}
$this->queued[$cookie->getName()][$cookie->getPath()] = $cookie;
}
/**
* Queue a cookie to expire with the next response.
*
* @param string $name
* @param string|null $path
* @param string|null $domain
* @return void
*/
public function expire($name, $path = null, $domain = null)
{
$this->queue($this->forget($name, $path, $domain));
}
/**
* Remove a cookie from the queue.
*
* @param string $name
* @param string|null $path
* @return void
*/
public function unqueue($name)
public function unqueue($name, $path = null)
{
unset($this->queued[$name]);
if ($path === null) {
unset($this->queued[$name]);
return;
}
unset($this->queued[$name][$path]);
if (empty($this->queued[$name])) {
unset($this->queued[$name]);
}
}
/**
@@ -143,12 +193,13 @@ class CookieJar implements JarContract
*
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool|null $secure
* @param string|null $sameSite
* @return array
*/
protected function getPathAndDomain($path, $domain, $secure = false)
protected function getPathAndDomain($path, $domain, $secure = null, $sameSite = null)
{
return [$path ?: $this->path, $domain ?: $this->domain, $secure ?: $this->secure];
return [$path ?: $this->path, $domain ?: $this->domain, is_bool($secure) ? $secure : $this->secure, $sameSite ?: $this->sameSite];
}
/**
@@ -156,12 +207,13 @@ class CookieJar implements JarContract
*
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool $secure
* @param string|null $sameSite
* @return $this
*/
public function setDefaultPathAndDomain($path, $domain, $secure = false)
public function setDefaultPathAndDomain($path, $domain, $secure = false, $sameSite = null)
{
list($this->path, $this->domain, $this->secure) = [$path, $domain, $secure];
[$this->path, $this->domain, $this->secure, $this->sameSite] = [$path, $domain, $secure, $sameSite];
return $this;
}
@@ -169,10 +221,22 @@ class CookieJar implements JarContract
/**
* Get the cookies which have been queued for the next request.
*
* @return array
* @return \Symfony\Component\HttpFoundation\Cookie[]
*/
public function getQueuedCookies()
{
return $this->queued;
return Arr::flatten($this->queued);
}
/**
* Flush the cookies which have been queued for the next request.
*
* @return $this
*/
public function flushQueuedCookies()
{
$this->queued = [];
return $this;
}
}

View File

@@ -16,7 +16,9 @@ class CookieServiceProvider extends ServiceProvider
$this->app->singleton('cookie', function ($app) {
$config = $app->make('config')->get('session');
return (new CookieJar)->setDefaultPathAndDomain($config['path'], $config['domain'], $config['secure']);
return (new CookieJar)->setDefaultPathAndDomain(
$config['path'], $config['domain'], $config['secure'], $config['same_site'] ?? null
);
});
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace Illuminate\Cookie;
class CookieValuePrefix
{
/**
* Create a new cookie value prefix for the given cookie name.
*
* @param string $cookieName
* @param string $key
* @return string
*/
public static function create($cookieName, $key)
{
return hash_hmac('sha1', $cookieName.'v2', $key).'|';
}
/**
* Remove the cookie value prefix.
*
* @param string $cookieValue
* @return string
*/
public static function remove($cookieValue)
{
return substr($cookieValue, 41);
}
/**
* Validate a cookie value contains a valid prefix. If it does, return the cookie value with the prefix removed. Otherwise, return null.
*
* @param string $cookieName
* @param string $cookieValue
* @param string $key
* @return string|null
*/
public static function validate($cookieName, $cookieValue, $key)
{
$hasValidPrefix = str_starts_with($cookieValue, static::create($cookieName, $key));
return $hasValidPrefix ? static::remove($cookieValue) : null;
}
}

View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) Taylor Otwell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -3,11 +3,12 @@
namespace Illuminate\Cookie\Middleware;
use Closure;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract;
use Illuminate\Cookie\CookieValuePrefix;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract;
class EncryptCookies
{
@@ -25,6 +26,13 @@ class EncryptCookies
*/
protected $except = [];
/**
* Indicates if cookies should be serialized.
*
* @var bool
*/
protected static $serialize = false;
/**
* Create a new CookieGuard instance.
*
@@ -39,12 +47,12 @@ class EncryptCookies
/**
* Disable encryption for the given cookie name(s).
*
* @param string|array $cookieName
* @param string|array $name
* @return void
*/
public function disableFor($cookieName)
public function disableFor($name)
{
$this->except = array_merge($this->except, (array) $cookieName);
$this->except = array_merge($this->except, (array) $name);
}
/**
@@ -52,7 +60,7 @@ class EncryptCookies
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
* @return \Symfony\Component\HttpFoundation\Response
*/
public function handle($request, Closure $next)
{
@@ -67,13 +75,15 @@ class EncryptCookies
*/
protected function decrypt(Request $request)
{
foreach ($request->cookies as $key => $c) {
foreach ($request->cookies as $key => $cookie) {
if ($this->isDisabled($key)) {
continue;
}
try {
$request->cookies->set($key, $this->decryptCookie($c));
$value = $this->decryptCookie($key, $cookie);
$request->cookies->set($key, $this->validateValue($key, $value));
} catch (DecryptException $e) {
$request->cookies->set($key, null);
}
@@ -82,17 +92,50 @@ class EncryptCookies
return $request;
}
/**
* Validate and remove the cookie value prefix from the value.
*
* @param string $key
* @param string $value
* @return string|array|null
*/
protected function validateValue(string $key, $value)
{
return is_array($value)
? $this->validateArray($key, $value)
: CookieValuePrefix::validate($key, $value, $this->encrypter->getKey());
}
/**
* Validate and remove the cookie value prefix from all values of an array.
*
* @param string $key
* @param array $value
* @return array
*/
protected function validateArray(string $key, array $value)
{
$validated = [];
foreach ($value as $index => $subValue) {
$validated[$index] = $this->validateValue("{$key}[{$index}]", $subValue);
}
return $validated;
}
/**
* 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, static::serialized($name));
}
/**
@@ -107,7 +150,11 @@ class EncryptCookies
foreach ($cookie as $key => $value) {
if (is_string($value)) {
$decrypted[$key] = $this->encrypter->decrypt($value);
$decrypted[$key] = $this->encrypter->decrypt($value, static::serialized($key));
}
if (is_array($value)) {
$decrypted[$key] = $this->decryptArray($value);
}
}
@@ -128,7 +175,11 @@ class EncryptCookies
}
$response->headers->setCookie($this->duplicate(
$cookie, $this->encrypter->encrypt($cookie->getValue())
$cookie,
$this->encrypter->encrypt(
CookieValuePrefix::create($cookie->getName(), $this->encrypter->getKey()).$cookie->getValue(),
static::serialized($cookie->getName())
)
));
}
@@ -138,26 +189,38 @@ class EncryptCookies
/**
* Duplicate a cookie with a new value.
*
* @param \Symfony\Component\HttpFoundation\Cookie $c
* @param \Symfony\Component\HttpFoundation\Cookie $cookie
* @param mixed $value
* @return \Symfony\Component\HttpFoundation\Cookie
*/
protected function duplicate(Cookie $c, $value)
protected function duplicate(Cookie $cookie, $value)
{
return new Cookie(
$c->getName(), $value, $c->getExpiresTime(), $c->getPath(),
$c->getDomain(), $c->isSecure(), $c->isHttpOnly()
$cookie->getName(), $value, $cookie->getExpiresTime(),
$cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(),
$cookie->isHttpOnly(), $cookie->isRaw(), $cookie->getSameSite()
);
}
/**
* Determine whether encryption has been disabled for the given cookie.
*
* @param string $name
* @param string $name
* @return bool
*/
public function isDisabled($name)
{
return in_array($name, $this->except);
}
/**
* Determine if the cookie contents should be serialized.
*
* @param string $name
* @return bool
*/
public static function serialized($name)
{
return static::$serialize;
}
}

View File

@@ -14,11 +14,13 @@
}
],
"require": {
"php": ">=5.6.4",
"illuminate/contracts": "5.4.*",
"illuminate/support": "5.4.*",
"symfony/http-foundation": "~3.2",
"symfony/http-kernel": "~3.2"
"php": "^8.0.2",
"illuminate/collections": "^9.0",
"illuminate/contracts": "^9.0",
"illuminate/macroable": "^9.0",
"illuminate/support": "^9.0",
"symfony/http-foundation": "^6.0",
"symfony/http-kernel": "^6.0"
},
"autoload": {
"psr-4": {
@@ -27,7 +29,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "5.4-dev"
"dev-master": "9.x-dev"
}
},
"config": {