Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelogs/v4.7.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions system/Helpers/html_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,17 @@ 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']);
}

// Append any other values
foreach ($src as $key => $value) {
$img .= ' ' . $key . '="' . $value . '"';
$img .= ' ' . $key . '="' . esc($value) . '"';
}

// Prevent passing completed values to stringify_attributes
Expand Down Expand Up @@ -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) . '" ');
}
}

Expand Down
28 changes: 28 additions & 0 deletions tests/system/Helpers/HTMLHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,20 @@ public function testIMGXHTMLWithIndexpage(): void
$this->enableHtml5();
}

public function testImgEscapesAttributesAndSrc(): void
{
$payload = 'x" onmouseover="alert(1)" data-x="';

$expectedArray = '<img src="https://example.com/a.png" alt="x&quot; onmouseover=&quot;alert(1)&quot; data-x=&quot;">';
$this->assertSame($expectedArray, img(['src' => 'https://example.com/a.png', 'alt' => $payload]));

$expectedRelative = '<img src="http://example.com/a.pngx&quot; onmouseover=&quot;alert(1)&quot; data-x=&quot;" alt="">';
$this->assertSame($expectedRelative, img('a.png' . $payload));

$expectedIndexPage = '<img src="http://example.com/index.php/a.pngx%22%20onmouseover=%22alert%281%29%22%20data-x=%22" alt="">';
$this->assertSame($expectedIndexPage, img('a.png' . $payload, true));
}

public function testImgData(): void
{
$expected = 'data:image/gif;base64,' . $this->imgData;
Expand Down Expand Up @@ -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 = '<script src="https://example.com/a.js" data-x="x&quot; onmouseover=&quot;alert(1)&quot; data-x=&quot;" async></script>';
$this->assertSame($expectedArray, script_tag(['src' => 'https://example.com/a.js', 'data-x' => $payload, 'async' => null]));

$expectedRelative = '<script src="http://example.com/a.jsx&quot; onmouseover=&quot;alert(1)&quot; data-x=&quot;"></script>';
$this->assertSame($expectedRelative, script_tag('a.js' . $payload));

$expectedIndexPage = '<script src="http://example.com/index.php/a.jsx%22%20onmouseover=%22alert%281%29%22%20data-x=%22"></script>';
$this->assertSame($expectedIndexPage, script_tag('a.js' . $payload, true));
}

public function testLinkTag(): void
{
$target = 'css/mystyles.css';
Expand Down
Loading