Skip to content

Commit db765cd

Browse files
committed
#11975 strict check added for smarty blade include
1 parent fbce211 commit db765cd

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

classes/template/PKPTemplateManager.php

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2512,29 +2512,41 @@ public function smartyLoadNavigationMenuArea($params, $smarty = null)
25122512
*
25132513
* Custom Smarty function for injecting a blade view in a smarty template
25142514
*
2515-
* @param array $params associative array
2515+
* IMPORTANT: The file path must follow strict Blade view format:
2516+
* - Use dot notation: 'path.to.template' or 'namespace::path.to.template'
2517+
* - NO slashes, extensions, or path traversal patterns
2518+
*
2519+
* Valid: 'frontend.pages.article', 'app::submissions.view'
2520+
* Invalid: 'frontend/pages/article', 'article.blade.php', '../config'
2521+
*
2522+
* @param array $params associative array with 'file' key
25162523
* @param Smarty|null $smarty
25172524
*
25182525
* @return string The compiled content of the blade view
2519-
*
2520-
* @throws Exception If the file parameter is missing or the blade view does not exist
2526+
*
2527+
* @throws Exception If validation fails or view does not exist
25212528
*/
25222529
public function smartyIncludeBlade($params, $smarty = null): string
25232530
{
2524-
if (!isset($params['file'])) {
2525-
throw new Exception('file parameter is missing in {include_blade}');
2526-
}
2527-
2528-
$file = $params['file'];
2529-
2530-
if (str_contains($file, '..') || str_contains($file, '\\') || str_starts_with($file, '/')) {
2531-
throw new Exception("Invalid file path: path traversal or absolute paths not allowed in given blade file {$file}");
2532-
}
2533-
2534-
$file = str_replace(['/', '.blade.php', '.blade'], ['.', '', ''], $file);
2531+
$file = $params['file'] ?? null;
2532+
2533+
match (true) {
2534+
!isset($file) =>
2535+
throw new Exception('file parameter is missing in {include_blade}'),
2536+
empty($file) =>
2537+
throw new Exception('Blade view path can not be empty'),
2538+
str_ends_with($file, '.blade.php') || str_ends_with($file, '.blade') || str_ends_with($file, '.php') =>
2539+
throw new Exception("Invalid Blade view path '{$file}': file extensions not allowed. Use dot notation (e.g., 'frontend.pages.article')"),
2540+
str_contains($file, '/') || str_contains($file, '\\') =>
2541+
throw new Exception("Invalid Blade view path '{$file}': slashes not allowed. Use dot notation (e.g., 'frontend.pages.article')"),
2542+
str_contains($file, '..') =>
2543+
throw new Exception("Invalid Blade view path '{$file}': path traversal patterns (..) not allowed"),
2544+
str_starts_with($file, '.') || str_ends_with($file, '.') =>
2545+
throw new Exception("Invalid Blade view path '{$file}': path cannot start or end with a dot"),
2546+
default => null, // Valid path, continue
2547+
};
25352548

25362549
// If the file does not contain a namespace, try to find it in the registered view namespaces
2537-
// by attaching the namespace to the view path as `app::view.path`
25382550
if (!Str::contains($file, '::')) {
25392551
$pathNamespaces = collect(config('view.paths'))->keys()->toArray();
25402552
foreach ($pathNamespaces as $pathNamespace) {
@@ -2546,15 +2558,15 @@ public function smartyIncludeBlade($params, $smarty = null): string
25462558
}
25472559

25482560
if (!view()->exists($file)) {
2549-
throw new Exception("blade view {$params['file']} does not exist");
2561+
throw new Exception("Blade view '{$file}' does not exist");
25502562
}
25512563

25522564
unset($params['file']);
2553-
2565+
25542566
// Merge the template variables into the params,
25552567
// with the provided params taking precedence over the template variables
25562568
$params = array_merge($this->getTemplateVars(), $params);
2557-
2569+
25582570
return view($file, $params)->render();
25592571
}
25602572

0 commit comments

Comments
 (0)