Skip to content

Commit

Permalink
Merge pull request #217 from creative-commoners/pulls/2.9/backport-214
Browse files Browse the repository at this point in the history
FIX Infinite recursion sanity checks with more helpful error messages…
  • Loading branch information
GuySartorelli authored Nov 10, 2023
2 parents bfeb2bd + e490370 commit 46da869
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/Controllers/ShareDraftController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class ShareDraftController extends Controller
*/
protected static $isViewingPreview = false;

private array $redirectRecursionIterations = [];

/**
* @return bool
*/
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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
*/
Expand Down

0 comments on commit 46da869

Please sign in to comment.