refactor asset rendering with HTML escaping

Improve security by escaping asset attributes and URLs in phpb_registered_assets.
Refactor Extensions class to render assets as HTML strings and add getAssetsArray for raw access.
Update phpb_e helper to accept nullable strings.
This commit is contained in:
2026-11-23 12:35:46 +01:00
parent 84f3be9fe0
commit b6960efd82
2 changed files with 53 additions and 31 deletions
+8 -18
View File
@@ -7,13 +7,13 @@ if (! function_exists('phpb_e')) {
/**
* Encode HTML special characters in a string.
*
* @param string $value
* @param string|null $value
* @param bool $doubleEncode
* @return string
*/
function phpb_e(string $value, bool $doubleEncode = true): string
function phpb_e(?string $value, bool $doubleEncode = true): string
{
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', $doubleEncode);
return htmlspecialchars($value ?? '', ENT_QUOTES, 'UTF-8', $doubleEncode);
}
}
@@ -641,27 +641,17 @@ if (! function_exists('phpb_registered_assets')) {
*/
function phpb_registered_assets(string $location = 'header'): void
{
$assets = [];
if($location === 'header'){
$assets = Extensions::getHeaderAssets();
}elseif($location === 'footer'){
$assets = Extensions::getFooterAssets();
}elseif($location === 'admin-header'){
$assets = Extensions::getAdminHeaderAssets();
}elseif($location === 'admin-footer'){
$assets = Extensions::getAdminFooterAssets();
}
$assets = Extensions::getAssetsArray($location);
foreach ($assets as $asset) {
$attributes = '';
foreach ($asset['attributes'] as $key => $value) {
$attributes .= ' ' . $key . '="' . $value . '"';
$attributes .= ' ' . htmlspecialchars($key, ENT_QUOTES, 'UTF-8') . '="' . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '"';
}
if ($asset['type'] == 'style') {
echo '<link rel="stylesheet" href="' . $asset['src'] . '" ' . $attributes . '/>';
if ($asset['type'] === 'style') {
echo '<link rel="stylesheet" href="' . htmlspecialchars($asset['src'], ENT_QUOTES, 'UTF-8') . '"' . $attributes . '/>';
} else {
echo '<script type="text/javascript" src="' . $asset['src'] . '" ' . $attributes . '></script>';
echo '<script type="text/javascript" src="' . htmlspecialchars($asset['src'], ENT_QUOTES, 'UTF-8') . '"' . $attributes . '></script>';
}
}
}
+45 -13
View File
@@ -241,7 +241,7 @@ class Extensions
}
/**
* Get all header assets.
* Get all html blocks for the given location.
*/
public static function getHtmlBlocks(string $location = 'head'): string
{
@@ -249,31 +249,63 @@ class Extensions
}
/**
* Get all header assets.
* Render assets as HTML string.
*/
public static function getHeaderAssets(): array
protected static function renderAssets(array $assets): string
{
return self::$assets['header'];
$html = '';
foreach ($assets as $asset) {
$attributes = '';
foreach ($asset['attributes'] as $key => $value) {
$attributes .= ' ' . htmlspecialchars($key, ENT_QUOTES, 'UTF-8') . '="' . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '"';
}
if ($asset['type'] === 'style') {
$html .= '<link rel="stylesheet" href="' . htmlspecialchars($asset['src'], ENT_QUOTES, 'UTF-8') . '"' . $attributes . '/>';
} else {
$html .= '<script type="text/javascript" src="' . htmlspecialchars($asset['src'], ENT_QUOTES, 'UTF-8') . '"' . $attributes . '></script>';
}
}
return $html;
}
/**
* Get all footer assets.
* Get all header assets as HTML string.
*/
public static function getFooterAssets() {
return self::$assets['footer'];
public static function getHeaderAssets(): string
{
return self::renderAssets(self::$assets['header']);
}
/**
* Get all admin header assets.
* Get all footer assets as HTML string.
*/
public static function getAdminHeaderAssets() {
return self::$assets['admin-header'];
public static function getFooterAssets(): string
{
return self::renderAssets(self::$assets['footer']);
}
/**
* Get all admin footer assets.
* Get all admin header assets as HTML string.
*/
public static function getAdminFooterAssets() {
return self::$assets['admin-footer'];
public static function getAdminHeaderAssets(): string
{
return self::renderAssets(self::$assets['admin-header']);
}
/**
* Get all admin footer assets as HTML string.
*/
public static function getAdminFooterAssets(): string
{
return self::renderAssets(self::$assets['admin-footer']);
}
/**
* Get raw assets array for internal use by phpb_registered_assets().
*/
public static function getAssetsArray(string $location): array
{
return self::$assets[$location] ?? [];
}
}