diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index 7c9c49545e73..ca8963553aba 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -28,6 +28,7 @@ Release Date: Unreleased - **Database:** Fixed a bug where `SQLite3\Table::dropIndexes()` used single-quoted string literals instead of escaped identifiers in `DROP INDEX IF EXISTS`, causing syntax errors on index names containing quotes or special characters. - **Helpers:** Fixed a bug where `excerpt()` threw a `TypeError` when `$phrase` was not found in `$text` or when `$phrase` was `null` and `$radius` was an odd integer. When `$phrase` is not found, `excerpt()` now falls back to extracting an excerpt from the beginning of `$text`. - **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 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. - **Images:** Fixed a bug in `ImageMagickHandler::save()` where saving without an extension or with an unsupported extension threw an `ImagickException` instead of falling back to the image's format. diff --git a/docs/helpers/html_helper.md b/docs/helpers/html_helper.md index e8fabed6d860..ae05a912dc9b 100644 --- a/docs/helpers/html_helper.md +++ b/docs/helpers/html_helper.md @@ -400,6 +400,9 @@ The above code will produce this: ``` +!!! note "Note" + Attribute values generated by media helper functions (`src`, `type`, `kind`, `srclang`, `label`, `name`, `value`, `data`) are automatically escaped. The `$unsupportedMessage`, `$attributes`, `$tracks` and `$params` parameters are inserted as raw HTML: they accept trusted markup, such as fallback links or attribute snippets, and must not contain unescaped user input. + #### audio($src[, $unsupportedMessage = ''[, $attributes = ''[, $tracks = [][, $indexPage = false]]]]) - **Parameters** diff --git a/system/Helpers/html_helper.php b/system/Helpers/html_helper.php index 67441f935c11..ccfdb0d832a9 100644 --- a/system/Helpers/html_helper.php +++ b/system/Helpers/html_helper.php @@ -298,11 +298,11 @@ function video($src, string $unsupportedMessage = '', string $attributes = '', a $video = ''; } } @@ -478,7 +478,7 @@ function object(string $data, string $type = 'unknown', string $attributes = '', $data = $indexPage ? site_url($data) : slash_item('baseURL') . $data; } - $object = ''; if ($params !== []) { @@ -507,9 +507,9 @@ function object(string $data, string $type = 'unknown', string $attributes = '', */ function param(string $name, string $value, string $type = 'ref', string $attributes = ''): string { - return ''; } } @@ -531,8 +531,8 @@ function embed(string $src, string $type = 'unknown', string $attributes = '', b $src = $indexPage ? site_url($src) : slash_item('baseURL') . $src; } - return '\n"; } } diff --git a/tests/system/Helpers/HTMLHelperTest.php b/tests/system/Helpers/HTMLHelperTest.php index 787b100bbf2b..ab6c4508d8f6 100644 --- a/tests/system/Helpers/HTMLHelperTest.php +++ b/tests/system/Helpers/HTMLHelperTest.php @@ -890,4 +890,136 @@ public function testObjectIndexed(): void $this->assertSame($expected, $object); } + + public function testVideoEscaping(): void + { + $src = 'http://example.com/test.mp4" onerror="alert(1)'; + $message = 'Download'; + + $html = video($src, $message, 'controls'); + + $this->assertStringNotContainsString('onerror="alert(1)', $html); + $this->assertStringContainsString('src="' . esc($src, 'html') . '"', $html); + $this->assertStringContainsString('Download', $html); + + $relative = video('test.mp4" onerror="alert(1)'); + $this->assertStringNotContainsString('onerror="alert(1)', $relative); + + $withArray = video([source('test.mp4', 'video/mp4')], 'Download'); + $this->assertStringContainsString('Download', $withArray); + } + + public function testAudioEscaping(): void + { + $src = 'http://example.com/test.mp3" onerror="alert(1)'; + $message = 'Download'; + + $html = audio($src, $message, 'controls'); + + $this->assertStringNotContainsString('onerror="alert(1)', $html); + $this->assertStringContainsString('src="' . esc($src, 'html') . '"', $html); + $this->assertStringContainsString('Download', $html); + + $relative = audio('test.mp3" onerror="alert(1)'); + $this->assertStringNotContainsString('onerror="alert(1)', $relative); + } + + public function testMediaEscaping(): void + { + $sources = [ + source('http://example.com/test.mp4" onerror="alert(1)', 'video/mp4" onload="alert(1)'), + ]; + $message = 'Download'; + + $html = _media('video', $sources, $message); + + $this->assertStringContainsString('Download', $html); + $this->assertStringNotContainsString('onerror="alert(1)', $html); + } + + public function testSourceEscaping(): void + { + $src = 'http://example.com/test.mp4" onerror="alert(1)'; + $type = 'video/mp4" onload="alert(1)'; + + $html = source($src, $type); + + $this->assertStringNotContainsString('onerror="alert(1)', $html); + $this->assertStringNotContainsString('onload="alert(1)', $html); + $this->assertStringContainsString('src="' . esc($src, 'html') . '"', $html); + $this->assertStringContainsString('type="' . esc($type, 'html') . '"', $html); + $this->assertSame('', $html); + } + + public function testTrackEscaping(): void + { + $src = 'subtitles.vtt" onerror="alert(1)'; + $kind = 'subtitles" onfocus="alert(1)'; + $srcLanguage = 'en" onclick="alert(1)'; + $label = 'English" onmouseover="alert(1)'; + + $html = track($src, $kind, $srcLanguage, $label); + + $this->assertStringNotContainsString('onerror="alert(1)', $html); + $this->assertStringNotContainsString('onfocus="alert(1)', $html); + $this->assertStringNotContainsString('onclick="alert(1)', $html); + $this->assertStringNotContainsString('onmouseover="alert(1)', $html); + $this->assertStringContainsString('src="' . esc($src, 'html') . '"', $html); + $this->assertStringContainsString('kind="' . esc($kind, 'html') . '"', $html); + $this->assertStringContainsString('srclang="' . esc($srcLanguage, 'html') . '"', $html); + $this->assertStringContainsString('label="' . esc($label, 'html') . '"', $html); + } + + public function testObjectEscaping(): void + { + $data = 'http://example.com/movie.swf" onerror="alert(1)'; + + $html = object($data); + + $this->assertStringNotContainsString('onerror="alert(1)', $html); + $this->assertStringContainsString('data="' . esc($data, 'html') . '"', $html); + } + + public function testParamEscaping(): void + { + $name = 'foo" onclick="alert(1)'; + $value = 'bar" onerror="alert(1)'; + $type = 'ref" onfocus="alert(1)'; + + $html = param($name, $value, $type); + + $this->assertStringNotContainsString('onclick="alert(1)', $html); + $this->assertStringNotContainsString('onerror="alert(1)', $html); + $this->assertStringNotContainsString('onfocus="alert(1)', $html); + $this->assertStringContainsString('name="' . esc($name, 'html') . '"', $html); + $this->assertStringContainsString('value="' . esc($value, 'html') . '"', $html); + $this->assertStringContainsString('type="' . esc($type, 'html') . '"', $html); + } + + public function testEmbedEscaping(): void + { + $src = 'http://example.com/movie.mov" onerror="alert(1)'; + $type = 'video/quicktime" onload="alert(1)'; + + $html = embed($src, $type); + + $this->assertStringNotContainsString('onerror="alert(1)', $html); + $this->assertStringNotContainsString('onload="alert(1)', $html); + $this->assertStringContainsString('src="' . esc($src, 'html') . '"', $html); + $this->assertStringContainsString('type="' . esc($type, 'html') . '"', $html); + } + + public function testMediaEscapingEncodesAmpersandOnce(): void + { + $src = 'http://example.com/test.mp4?a=1&b=2'; + $escaped = 'src="http://example.com/test.mp4?a=1&b=2"'; + + $html = video($src); + $this->assertStringContainsString($escaped, $html); + $this->assertStringNotContainsString('&', $html); + + $media = _media('video', [source($src, 'video/mp4')]); + $this->assertStringContainsString($escaped, $media); + $this->assertStringNotContainsString('&', $media); + } }