Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Infinite recursion sanity checks with more helpful error messages #214

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/Controllers/ShareDraftController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class ShareDraftController extends Controller
*/
protected static $isViewingPreview = false;

/**
* @var array
*/
private $redirectRecursionIterations = [];
nathanbrauer marked this conversation as resolved.
Show resolved Hide resolved

/**
* @return bool
*/
Expand Down Expand Up @@ -172,8 +177,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 +188,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));
nathanbrauer marked this conversation as resolved.
Show resolved Hide resolved
}

$this->redirectRecursionIterations[] = $url;
if (count($this->redirectRecursionIterations) >= 30) {
throw new \Exception("Max redirect recursions reached.".$this->getRedirectRecursionIterationsLog());
nathanbrauer marked this conversation as resolved.
Show resolved Hide resolved
}

// 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 +206,17 @@ private function getRenderedPageByURL(string $url): HTTPResponse
return $response;
}

/**
* @param string $append_url
* @return string
*/
nathanbrauer marked this conversation as resolved.
Show resolved Hide resolved
protected function getRedirectRecursionIterationsLog(string $append_url=''): string
nathanbrauer marked this conversation as resolved.
Show resolved Hide resolved
{
return "\n\nRedirected URLs stack: \n"
. implode("\n", $this->redirectRecursionIterations)
. ($append_url ? "\n$append_url" : '');
nathanbrauer marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @return DBHTMLText
*/
Expand Down