fix(template-helper): restrict escapeButPreserveUris to http/https schemes

escapeButPreserveUris() matched any scheme via [A-z]+?://, which also
turns javascript://, vbscript:// and data:// URIs into clickable links
on the generated error page, allowing script execution on click (XSS).
The [A-z] class was also invalid, spanning extra ASCII punctuation.

Restrict the scheme to http/https, the only two schemes that make
sense to linkify on an error page, leaving other schemes as plain
escaped text.
This commit is contained in:
dualfroz committed 2026-09-04 03:00:40 +02:00
1 parent 24517bcbde
commit 7f2cd8f154
2 files changed
+44 -1

No files matched your search

+6 -1
View File
@@ -80,6 +80,11 @@ class TemplateHelper
* Escapes a string for output in an HTML document, but preserves
* URIs within it, and converts them to clickable anchor elements.
*
* Only http and https URIs are linkified. Other schemes (e.g.
* javascript:, vbscript:, data:) are left as plain escaped text,
* since linkifying them would allow script execution when the
* generated link is clicked (see GHSA / issue #791).
*
* @param string $raw
* @return string
*/
@@ -87,7 +92,7 @@ class TemplateHelper
{
$escaped = $this->escape($raw);
return preg_replace(
"@([A-z]+?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@",
"@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@i",
"<a href=\"$1\" target=\"_blank\" rel=\"noreferrer noopener\">$1</a>",
$escaped
);