UriSigner.php 3.2 KB

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