diff --git a/src/Controllers/ShareDraftController.php b/src/Controllers/ShareDraftController.php index 3ea1914..883a228 100644 --- a/src/Controllers/ShareDraftController.php +++ b/src/Controllers/ShareDraftController.php @@ -52,6 +52,8 @@ class ShareDraftController extends Controller */ protected static $isViewingPreview = false; + private array $redirectRecursionIterations = []; + /** * @return bool */ @@ -172,8 +174,8 @@ private function getRenderedPageByURL(string $url): HTTPResponse $variables['_SERVER']['HTTP_USER_AGENT'] = isset($variables['_SERVER']['HTTP_USER_AGENT']) && $variables['_SERVER']['HTTP_USER_AGENT'] - ? $variables['_SERVER']['HTTP_USER_AGENT'] - : 'CLI'; + ? $variables['_SERVER']['HTTP_USER_AGENT'] + : 'CLI'; Environment::setVariables($variables); @@ -183,6 +185,15 @@ private function getRenderedPageByURL(string $url): HTTPResponse $response = Director::singleton()->handleRequest($pageRequest); if ($response->isRedirect()) { + if (in_array($url, $this->redirectRecursionIterations)) { + throw new \Exception("Infinite recursion detected." . $this->getRedirectRecursionIterationsLog($url)); + } + + $this->redirectRecursionIterations[] = $url; + if (count($this->redirectRecursionIterations) >= 30) { + throw new \Exception("Max redirect recursions reached." . $this->getRedirectRecursionIterationsLog()); + } + // The redirect will probably be Absolute URL so just want the path $newUrl = parse_url($response->getHeader('location') ?? '', PHP_URL_PATH); @@ -192,6 +203,13 @@ private function getRenderedPageByURL(string $url): HTTPResponse return $response; } + private function getRedirectRecursionIterationsLog(string $appendUrl = ''): string + { + return "\n\nRedirected URLs stack: \n" + . implode("\n", $this->redirectRecursionIterations) + . ($appendUrl ? "\n$appendUrl" : ''); + } + /** * @return DBHTMLText */