From b5cf44637522b853b5eb8fa5782e7a2c0e041093 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Sun, 15 Oct 2023 01:44:56 +0900 Subject: [PATCH] Properly escape and unescape Blade-style directives --- .../parsers/template/TemplateParser_v2.php | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/common/framework/parsers/template/TemplateParser_v2.php b/common/framework/parsers/template/TemplateParser_v2.php index 24cf085fc..9fa1a9b87 100644 --- a/common/framework/parsers/template/TemplateParser_v2.php +++ b/common/framework/parsers/template/TemplateParser_v2.php @@ -202,7 +202,7 @@ class TemplateParser_v2 $content = preg_replace_callback('#(<\?php|<\?(?!=))(.+?)(\?>)#s', $callback, $content); $content = preg_replace_callback('#(\{@)(.+?)(\})#s', $callback, $content); - $content = preg_replace_callback('#(@php)(.+?)(@endphp)#s', $callback, $content); + $content = preg_replace_callback('#(?|@use\x20?\([\'"]([^\'"]+)[\'"],\s*[\'"]([^\'"]+)[\'"]\))[\x09\x20]*$#m'; + $regexp = '#^[\x09\x20]*(?:|(? %s="%s"', $condition, $match[1], $match[1]); @@ -683,13 +681,13 @@ class TemplateParser_v2 protected function _convertMiscDirectives(string $content): string { // Insert CSRF tokens. - $content = preg_replace_callback('#@csrf#', function($match) { + $content = preg_replace_callback('#(?" />'; }, $content); // Insert JSON and lang codes. $parentheses = self::_getRegexpForParentheses(2); - $content = preg_replace_callback('#@(json|lang)('. $parentheses . ')#', function($match) { + $content = preg_replace_callback('#(? '{', '}' => '}', '$' => '$', '\\$' => '$', ]); + // Restore escaped Blade-style directives. + $content = preg_replace([ + '#@(@[a-z]{2,})#', + '#@(\{\{)#', + ], '$1', $content); + // Remove unnecessary spaces before and after PHP tags. $content = preg_replace([ '#^[\x20\x09]+(<\?(?:php\b|=))#m',