From b1ccd3c9e20b86f98cd5564d66292e47c0bf71d2 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Fri, 27 Nov 2026 09:42:25 +0700 Subject: [PATCH] fix(Helpers): escape src and attribute values in img() and script_tag() Escape src and non-null attribute values in img() and script_tag() using esc() to prevent malformed HTML and attribute injection, consistent with link_tag() and stringify_attributes(). --- docs/changelogs/v4.7.7.md | 1 + system/Helpers/html_helper.php | 12 +++++------ tests/system/Helpers/HTMLHelperTest.php | 28 +++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index 39eddfb3664d..ebd9c53ef7c7 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -40,6 +40,7 @@ Release Date: Unreleased - **Helpers:** Fixed a bug where `parse_subdomain()` incorrectly treated hostnames with ports (such as IP addresses and two-part TLD domains) as having unexpected subdomains. - **Helpers:** Fixed a bug in `plural()` where the regex for `matrix`, `vertex`, and `index` lacked non-capturing grouping and an end-of-string anchor for the `ix` alternative, causing incorrect pluralization of words like `complex`, `sex`, and strings containing `matrix` before the end. Expanded `plural()` and `singular()` to support additional words ending in `ix` or `ex` (`apex`, `appendix`, `codex`, `cortex`, `latex`, `murex`, `vortex`). - **Helpers:** Fixed bugs in `ArrayHelper::dotKeyExists()` where `'0'` path segments caused early loop termination, non-array intermediate values threw a `TypeError`, and paths with segments after a wildcard failed when the wildcard matched multiple elements. +- **Helpers:** Fixed a bug where `img()` and `script_tag()` did not escape `src` and attribute values when passed as strings or associative arrays. - **Helpers:** Fixed escaping of attribute values in HTML media helpers to prevent malformed HTML and attribute injection. - **HTTP:** Fixed a bug where query-string based routing truncated additional query parameters in `SiteURIFactory::parseRequestURI()` due to incorrect array index access. - **HTTP:** Fixed a bug where `UploadedFile::move()` and `UploadedFile::store()` on Windows could store a file under a different name than `getName()` reported, or write its contents to an NTFS alternate data stream. Filenames ending in a period or space or containing a colon are now rejected, including explicitly supplied names. diff --git a/system/Helpers/html_helper.php b/system/Helpers/html_helper.php index ccfdb0d832a9..e238c146edd0 100644 --- a/system/Helpers/html_helper.php +++ b/system/Helpers/html_helper.php @@ -110,9 +110,9 @@ function img($src = '', bool $indexPage = false, $attributes = ''): string // Check for a relative URI if (preg_match('#^([a-z]+:)?//#i', $src['src']) !== 1 && ! str_starts_with($src['src'], 'data:')) { if ($indexPage) { - $img .= ' src="' . site_url($src['src']) . '"'; + $img .= ' src="' . esc(site_url($src['src'])) . '"'; } else { - $img .= ' src="' . slash_item('baseURL') . $src['src'] . '"'; + $img .= ' src="' . esc(slash_item('baseURL') . $src['src']) . '"'; } unset($src['src']); @@ -120,7 +120,7 @@ function img($src = '', bool $indexPage = false, $attributes = ''): string // Append any other values foreach ($src as $key => $value) { - $img .= ' ' . $key . '="' . $value . '"'; + $img .= ' ' . $key . '="' . esc($value) . '"'; } // Prevent passing completed values to stringify_attributes @@ -204,13 +204,13 @@ function script_tag($src = '', bool $indexPage = false): string foreach ($src as $k => $v) { if ($k === 'src' && preg_match('#^([a-z]+:)?//#i', $v) !== 1) { if ($indexPage) { - $script .= 'src="' . site_url($v) . '" '; + $script .= 'src="' . esc(site_url($v)) . '" '; } else { - $script .= 'src="' . slash_item('baseURL') . $v . '" '; + $script .= 'src="' . esc(slash_item('baseURL') . $v) . '" '; } } else { // for attributes without values, like async or defer, use NULL. - $script .= $k . (null === $v ? ' ' : '="' . $v . '" '); + $script .= $k . (null === $v ? ' ' : '="' . esc($v) . '" '); } } diff --git a/tests/system/Helpers/HTMLHelperTest.php b/tests/system/Helpers/HTMLHelperTest.php index ab6c4508d8f6..0eb5960bb2da 100644 --- a/tests/system/Helpers/HTMLHelperTest.php +++ b/tests/system/Helpers/HTMLHelperTest.php @@ -256,6 +256,20 @@ public function testIMGXHTMLWithIndexpage(): void $this->enableHtml5(); } + public function testImgEscapesAttributesAndSrc(): void + { + $payload = 'x" onmouseover="alert(1)" data-x="'; + + $expectedArray = 'x" onmouseover="alert(1)" data-x="'; + $this->assertSame($expectedArray, img(['src' => 'https://example.com/a.png', 'alt' => $payload])); + + $expectedRelative = ''; + $this->assertSame($expectedRelative, img('a.png' . $payload)); + + $expectedIndexPage = ''; + $this->assertSame($expectedIndexPage, img('a.png' . $payload, true)); + } + public function testImgData(): void { $expected = 'data:image/gif;base64,' . $this->imgData; @@ -359,6 +373,20 @@ public function testScriptTagWithoutAnyArg(): void $this->assertSame($expected, script_tag()); } + public function testScriptTagEscapesAttributesAndSrc(): void + { + $payload = 'x" onmouseover="alert(1)" data-x="'; + + $expectedArray = ''; + $this->assertSame($expectedArray, script_tag(['src' => 'https://example.com/a.js', 'data-x' => $payload, 'async' => null])); + + $expectedRelative = ''; + $this->assertSame($expectedRelative, script_tag('a.js' . $payload)); + + $expectedIndexPage = ''; + $this->assertSame($expectedIndexPage, script_tag('a.js' . $payload, true)); + } + public function testLinkTag(): void { $target = 'css/mystyles.css';