‪TYPO3CMS  10.4
RedirectUrlValidator.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Log\LoggerAwareInterface;
21 use Psr\Log\LoggerAwareTrait;
24 
31 class ‪RedirectUrlValidator implements LoggerAwareInterface
32 {
33  use LoggerAwareTrait;
34 
38  protected ‪$siteFinder;
39 
44  {
45  $this->siteFinder = ‪$siteFinder ?? GeneralUtility::makeInstance(SiteFinder::class);
46  }
47 
54  public function ‪isValid(string $value): bool
55  {
56  if ($value === '') {
57  return false;
58  }
59  // Validate the URL
60  if ($this->‪isRelativeUrl($value) || $this->‪isInCurrentDomain($value) || $this->‪isInLocalDomain($value)) {
61  return true;
62  }
63  // URL is not allowed
64  $this->logger->warning('Url "' . $value . '" was not accepted.');
65  return false;
66  }
67 
75  protected function ‪isInCurrentDomain(string $url): bool
76  {
77  $urlWithoutSchema = preg_replace('#^https?://#', '', $url) ?? '';
78  $siteUrlWithoutSchema = preg_replace('#^https?://#', '', GeneralUtility::getIndpEnv('TYPO3_SITE_URL')) ?? '';
79  return strpos($urlWithoutSchema . '/', GeneralUtility::getIndpEnv('HTTP_HOST') . '/') === 0
80  && strpos($urlWithoutSchema, $siteUrlWithoutSchema) === 0;
81  }
82 
89  protected function ‪isInLocalDomain(string $url): bool
90  {
91  if (!‪GeneralUtility::isValidUrl($url)) {
92  return false;
93  }
94  $parsedUrl = parse_url($url);
95  if ($parsedUrl['scheme'] === 'http' || $parsedUrl['scheme'] === 'https') {
96  $host = $parsedUrl['host'];
97  foreach ($this->siteFinder->getAllSites() as $site) {
98  if ($site->getBase()->getHost() === $host) {
99  return true;
100  }
101  }
102  }
103  return false;
104  }
105 
112  protected function ‪isRelativeUrl($url): bool
113  {
114  $url = GeneralUtility::sanitizeLocalUrl($url);
115  if (!empty($url)) {
116  $parsedUrl = @parse_url($url);
117  if ($parsedUrl !== false && !isset($parsedUrl['scheme']) && !isset($parsedUrl['host'])) {
118  // If the relative URL starts with a slash, we need to check if it's within the current site path
119  return $parsedUrl['path'][0] !== '/' || GeneralUtility::isFirstPartOfStr($parsedUrl['path'], GeneralUtility::getIndpEnv('TYPO3_SITE_PATH'));
120  }
121  }
122  return false;
123  }
124 }
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\FrontendLogin\Validation\RedirectUrlValidator\$siteFinder
‪SiteFinder $siteFinder
Definition: RedirectUrlValidator.php:37
‪TYPO3\CMS\FrontendLogin\Validation\RedirectUrlValidator\__construct
‪__construct(?SiteFinder $siteFinder)
Definition: RedirectUrlValidator.php:42
‪TYPO3\CMS\FrontendLogin\Validation\RedirectUrlValidator\isInLocalDomain
‪bool isInLocalDomain(string $url)
Definition: RedirectUrlValidator.php:88
‪TYPO3\CMS\FrontendLogin\Validation\RedirectUrlValidator\isRelativeUrl
‪bool isRelativeUrl($url)
Definition: RedirectUrlValidator.php:111
‪TYPO3\CMS\Core\Utility\GeneralUtility\isValidUrl
‪static bool isValidUrl($url)
Definition: GeneralUtility.php:944
‪TYPO3\CMS\FrontendLogin\Validation\RedirectUrlValidator
Definition: RedirectUrlValidator.php:32
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\FrontendLogin\Validation\RedirectUrlValidator\isValid
‪bool isValid(string $value)
Definition: RedirectUrlValidator.php:53
‪TYPO3\CMS\FrontendLogin\Validation\RedirectUrlValidator\isInCurrentDomain
‪bool isInCurrentDomain(string $url)
Definition: RedirectUrlValidator.php:74
‪TYPO3\CMS\FrontendLogin\Validation
Definition: RedirectUrlValidator.php:18