Skip to content

Commit e587c2d

Browse files
committed
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.
1 parent 59e5eea commit e587c2d

2 files changed

Lines changed: 168 additions & 21 deletions

File tree

‎system/Helpers/html_helper.php‎

Lines changed: 31 additions & 21 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_attr($src) . '"';
302302
} elseif ($indexPage) {
303-
$video .= ' src="' . site_url($src) . '"';
303+
$video .= ' src="' . _esc_attr(site_url($src)) . '"';
304304
} else {
305-
$video .= ' src="' . slash_item('baseURL') . $src . '"';
305+
$video .= ' src="' . _esc_attr(slash_item('baseURL') . $src) . '"';
306306
}
307307

308308
if ($attributes !== '') {
@@ -317,7 +317,7 @@ function video($src, string $unsupportedMessage = '', string $attributes = '', a
317317

318318
if ($unsupportedMessage !== '') {
319319
$video .= _space_indent()
320-
. $unsupportedMessage
320+
. esc($unsupportedMessage, 'html')
321321
. "\n";
322322
}
323323

@@ -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_attr($src) . '"';
350350
} elseif ($indexPage) {
351-
$audio .= ' src="' . site_url($src) . '"';
351+
$audio .= ' src="' . _esc_attr(site_url($src)) . '"';
352352
} else {
353-
$audio .= ' src="' . slash_item('baseURL') . $src . '"';
353+
$audio .= ' src="' . _esc_attr(slash_item('baseURL') . $src) . '"';
354354
}
355355

356356
if ($attributes !== '') {
@@ -364,7 +364,7 @@ function audio($src, string $unsupportedMessage = '', string $attributes = '', a
364364
}
365365

366366
if ($unsupportedMessage !== '') {
367-
$audio .= "\n" . _space_indent() . $unsupportedMessage . "\n";
367+
$audio .= "\n" . _space_indent() . esc($unsupportedMessage, 'html') . "\n";
368368
}
369369

370370
return $audio . "</audio>\n";
@@ -400,7 +400,7 @@ function _media(string $name, array $types = [], string $unsupportedMessage = ''
400400
}
401401

402402
if ($unsupportedMessage !== '') {
403-
$media .= _space_indent() . $unsupportedMessage . "\n";
403+
$media .= _space_indent() . esc($unsupportedMessage, 'html') . "\n";
404404
}
405405

406406
return $media . ('</' . $name . ">\n");
@@ -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_attr($src)
429+
. '" type="' . _esc_attr($type) . '"';
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_attr($src)
454+
. '" kind="' . _esc_attr($kind)
455+
. '" srclang="' . _esc_attr($srcLanguage)
456+
. '" label="' . _esc_attr($label)
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_attr($data) . '" '
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_attr($name)
511+
. '" type="' . _esc_attr($type)
512+
. '" value="' . _esc_attr($value)
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_attr($src)
535+
. '" type="' . _esc_attr($type) . '" '
536536
. $attributes . _solidus() . ">\n";
537537
}
538538
}
@@ -556,3 +556,13 @@ function _space_indent(int $depth = 2): string
556556
return str_repeat(' ', $depth);
557557
}
558558
}
559+
560+
if (! function_exists('_esc_attr')) {
561+
/**
562+
* Escapes a value for use inside a double-quoted HTML attribute.
563+
*/
564+
function _esc_attr(string $value): string
565+
{
566+
return htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
567+
}
568+
}

