Skip to content

Commit 641ee5b

Browse files
authored
Merge commit from fork
* fix: prevent View Parser from interpreting substituted values as template syntax * fix: handle all repeated nested pairs in View Parser * fix: prevent Parser syntax injection through built-in plugins * docs: clarify existing support for closures in Parser filters
1 parent 12a9bb5 commit 641ee5b

7 files changed

Lines changed: 439 additions & 9 deletions

File tree

‎app/Config/View.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class View extends BaseView
3030
* { title|esc(js) }
3131
* { created_on|date(Y-m-d)|esc(attr) }
3232
*
33-
* @var array<string, (callable(mixed): mixed)&string>
33+
* @var array<string, callable(mixed): mixed>
3434
*/
3535
public $filters = [];
3636

‎system/Config/View.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class View extends BaseConfig
3737
*
3838
* @psalm-suppress UndefinedDocblockClass
3939
*
40-
* @var array<string, (callable(mixed): mixed)&string>
40+
* @var array<string, callable(mixed): mixed>
4141
*/
4242
public $filters = [];
4343

‎system/View/Parser.php‎

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ class Parser extends View
7272
*/
7373
protected $dataContexts = [];
7474

75+
/**
76+
* Substitution values are temporarily replaced with opaque tokens so they
77+
* cannot be interpreted as Parser syntax by a later substitution pass.
78+
*
79+
* @var array<string, string>
80+
*/
81+
private array $replacementTokens = [];
82+
83+
private ?string $replacementTokenPrefix = null;
84+
7585
/**
7686
* Constructor
7787
*
@@ -231,6 +241,29 @@ protected function parse(string $template, array $data = [], ?array $options = n
231241
return '';
232242
}
233243

244+
$replacementState = [
245+
$this->replacementTokens,
246+
$this->replacementTokenPrefix,
247+
];
248+
249+
$this->replacementTokens = [];
250+
$this->replacementTokenPrefix = bin2hex(random_bytes(16));
251+
252+
try {
253+
return $this->parseTemplate($template, $data);
254+
} finally {
255+
[
256+
$this->replacementTokens,
257+
$this->replacementTokenPrefix,
258+
] = $replacementState;
259+
}
260+
}
261+
262+
/**
263+
* @param array<string, mixed> $data
264+
*/
265+
private function parseTemplate(string $template, array $data): string
266+
{
234267
// Remove any possible PHP tags since we don't support it
235268
// and parseConditionals needs it clean anyway...
236269
$template = str_replace(['<?', '?>'], ['&lt;?', '?&gt;'], $template);
@@ -281,7 +314,9 @@ protected function parse(string $template, array $data = [], ?array $options = n
281314
}
282315
}
283316

284-
return $this->insertNoparse($template);
317+
$template = $this->insertNoparse($template);
318+
319+
return strtr($template, $this->replacementTokens);
285320
}
286321

287322
/**
@@ -355,7 +390,9 @@ protected function parsePair(string $variable, array $data, string $template): a
355390
$pair = $this->parsePair($key, $val, $match[1]);
356391

357392
if ($pair !== []) {
358-
$pairs[array_keys($pair)[0]] = true;
393+
foreach (array_keys($pair) as $pattern) {
394+
$pairs[$pattern] = true;
395+
}
359396

360397
$temp = array_merge($temp, $pair);
361398
}
@@ -541,8 +578,12 @@ protected function replaceSingle($pattern, $content, $template, bool $escape = f
541578
{
542579
$content = (string) $content;
543580

581+
// Callers enable escaping for scalar data, but not assembled pairs.
582+
// Protect scalar data even when {! !} disables escaping below.
583+
$protectReplacement = $escape;
584+
544585
// Replace the content in the template
545-
return preg_replace_callback($pattern, function ($matches) use ($content, $escape): string {
586+
return preg_replace_callback($pattern, function ($matches) use ($content, $escape, $protectReplacement): string {
546587
// Check for {! !} syntax to not escape this one.
547588
if (
548589
str_starts_with($matches[0], $this->leftDelimiter . '!')
@@ -551,10 +592,31 @@ protected function replaceSingle($pattern, $content, $template, bool $escape = f
551592
$escape = false;
552593
}
553594

554-
return $this->prepareReplacement($matches, $content, $escape);
595+
$replacement = $this->prepareReplacement($matches, $content, $escape);
596+
597+
if (! $protectReplacement || $this->replacementTokenPrefix === null) {
598+
return $replacement;
599+
}
600+
601+
return $this->protectReplacement($replacement);
555602
}, (string) $template);
556603
}
557604

605+
/**
606+
* Keeps rendered data opaque until all template processing is complete.
607+
*/
608+
private function protectReplacement(string $replacement): string
609+
{
610+
if ($this->replacementTokenPrefix === null) {
611+
return $replacement;
612+
}
613+
614+
$token = "\x1A{$this->replacementTokenPrefix}:" . count($this->replacementTokens) . "\x1A";
615+
$this->replacementTokens[$token] = $replacement;
616+
617+
return $token;
618+
}
619+
558620
/**
559621
* Callback used during parse() to apply any filters to the value.
560622
*
@@ -699,9 +761,19 @@ protected function parsePlugins(string $template)
699761
}
700762
}
701763

702-
$template = $isPair
703-
? str_replace($match[0], $callable($match[2], $params), $template)
704-
: str_replace($match[0], $callable($params), $template);
764+
$replacement = $isPair ? $callable($match[2], $params) : $callable($params);
765+
766+
// Built-in plugins return rendered data. Custom plugins may
767+
// intentionally return template code for subsequent processing.
768+
// Identify the implementation, not the alias applications can override.
769+
if (
770+
is_string($callable)
771+
&& str_starts_with(strtolower(ltrim($callable, '\\')), strtolower(Plugins::class) . '::')
772+
) {
773+
$replacement = $this->protectReplacement((string) $replacement);
774+
}
775+
776+
$template = str_replace($match[0], $replacement, $template);
705777
}
706778
}
707779

0 commit comments

Comments
 (0)