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

@@ -11,6 +11,8 @@
namespace Symfony\Component\HttpKernel;
use Symfony\Component\HttpFoundation\Request;
/**
* Signs URIs.
*
@@ -18,16 +20,14 @@ namespace Symfony\Component\HttpKernel;
*/
class UriSigner
{
private $secret;
private $parameter;
private string $secret;
private string $parameter;
/**
* Constructor.
*
* @param string $secret A secret
* @param string $parameter Query string parameter to use
*/
public function __construct($secret, $parameter = '_hash')
public function __construct(string $secret, string $parameter = '_hash')
{
$this->secret = $secret;
$this->parameter = $parameter;
@@ -38,72 +38,69 @@ class UriSigner
*
* The given URI is signed by adding the query string parameter
* which value depends on the URI and the secret.
*
* @param string $uri A URI to sign
*
* @return string The signed URI
*/
public function sign($uri)
public function sign(string $uri): string
{
$url = parse_url($uri);
if (isset($url['query'])) {
parse_str($url['query'], $params);
} else {
$params = array();
$params = [];
}
$uri = $this->buildUrl($url, $params);
$params[$this->parameter] = $this->computeHash($uri);
return $uri.(false === strpos($uri, '?') ? '?' : '&').$this->parameter.'='.$this->computeHash($uri);
return $this->buildUrl($url, $params);
}
/**
* Checks that a URI contains the correct hash.
*
* The query string parameter must be the last one
* (as it is generated that way by the sign() method, it should
* never be a problem).
*
* @param string $uri A signed URI
*
* @return bool True if the URI is signed correctly, false otherwise
*/
public function check($uri)
public function check(string $uri): bool
{
$url = parse_url($uri);
if (isset($url['query'])) {
parse_str($url['query'], $params);
} else {
$params = array();
$params = [];
}
if (empty($params[$this->parameter])) {
return false;
}
$hash = urlencode($params[$this->parameter]);
$hash = $params[$this->parameter];
unset($params[$this->parameter]);
return $this->computeHash($this->buildUrl($url, $params)) === $hash;
return hash_equals($this->computeHash($this->buildUrl($url, $params)), $hash);
}
private function computeHash($uri)
public function checkRequest(Request $request): bool
{
return urlencode(base64_encode(hash_hmac('sha256', $uri, $this->secret, true)));
$qs = ($qs = $request->server->get('QUERY_STRING')) ? '?'.$qs : '';
// we cannot use $request->getUri() here as we want to work with the original URI (no query string reordering)
return $this->check($request->getSchemeAndHttpHost().$request->getBaseUrl().$request->getPathInfo().$qs);
}
private function buildUrl(array $url, array $params = array())
private function computeHash(string $uri): string
{
ksort($params, SORT_STRING);
return base64_encode(hash_hmac('sha256', $uri, $this->secret, true));
}
private function buildUrl(array $url, array $params = []): string
{
ksort($params, \SORT_STRING);
$url['query'] = http_build_query($params, '', '&');
$scheme = isset($url['scheme']) ? $url['scheme'].'://' : '';
$host = isset($url['host']) ? $url['host'] : '';
$host = $url['host'] ?? '';
$port = isset($url['port']) ? ':'.$url['port'] : '';
$user = isset($url['user']) ? $url['user'] : '';
$user = $url['user'] ?? '';
$pass = isset($url['pass']) ? ':'.$url['pass'] : '';
$pass = ($user || $pass) ? "$pass@" : '';
$path = isset($url['path']) ? $url['path'] : '';
$path = $url['path'] ?? '';
$query = isset($url['query']) && $url['query'] ? '?'.$url['query'] : '';
$fragment = isset($url['fragment']) ? '#'.$url['fragment'] : '';