‎tests/system/Helpers/HTMLHelperTest.php‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,4 +890,141 @@ 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 = '<script>alert(1)</script>';
898+
899+
$html = video($src, $message, 'controls');
900+
901+
$this->assertStringNotContainsString('onerror="alert(1)', $html);
902+
$this->assertStringNotContainsString('<script>', $html);
903+
$this->assertStringContainsString('src="' . _esc_attr($src) . '"', $html);
904+
$this->assertStringContainsString('&lt;script&gt;alert(1)&lt;/script&gt;', $html);
905+
906+
$relative = video('test.mp4" onerror="alert(1)');
907+
$this->assertStringNotContainsString('onerror="alert(1)', $relative);
908+
909+
$withArray = video([source('test.mp4', 'video/mp4')], '<script>alert(1)</script>');
910+
$this->assertStringNotContainsString('<script>', $withArray);
911+
$this->assertStringContainsString('&lt;script&gt;alert(1)&lt;/script&gt;', $withArray);
912+
}
913+
914+
public function testAudioEscaping(): void
915+
{
916+
$src = 'http://example.com/test.mp3" onerror="alert(1)';
917+
$message = '<script>alert(1)</script>';
918+
919+
$html = audio($src, $message, 'controls');
920+
921+
$this->assertStringNotContainsString('onerror="alert(1)', $html);
922+
$this->assertStringNotContainsString('<script>', $html);
923+
$this->assertStringContainsString('src="' . _esc_attr($src) . '"', $html);
924+
$this->assertStringContainsString('&lt;script&gt;alert(1)&lt;/script&gt;', $html);
925+
926+
$relative = audio('test.mp3" onerror="alert(1)');
927+
$this->assertStringNotContainsString('onerror="alert(1)', $relative);
928+
}
929+
930+
public function testMediaEscaping(): void
931+
{
932+
$sources = [
933+
source('http://example.com/test.mp4" onerror="alert(1)', 'video/mp4" onload="alert(1)'),
934+
];
935+
$message = '<script>alert(1)</script>';
936+
937+
$html = _media('video', $sources, $message);
938+
939+
$this->assertStringNotContainsString('<script>', $html);
940+
$this->assertStringContainsString('&lt;script&gt;alert(1)&lt;/script&gt;', $html);
941+
$this->assertStringNotContainsString('onerror="alert(1)', $html);
942+
}
943+
944+
public function testSourceEscaping(): void
945+
{
946+
$src = 'http://example.com/test.mp4" onerror="alert(1)';
947+
$type = 'video/mp4" onload="alert(1)';
948+
949+
$html = source($src, $type);
950+
951+
$this->assertStringNotContainsString('onerror="alert(1)', $html);
952+
$this->assertStringNotContainsString('onload="alert(1)', $html);
953+
$this->assertStringContainsString('src="' . _esc_attr($src) . '"', $html);
954+
$this->assertStringContainsString('type="' . _esc_attr($type) . '"', $html);
955+
$this->assertSame('<source src="http://example.com/test.mp4&quot; onerror=&quot;alert(1)" type="video/mp4&quot; onload=&quot;alert(1)">', $html);
956+
}
957+
958+
public function testTrackEscaping(): void
959+
{
960+
$src = 'subtitles.vtt" onerror="alert(1)';
961+
$kind = 'subtitles" onfocus="alert(1)';
962+
$srcLanguage = 'en" onclick="alert(1)';
963+
$label = 'English" onmouseover="alert(1)';
964+
965+
$html = track($src, $kind, $srcLanguage, $label);
966+
967+
$this->assertStringNotContainsString('onerror="alert(1)', $html);
968+
$this->assertStringNotContainsString('onfocus="alert(1)', $html);
969+
$this->assertStringNotContainsString('onclick="alert(1)', $html);
970+
$this->assertStringNotContainsString('onmouseover="alert(1)', $html);
971+
$this->assertStringContainsString('src="' . _esc_attr($src) . '"', $html);
972+
$this->assertStringContainsString('kind="' . _esc_attr($kind) . '"', $html);
973+
$this->assertStringContainsString('srclang="' . _esc_attr($srcLanguage) . '"', $html);
974+
$this->assertStringContainsString('label="' . _esc_attr($label) . '"', $html);
975+
}
976+
977+
public function testObjectEscaping(): void
978+
{
979+
$data = 'http://example.com/movie.swf" onerror="alert(1)';
980+
981+
$html = object($data);
982+
983+
$this->assertStringNotContainsString('onerror="alert(1)', $html);
984+
$this->assertStringContainsString('data="' . _esc_attr($data) . '"', $html);
985+
}
986+
987+
public function testParamEscaping(): void
988+
{
989+
$name = 'foo" onclick="alert(1)';
990+
$value = 'bar" onerror="alert(1)';
991+
$type = 'ref" onfocus="alert(1)';
992+
993+
$html = param($name, $value, $type);
994+
995+
$this->assertStringNotContainsString('onclick="alert(1)', $html);
996+
$this->assertStringNotContainsString('onerror="alert(1)', $html);
997+
$this->assertStringNotContainsString('onfocus="alert(1)', $html);
998+
$this->assertStringContainsString('name="' . _esc_attr($name) . '"', $html);
999+
$this->assertStringContainsString('value="' . _esc_attr($value) . '"', $html);
1000+
$this->assertStringContainsString('type="' . _esc_attr($type) . '"', $html);
1001+
}
1002+
1003+
public function testEmbedEscaping(): void
1004+
{
1005+
$src = 'http://example.com/movie.mov" onerror="alert(1)';
1006+
$type = 'video/quicktime" onload="alert(1)';
1007+
1008+
$html = embed($src, $type);
1009+
1010+
$this->assertStringNotContainsString('onerror="alert(1)', $html);
1011+
$this->assertStringNotContainsString('onload="alert(1)', $html);
1012+
$this->assertStringContainsString('src="' . _esc_attr($src) . '"', $html);
1013+
$this->assertStringContainsString('type="' . _esc_attr($type) . '"', $html);
1014+
}
1015+
1016+
public function testEscAttrFunctionLiteralOutput(): void
1017+
{
1018+
$this->assertSame(
1019+
'http://example.com/test.mp4?a=1&amp;b=2',
1020+
_esc_attr('http://example.com/test.mp4?a=1&b=2'),
1021+
);
1022+
$this->assertSame(
1023+
'foo&quot;bar&#039;&lt;baz&gt;',
1024+
_esc_attr('foo"bar\'<baz>'),
1025+
);
1026+
1027+
// ENT_SUBSTITUTE replaces invalid UTF-8 instead of returning an empty string.
1028+
$this->assertSame("\u{FFFD}", _esc_attr("\xFF"));
1029+
}
8931030
}

0 commit comments

Comments
 (0)