Skip to content

Commit 835a8c2

Browse files
committed
trigger ci
1 parent 0358156 commit 835a8c2

4 files changed

Lines changed: 47 additions & 72 deletions

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Release Date: Unreleased
2727
- **Database:** Fixed a bug where `SQLite3\Connection::_foreignKeyData()` did not escape table identifiers in `PRAGMA foreign_key_list`, causing syntax errors on table names containing hyphens or schemas.
2828
- **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.
2929
- **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`.
30+
- **Helpers:** Fixed a bug where HTML media helper functions did not escape attribute values, allowing attribute injection and XSS.
3031
- **HTTP:** Fixed a bug where query-string based routing truncated additional query parameters in `SiteURIFactory::parseRequestURI()` due to incorrect array index access.
3132
- **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.
3233
- **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]
404+
> Attribute values generated by media helper functions (`src`, `type`, `kind`, `srclang`, `label`, `name`, `value`, `data`) are automatically escaped. The `$unsupportedMessage` parameter in `video()` and `audio()` accepts raw HTML (such as fallback links).
405+
403406
#### audio($src[, $unsupportedMessage = ''[, $attributes = ''[, $tracks = [][, $indexPage = false]]]])
404407

405408
- **Parameters**

‎system/Helpers/html_helper.php‎

Lines changed: 21 additions & 31 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="' . _esc_attr($src) . '"';
301+
$video .= ' src="' . esc($src, 'html') . '"';
302302
} elseif ($indexPage) {
303-
$video .= ' src="' . _esc_attr(site_url($src)) . '"';
303+
$video .= ' src="' . esc(site_url($src), 'html') . '"';
304304
} else {
305-
$video .= ' src="' . _esc_attr(slash_item('baseURL') . $src) . '"';
305+
$video .= ' src="' . esc(slash_item('baseURL') . $src, 'html') . '"';
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-
. esc($unsupportedMessage, 'html')
320+
. $unsupportedMessage
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="' . _esc_attr($src) . '"';
349+
$audio .= ' src="' . esc($src, 'html') . '"';
350350
} elseif ($indexPage) {
351-
$audio .= ' src="' . _esc_attr(site_url($src)) . '"';
351+
$audio .= ' src="' . esc(site_url($src), 'html') . '"';
352352
} else {
353-
$audio .= ' src="' . _esc_attr(slash_item('baseURL') . $src) . '"';
353+
$audio .= ' src="' . esc(slash_item('baseURL') . $src, 'html') . '"';
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() . esc($unsupportedMessage, 'html') . "\n";
367+
$audio .= "\n" . _space_indent() . $unsupportedMessage . "\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() . esc($unsupportedMessage, 'html') . "\n";
403+
$media .= _space_indent() . $unsupportedMessage . "\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="' . _esc_attr($src)
429-
. '" type="' . _esc_attr($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="' . _esc_attr($src)
454-
. '" kind="' . _esc_attr($kind)
455-
. '" srclang="' . _esc_attr($srcLanguage)
456-
. '" label="' . _esc_attr($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="' . _esc_attr($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="' . _esc_attr($name)
511-
. '" type="' . _esc_attr($type)
512-
. '" value="' . _esc_attr($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="' . _esc_attr($src)
535-
. '" type="' . _esc_attr($type) . '" '
534+
return '<embed src="' . esc($src, 'html')
535+
. '" type="' . esc($type, 'html') . '" '
536536
. $attributes . _solidus() . ">\n";
537537
}
538538
}
@@ -556,13 +556,3 @@ 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: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -894,34 +894,31 @@ public function testObjectIndexed(): void
894894
public function testVideoEscaping(): void
895895
{
896896
$src = 'http://example.com/test.mp4" onerror="alert(1)';
897-
$message = '<script>alert(1)</script>';
897+
$message = '<a href="movie.mp4">Download</a>';
898898

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

901901
$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);
902+
$this->assertStringContainsString('src="' . esc($src, 'html') . '"', $html);
903+
$this->assertStringContainsString('<a href="movie.mp4">Download</a>', $html);
905904

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

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);
908+
$withArray = video([source('test.mp4', 'video/mp4')], '<a href="movie.mp4">Download</a>');
909+
$this->assertStringContainsString('<a href="movie.mp4">Download</a>', $withArray);
912910
}
913911

914912
public function testAudioEscaping(): void
915913
{
916914
$src = 'http://example.com/test.mp3" onerror="alert(1)';
917-
$message = '<script>alert(1)</script>';
915+
$message = '<a href="movie.mp4">Download</a>';
918916

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

921919
$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);
920+
$this->assertStringContainsString('src="' . esc($src, 'html') . '"', $html);
921+
$this->assertStringContainsString('<a href="movie.mp4">Download</a>', $html);
925922

926923
$relative = audio('test.mp3" onerror="alert(1)');
927924
$this->assertStringNotContainsString('onerror="alert(1)', $relative);
@@ -932,12 +929,11 @@ public function testMediaEscaping(): void
932929
$sources = [
933930
source('http://example.com/test.mp4" onerror="alert(1)', 'video/mp4" onload="alert(1)'),
934931
];
935-
$message = '<script>alert(1)</script>';
932+
$message = '<a href="movie.mp4">Download</a>';
936933

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

939-
$this->assertStringNotContainsString('<script>', $html);
940-
$this->assertStringContainsString('&lt;script&gt;alert(1)&lt;/script&gt;', $html);
936+
$this->assertStringContainsString('<a href="movie.mp4">Download</a>', $html);
941937
$this->assertStringNotContainsString('onerror="alert(1)', $html);
942938
}
943939

@@ -950,8 +946,8 @@ public function testSourceEscaping(): void
950946

951947
$this->assertStringNotContainsString('onerror="alert(1)', $html);
952948
$this->assertStringNotContainsString('onload="alert(1)', $html);
953-
$this->assertStringContainsString('src="' . _esc_attr($src) . '"', $html);
954-
$this->assertStringContainsString('type="' . _esc_attr($type) . '"', $html);
949+
$this->assertStringContainsString('src="' . esc($src, 'html') . '"', $html);
950+
$this->assertStringContainsString('type="' . esc($type, 'html') . '"', $html);
955951
$this->assertSame('<source src="http://example.com/test.mp4&quot; onerror=&quot;alert(1)" type="video/mp4&quot; onload=&quot;alert(1)">', $html);
956952
}
957953

@@ -968,10 +964,10 @@ public function testTrackEscaping(): void
968964
$this->assertStringNotContainsString('onfocus="alert(1)', $html);
969965
$this->assertStringNotContainsString('onclick="alert(1)', $html);
970966
$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);
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);
975971
}
976972

977973
public function testObjectEscaping(): void
@@ -981,7 +977,7 @@ public function testObjectEscaping(): void
981977
$html = object($data);
982978

983979
$this->assertStringNotContainsString('onerror="alert(1)', $html);
984-
$this->assertStringContainsString('data="' . _esc_attr($data) . '"', $html);
980+
$this->assertStringContainsString('data="' . esc($data, 'html') . '"', $html);
985981
}
986982

987983
public function testParamEscaping(): void
@@ -995,9 +991,9 @@ public function testParamEscaping(): void
995991
$this->assertStringNotContainsString('onclick="alert(1)', $html);
996992
$this->assertStringNotContainsString('onerror="alert(1)', $html);
997993
$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);
994+
$this->assertStringContainsString('name="' . esc($name, 'html') . '"', $html);
995+
$this->assertStringContainsString('value="' . esc($value, 'html') . '"', $html);
996+
$this->assertStringContainsString('type="' . esc($type, 'html') . '"', $html);
1001997
}
1002998

1003999
public function testEmbedEscaping(): void
@@ -1009,22 +1005,7 @@ public function testEmbedEscaping(): void
10091005

10101006
$this->assertStringNotContainsString('onerror="alert(1)', $html);
10111007
$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"));
1008+
$this->assertStringContainsString('src="' . esc($src, 'html') . '"', $html);
1009+
$this->assertStringContainsString('type="' . esc($type, 'html') . '"', $html);
10291010
}
10301011
}

0 commit comments

Comments
 (0)