Attribute
2 years ago
Bundle
1 year ago
CacheClearer
2 years ago
CacheWarmer
1 year ago
Config
1 year ago
Controller
1 year ago
ControllerMetadata
1 year ago
DataCollector
1 year ago
Debug
1 year ago
DependencyInjection
1 year ago
Event
1 year ago
EventListener
1 year ago
Exception
1 year ago
Fragment
1 year ago
HttpCache
1 year ago
Log
1 year ago
Profiler
1 year ago
Resources
2 years ago
HttpClientKernel.php
1 year ago
HttpKernel.php
1 year ago
HttpKernelBrowser.php
1 year ago
HttpKernelInterface.php
1 year ago
Kernel.php
1 month ago
KernelEvents.php
2 years ago
KernelInterface.php
2 years ago
LICENSE
2 years ago
README.md
2 years ago
RebootableInterface.php
2 years ago
TerminableInterface.php
2 years ago
UriSigner.php
1 year ago
UriSigner.php
98 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace Matomo\Dependencies\Symfony\Component\HttpKernel; |
| 12 | |
| 13 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\Request; |
| 14 | /** |
| 15 | * Signs URIs. |
| 16 | * |
| 17 | * @author Fabien Potencier <fabien@symfony.com> |
| 18 | */ |
| 19 | class UriSigner |
| 20 | { |
| 21 | private $secret; |
| 22 | private $parameter; |
| 23 | /** |
| 24 | * @param string $secret A secret |
| 25 | * @param string $parameter Query string parameter to use |
| 26 | */ |
| 27 | public function __construct(string $secret, string $parameter = '_hash') |
| 28 | { |
| 29 | $this->secret = $secret; |
| 30 | $this->parameter = $parameter; |
| 31 | } |
| 32 | /** |
| 33 | * Signs a URI. |
| 34 | * |
| 35 | * The given URI is signed by adding the query string parameter |
| 36 | * which value depends on the URI and the secret. |
| 37 | * |
| 38 | * @return string |
| 39 | */ |
| 40 | public function sign(string $uri) |
| 41 | { |
| 42 | $url = parse_url($uri); |
| 43 | if (isset($url['query'])) { |
| 44 | parse_str($url['query'], $params); |
| 45 | } else { |
| 46 | $params = []; |
| 47 | } |
| 48 | $uri = $this->buildUrl($url, $params); |
| 49 | $params[$this->parameter] = $this->computeHash($uri); |
| 50 | return $this->buildUrl($url, $params); |
| 51 | } |
| 52 | /** |
| 53 | * Checks that a URI contains the correct hash. |
| 54 | * |
| 55 | * @return bool |
| 56 | */ |
| 57 | public function check(string $uri) |
| 58 | { |
| 59 | $url = parse_url($uri); |
| 60 | if (isset($url['query'])) { |
| 61 | parse_str($url['query'], $params); |
| 62 | } else { |
| 63 | $params = []; |
| 64 | } |
| 65 | if (empty($params[$this->parameter])) { |
| 66 | return \false; |
| 67 | } |
| 68 | $hash = $params[$this->parameter]; |
| 69 | unset($params[$this->parameter]); |
| 70 | return hash_equals($this->computeHash($this->buildUrl($url, $params)), $hash); |
| 71 | } |
| 72 | public function checkRequest(Request $request) : bool |
| 73 | { |
| 74 | $qs = ($qs = $request->server->get('QUERY_STRING')) ? '?' . $qs : ''; |
| 75 | // we cannot use $request->getUri() here as we want to work with the original URI (no query string reordering) |
| 76 | return $this->check($request->getSchemeAndHttpHost() . $request->getBaseUrl() . $request->getPathInfo() . $qs); |
| 77 | } |
| 78 | private function computeHash(string $uri) : string |
| 79 | { |
| 80 | return base64_encode(hash_hmac('sha256', $uri, $this->secret, \true)); |
| 81 | } |
| 82 | private function buildUrl(array $url, array $params = []) : string |
| 83 | { |
| 84 | ksort($params, \SORT_STRING); |
| 85 | $url['query'] = http_build_query($params, '', '&'); |
| 86 | $scheme = isset($url['scheme']) ? $url['scheme'] . '://' : ''; |
| 87 | $host = $url['host'] ?? ''; |
| 88 | $port = isset($url['port']) ? ':' . $url['port'] : ''; |
| 89 | $user = $url['user'] ?? ''; |
| 90 | $pass = isset($url['pass']) ? ':' . $url['pass'] : ''; |
| 91 | $pass = $user || $pass ? "{$pass}@" : ''; |
| 92 | $path = $url['path'] ?? ''; |
| 93 | $query = isset($url['query']) && $url['query'] ? '?' . $url['query'] : ''; |
| 94 | $fragment = isset($url['fragment']) ? '#' . $url['fragment'] : ''; |
| 95 | return $scheme . $user . $pass . $host . $port . $path . $query . $fragment; |
| 96 | } |
| 97 | } |
| 98 |