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

Prevent recursion in title_format #1827

Merged
merged 1 commit into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions src/Utils/ContentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ function ($match) use ($record, $locale) {
return $field;
}

// We must ensure this method is not called recursively. For example
// `title_format: {title}` would otherwise result in an endless loop
// @see https://github.com/bolt/core/issues/1825
if (Recursion::detect()) {
return '(recursion)';
}

if (array_key_exists($match[1], $record->getExtras())) {
// If it's the icon, return an html element
if ($match[1] === 'icon') {
Expand Down
30 changes: 30 additions & 0 deletions src/Utils/Recursion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Bolt\Utils;

class Recursion
{
public static function detect(): bool
{
$backtrace = [];

foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $call) {
if (array_key_exists('class', $call)) {
$backtrace[] = $call['class'] . $call['type'] . $call['function'];
} else {
$backtrace[] = basename($call['file']) . '::' . $call['function'];
}
}

// The one we called from is [1]
$callee = $backtrace[1];

$count = count(array_filter($backtrace, function ($a) use ($callee) {
return $a === $callee;
}));

return $count > 1;
}
}