Skip to content
Merged
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 @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions docs/helpers/html_helper.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,9 @@ The above code will produce this:
</video>
```

!!! 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**
Expand Down
36 changes: 18 additions & 18 deletions system/Helpers/html_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,11 @@ function video($src, string $unsupportedMessage = '', string $attributes = '', a
$video = '<video';

if (_has_protocol($src)) {
$video .= ' src="' . $src . '"';
$video .= ' src="' . esc($src, 'html') . '"';
} elseif ($indexPage) {
$video .= ' src="' . site_url($src) . '"';
$video .= ' src="' . esc(site_url($src), 'html') . '"';
} else {
$video .= ' src="' . slash_item('baseURL') . $src . '"';
$video .= ' src="' . esc(slash_item('baseURL') . $src, 'html') . '"';
}

if ($attributes !== '') {
Expand Down Expand Up @@ -346,11 +346,11 @@ function audio($src, string $unsupportedMessage = '', string $attributes = '', a
$audio = '<audio';

if (_has_protocol($src)) {
$audio .= ' src="' . $src . '"';
$audio .= ' src="' . esc($src, 'html') . '"';
} elseif ($indexPage) {
$audio .= ' src="' . site_url($src) . '"';
$audio .= ' src="' . esc(site_url($src), 'html') . '"';
} else {
$audio .= ' src="' . slash_item('baseURL') . $src . '"';
$audio .= ' src="' . esc(slash_item('baseURL') . $src, 'html') . '"';
}

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

$source = '<source src="' . $src
. '" type="' . $type . '"';
$source = '<source src="' . esc($src, 'html')
. '" type="' . esc($type, 'html') . '"';

if ($attributes !== '') {
$source .= ' ' . $attributes;
Expand All @@ -450,10 +450,10 @@ function source(string $src, string $type = 'unknown', string $attributes = '',
*/
function track(string $src, string $kind, string $srcLanguage, string $label): string
{
return '<track src="' . $src
. '" kind="' . $kind
. '" srclang="' . $srcLanguage
. '" label="' . $label
return '<track src="' . esc($src, 'html')
. '" kind="' . esc($kind, 'html')
. '" srclang="' . esc($srcLanguage, 'html')
. '" label="' . esc($label, 'html')
. '"' . _solidus() . '>';
}
}
Expand All @@ -478,7 +478,7 @@ function object(string $data, string $type = 'unknown', string $attributes = '',
$data = $indexPage ? site_url($data) : slash_item('baseURL') . $data;
}

$object = '<object data="' . $data . '" '
$object = '<object data="' . esc($data, 'html') . '" '
. $attributes . '>';

if ($params !== []) {
Expand Down Expand Up @@ -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 '<param name="' . $name
. '" type="' . $type
. '" value="' . $value
return '<param name="' . esc($name, 'html')
. '" type="' . esc($type, 'html')
. '" value="' . esc($value, 'html')
. '" ' . $attributes . _solidus() . '>';
}
}
Expand All @@ -531,8 +531,8 @@ function embed(string $src, string $type = 'unknown', string $attributes = '', b
$src = $indexPage ? site_url($src) : slash_item('baseURL') . $src;
}

return '<embed src="' . $src
. '" type="' . $type . '" '
return '<embed src="' . esc($src, 'html')
. '" type="' . esc($type, 'html') . '" '
. $attributes . _solidus() . ">\n";
}
}
Expand Down
132 changes: 132 additions & 0 deletions tests/system/Helpers/HTMLHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<a href="movie.mp4">Download</a>';

$html = video($src, $message, 'controls');

$this->assertStringNotContainsString('onerror="alert(1)', $html);
$this->assertStringContainsString('src="' . esc($src, 'html') . '"', $html);
$this->assertStringContainsString('<a href="movie.mp4">Download</a>', $html);

$relative = video('test.mp4" onerror="alert(1)');
$this->assertStringNotContainsString('onerror="alert(1)', $relative);

$withArray = video([source('test.mp4', 'video/mp4')], '<a href="movie.mp4">Download</a>');
$this->assertStringContainsString('<a href="movie.mp4">Download</a>', $withArray);
}

public function testAudioEscaping(): void
{
$src = 'http://example.com/test.mp3" onerror="alert(1)';
$message = '<a href="movie.mp4">Download</a>';

$html = audio($src, $message, 'controls');

$this->assertStringNotContainsString('onerror="alert(1)', $html);
$this->assertStringContainsString('src="' . esc($src, 'html') . '"', $html);
$this->assertStringContainsString('<a href="movie.mp4">Download</a>', $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 = '<a href="movie.mp4">Download</a>';

$html = _media('video', $sources, $message);

$this->assertStringContainsString('<a href="movie.mp4">Download</a>', $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('<source src="http://example.com/test.mp4&quot; onerror=&quot;alert(1)" type="video/mp4&quot; onload=&quot;alert(1)">', $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&amp;b=2"';

$html = video($src);
$this->assertStringContainsString($escaped, $html);
$this->assertStringNotContainsString('&amp;amp;', $html);

$media = _media('video', [source($src, 'video/mp4')]);
$this->assertStringContainsString($escaped, $media);
$this->assertStringNotContainsString('&amp;amp;', $media);
}
}
Loading