Skip to content

Commit b15eadf

Browse files
authored
fix(Helpers): escape attribute values in HTML media helpers (#10765)
* fix(Helpers): escape attributes and unsupported messages in HTML media tags The `video()`, `audio()`, `_media()`, `source()`, `track()`, `object()`, `param()` and `embed()` helpers concatenated their arguments straight into double-quoted HTML attributes. A value containing a double quote - a `src`, `type`, `kind`, `label` or `name` that comes from a database column or a request parameter, for instance - could therefore close the attribute and inject arbitrary ones, such as `onerror`, into the generated markup. Attribute values now go through a new `_esc_attr()` helper that applies `htmlspecialchars()` with `ENT_QUOTES | ENT_SUBSTITUTE`, so both quote styles, `<`, `>` and `&` are encoded while ordinary URLs stay readable. `ENT_SUBSTITUTE` keeps invalid UTF-8 from turning the whole value into an empty string. The `$unsupportedMessage` fallback text of `video()`, `audio()` and `_media()` was emitted verbatim as well, so markup passed as the fallback message was rendered as HTML. It is now escaped with `esc($message, 'html')` and displayed as plain text. Regression tests cover each affected helper, including the alternative `ENT_SUBSTITUTE` behaviour. * trigger ci * docs: use an MkDocs admonition for media helper escaping GitHub-only alert `> [!NOTE]` does not render in MkDocs; use the admonition syntax from contributing/documentation.md, and document that `$unsupportedMessage`, `$attributes`, `$tracks` and `$params` are inserted as raw HTML. * test: assert media helper attributes are escaped once Cover `&` encoding through `video()` and through `video()` -> `source()` -> `_media()`, so a missing or a duplicated `esc()` fails the test. * docs: align changelog wording for media helper escaping
1 parent b95b9c7 commit b15eadf

4 files changed

Lines changed: 154 additions & 18 deletions

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Release Date: Unreleased
3232
- **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.
3333
- **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`.
3434
- **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.
35+
- **Helpers:** Fixed escaping of attribute values in HTML media helpers to prevent malformed HTML and attribute injection.
3536
- **HTTP:** Fixed a bug where query-string based routing truncated additional query parameters in `SiteURIFactory::parseRequestURI()` due to incorrect array index access.
3637
- **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.
3738
- **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.

‎docs/helpers/html_helper.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,9 @@ The above code will produce this:
400400
</video>
401401
```
402402

403+
!!! note "Note"
404+
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.
405+
403406
#### audio($src[, $unsupportedMessage = ''[, $attributes = ''[, $tracks = [][, $indexPage = false]]]])
404407

405408
- **Parameters**

‎system/Helpers/html_helper.php‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,11 @@ function video($src, string $unsupportedMessage = '', string $attributes = '', a
298298
$video = '<video';
299299

300300
if (_has_protocol($src)) {
301-
$video .= ' src="' . $src . '"';
301+
$video .= ' src="' . esc($src, 'html') . '"';
302302
} elseif ($indexPage) {
303-
$video .= ' src="' . site_url($src) . '"';
303+
$video .= ' src="' . esc(site_url($src), 'html') . '"';
304304
} else {
305-
$video .= ' src="' . slash_item('baseURL') . $src . '"';
305+
$video .= ' src="' . esc(slash_item('baseURL') . $src, 'html') . '"';
306306
}
307307

308308
if ($attributes !== '') {
@@ -346,11 +346,11 @@ function audio($src, string $unsupportedMessage = '', string $attributes = '', a
346346
$audio = '<audio';
347347

348348
if (_has_protocol($src)) {
349-
$audio .= ' src="' . $src . '"';
349+
$audio .= ' src="' . esc($src, 'html') . '"';
350350
} elseif ($indexPage) {
351-
$audio .= ' src="' . site_url($src) . '"';
351+
$audio .= ' src="' . esc(site_url($src), 'html') . '"';
352352
} else {
353-
$audio .= ' src="' . slash_item('baseURL') . $src . '"';
353+
$audio .= ' src="' . esc(slash_item('baseURL') . $src, 'html') . '"';
354354
}
355355

356356
if ($attributes !== '') {
@@ -425,8 +425,8 @@ function source(string $src, string $type = 'unknown', string $attributes = '',
425425
$src = $indexPage ? site_url($src) : slash_item('baseURL') . $src;
426426
}
427427

428-
$source = '<source src="' . $src
429-
. '" type="' . $type . '"';
428+
$source = '<source src="' . esc($src, 'html')
429+
. '" type="' . esc($type, 'html') . '"';
430430

431431
if ($attributes !== '') {
432432
$source .= ' ' . $attributes;
@@ -450,10 +450,10 @@ function source(string $src, string $type = 'unknown', string $attributes = '',
450450
*/
451451
function track(string $src, string $kind, string $srcLanguage, string $label): string
452452
{
453-
return '<track src="' . $src
454-
. '" kind="' . $kind
455-
. '" srclang="' . $srcLanguage
456-
. '" label="' . $label
453+
return '<track src="' . esc($src, 'html')
454+
. '" kind="' . esc($kind, 'html')
455+
. '" srclang="' . esc($srcLanguage, 'html')
456+
. '" label="' . esc($label, 'html')
457457
. '"' . _solidus() . '>';
458458
}
459459
}
@@ -478,7 +478,7 @@ function object(string $data, string $type = 'unknown', string $attributes = '',
478478
$data = $indexPage ? site_url($data) : slash_item('baseURL') . $data;
479479
}
480480

481-
$object = '<object data="' . $data . '" '
481+
$object = '<object data="' . esc($data, 'html') . '" '
482482
. $attributes . '>';
483483

484484
if ($params !== []) {
@@ -507,9 +507,9 @@ function object(string $data, string $type = 'unknown', string $attributes = '',
507507
*/
508508
function param(string $name, string $value, string $type = 'ref', string $attributes = ''): string
509509
{
510-
return '<param name="' . $name
511-
. '" type="' . $type
512-
. '" value="' . $value
510+
return '<param name="' . esc($name, 'html')
511+
. '" type="' . esc($type, 'html')
512+
. '" value="' . esc($value, 'html')
513513
. '" ' . $attributes . _solidus() . '>';
514514
}
515515
}
@@ -531,8 +531,8 @@ function embed(string $src, string $type = 'unknown', string $attributes = '', b
531531
$src = $indexPage ? site_url($src) : slash_item('baseURL') . $src;
532532
}
533533

534-
return '<embed src="' . $src
535-
. '" type="' . $type . '" '
534+
return '<embed src="' . esc($src, 'html')
535+
. '" type="' . esc($type, 'html') . '" '
536536
. $attributes . _solidus() . ">\n";
537537
}
538538
}

‎tests/system/Helpers/HTMLHelperTest.php‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,4 +890,136 @@ public function testObjectIndexed(): void
890890

891891
$this->assertSame($expected, $object);
892892
}
893+
894+
public function testVideoEscaping(): void
895+
{
896+
$src = 'http://example.com/test.mp4" onerror="alert(1)';
897+
$message = '<a href="movie.mp4">Download</a>';
898+
899+
$html = video($src, $message, 'controls');
900+
901+
$this->assertStringNotContainsString('onerror="alert(1)', $html);
902+
$this->assertStringContainsString('src="' . esc($src, 'html') . '"', $html);
903+
$this->assertStringContainsString('<a href="movie.mp4">Download</a>', $html);
904+
905+
$relative = video('test.mp4" onerror="alert(1)');
906+
$this->assertStringNotContainsString('onerror="alert(1)', $relative);
907+
908+
$withArray = video([source('test.mp4', 'video/mp4')], '<a href="movie.mp4">Download</a>');
909+
$this->assertStringContainsString('<a href="movie.mp4">Download</a>', $withArray);
910+
}
911+
912+
public function testAudioEscaping(): void
913+
{
914+
$src = 'http://example.com/test.mp3" onerror="alert(1)';
915+
$message = '<a href="movie.mp4">Download</a>';
916+
917+
$html = audio($src, $message, 'controls');
918+
919+
$this->assertStringNotContainsString('onerror="alert(1)', $html);
920+
$this->assertStringContainsString('src="' . esc($src, 'html') . '"', $html);
921+
$this->assertStringContainsString('<a href="movie.mp4">Download</a>', $html);
922+
923+
$relative = audio('test.mp3" onerror="alert(1)');
924+
$this->assertStringNotContainsString('onerror="alert(1)', $relative);
925+
}
926+
927+
public function testMediaEscaping(): void
928+
{
929+
$sources = [
930+
source('http://example.com/test.mp4" onerror="alert(1)', 'video/mp4" onload="alert(1)'),
931+
];
932+
$message = '<a href="movie.mp4">Download</a>';
933+
934+
$html = _media('video', $sources, $message);
935+
936+
$this->assertStringContainsString('<a href="movie.mp4">Download</a>', $html);
937+
$this->assertStringNotContainsString('onerror="alert(1)', $html);
938+
}
939+
940+
public function testSourceEscaping(): void
941+
{
942+
$src = 'http://example.com/test.mp4" onerror="alert(1)';
943+
$type = 'video/mp4" onload="alert(1)';
944+
945+
$html = source($src, $type);
946+
947+
$this->assertStringNotContainsString('onerror="alert(1)', $html);
948+
$this->assertStringNotContainsString('onload="alert(1)', $html);
949+
$this->assertStringContainsString('src="' . esc($src, 'html') . '"', $html);
950+
$this->assertStringContainsString('type="' . esc($type, 'html') . '"', $html);
951+
$this->assertSame('<source src="http://example.com/test.mp4&quot; onerror=&quot;alert(1)" type="video/mp4&quot; onload=&quot;alert(1)">', $html);
952+
}
953+
954+
public function testTrackEscaping(): void
955+
{
956+
$src = 'subtitles.vtt" onerror="alert(1)';
957+
$kind = 'subtitles" onfocus="alert(1)';
958+
$srcLanguage = 'en" onclick="alert(1)';
959+
$label = 'English" onmouseover="alert(1)';
960+
961+
$html = track($src, $kind, $srcLanguage, $label);
962+
963+
$this->assertStringNotContainsString('onerror="alert(1)', $html);
964+
$this->assertStringNotContainsString('onfocus="alert(1)', $html);
965+
$this->assertStringNotContainsString('onclick="alert(1)', $html);
966+
$this->assertStringNotContainsString('onmouseover="alert(1)', $html);
967+
$this->assertStringContainsString('src="' . esc($src, 'html') . '"', $html);
968+
$this->assertStringContainsString('kind="' . esc($kind, 'html') . '"', $html);
969+
$this->assertStringContainsString('srclang="' . esc($srcLanguage, 'html') . '"', $html);
970+
$this->assertStringContainsString('label="' . esc($label, 'html') . '"', $html);
971+
}
972+
973+
public function testObjectEscaping(): void
974+
{
975+
$data = 'http://example.com/movie.swf" onerror="alert(1)';
976+
977+
$html = object($data);
978+
979+
$this->assertStringNotContainsString('onerror="alert(1)', $html);
980+
$this->assertStringContainsString('data="' . esc($data, 'html') . '"', $html);
981+
}
982+
983+
public function testParamEscaping(): void
984+
{
985+
$name = 'foo" onclick="alert(1)';
986+
$value = 'bar" onerror="alert(1)';
987+
$type = 'ref" onfocus="alert(1)';
988+
989+
$html = param($name, $value, $type);
990+
991+
$this->assertStringNotContainsString('onclick="alert(1)', $html);
992+
$this->assertStringNotContainsString('onerror="alert(1)', $html);
993+
$this->assertStringNotContainsString('onfocus="alert(1)', $html);
994+
$this->assertStringContainsString('name="' . esc($name, 'html') . '"', $html);
995+
$this->assertStringContainsString('value="' . esc($value, 'html') . '"', $html);
996+
$this->assertStringContainsString('type="' . esc($type, 'html') . '"', $html);
997+
}
998+
999+
public function testEmbedEscaping(): void
1000+
{
1001+
$src = 'http://example.com/movie.mov" onerror="alert(1)';
1002+
$type = 'video/quicktime" onload="alert(1)';
1003+
1004+
$html = embed($src, $type);
1005+
1006+
$this->assertStringNotContainsString('onerror="alert(1)', $html);
1007+
$this->assertStringNotContainsString('onload="alert(1)', $html);
1008+
$this->assertStringContainsString('src="' . esc($src, 'html') . '"', $html);
1009+
$this->assertStringContainsString('type="' . esc($type, 'html') . '"', $html);
1010+
}
1011+
1012+
public function testMediaEscapingEncodesAmpersandOnce(): void
1013+
{
1014+
$src = 'http://example.com/test.mp4?a=1&b=2';
1015+
$escaped = 'src="http://example.com/test.mp4?a=1&amp;b=2"';
1016+
1017+
$html = video($src);
1018+
$this->assertStringContainsString($escaped, $html);
1019+
$this->assertStringNotContainsString('&amp;amp;', $html);
1020+
1021+
$media = _media('video', [source($src, 'video/mp4')]);
1022+
$this->assertStringContainsString($escaped, $media);
1023+
$this->assertStringNotContainsString('&amp;amp;', $media);
1024+
}
8931025
}

0 commit comments

Comments
 (0)