3 namespace BookStack\Util;
5 use BookStack\Exceptions\HttpFetchException;
9 protected string $config;
11 public function __construct(string $config = null)
13 $this->config = $config ?? config('app.ssr_hosts') ?? '';
17 * @throws HttpFetchException
19 public function ensureAllowed(string $url): void
21 if (!$this->allowed($url)) {
22 throw new HttpFetchException(trans('errors.http_ssr_url_no_match'));
27 * Check if the given URL is allowed by the configured SSR host values.
29 public function allowed(string $url): bool
31 $allowed = $this->getHostPatterns();
33 foreach ($allowed as $pattern) {
34 if ($this->urlMatchesPattern($url, $pattern)) {
42 protected function urlMatchesPattern($url, $pattern): bool
44 $pattern = rtrim(trim($pattern), '/');
47 if (empty($pattern) || empty($url)) {
51 $quoted = preg_quote($pattern, '/');
52 $regexPattern = str_replace('\*', '.*', $quoted);
54 return preg_match('/^' . $regexPattern . '($|\/.*$|#.*$)/i', $url);
60 protected function getHostPatterns(): array
62 return explode(' ', strtolower($this->config));