Skip to content

Commit

Permalink
Infinite recursion protections
Browse files Browse the repository at this point in the history
Fixes internal infinite recursion on Nginx/PHP-FPM builds and implements some protections against unanticipated infinite recursions (previously timed out or hit memory limits)
  • Loading branch information
nathanbrauer committed Oct 14, 2023
1 parent f315bfd commit dcde0d4
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 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 = [];

/**
* @return bool
*/
Expand Down Expand Up @@ -172,8 +177,11 @@ 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';

// This extra step required for PHP-FPM/Nginx implementations
$variables['env'] = $variables['_SERVER'];

Environment::setVariables($variables);

Expand All @@ -183,6 +191,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. \n\nRedirected URLs stack: \n" . implode("\n", $this->redirectRecursionIterations) . "\n$url");
}

$this->redirectRecursionIterations[] = $url;
if (count($this->redirectRecursionIterations) >= 30) {
throw new \Exception("Max redirect recursions reached. \n\nRedirected URLs stack: \n" . implode("\n", $this->redirectRecursionIterations));
}

// The redirect will probably be Absolute URL so just want the path
$newUrl = parse_url($response->getHeader('location') ?? '', PHP_URL_PATH);

Expand Down

0 comments on commit dcde0d4

Please sign in to comment.