Skip to content
Open
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 @@ -45,6 +45,7 @@ Release Date: Unreleased
- **Helpers:** Fixed a bug in `plural()` where the regex for `matrix`, `vertex`, and `index` lacked non-capturing grouping and an end-of-string anchor for the `ix` alternative, causing incorrect pluralization of words like `complex`, `sex`, and strings containing `matrix` before the end. Expanded `plural()` and `singular()` to support additional words ending in `ix` or `ex` (`apex`, `appendix`, `codex`, `cortex`, `latex`, `murex`, `vortex`).
- **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 a bug where form helpers (`parse_form_attributes()`, `form_label()`, `form_datalist()`, and optgroups in `form_dropdown()`) did not escape attribute and option values other than `value` when passed as associative arrays.
- **Helpers:** Fixed a bug where `img()` and `script_tag()` did not escape `src` and attribute values when passed as strings or associative arrays. See [HTML Helper Escaping in `img()` and `script_tag()`](../installation/upgrade_477.md#html-helper-escaping-in-img-and-script_tag) for behavior changes.
- **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.
Expand Down
6 changes: 6 additions & 0 deletions docs/helpers/html_helper.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ img($imageProperties);
// <img src="http://site.com/index.php/images/picture.jpg" alt="Me, demonstrating how to eat 4 slices of pizza at one time" class="post_images" width="200" height="200" title="That was quite a night" rel="lightbox">
```

!!! note "Note"
Since v4.7.7, `img()` automatically escapes `src` and attribute values from the `$src` array using `esc()` (in addition to array/object `$attributes`). Do not pass values that are already HTML-escaped, as they will be escaped again. String `$attributes` are still inserted as raw HTML.

#### img_data($path[, $mime = null])

- **Parameters**
Expand Down Expand Up @@ -189,6 +192,9 @@ echo script_tag($script);
// <script src="http://site.com/js/printer.js" defer></script>
```

!!! note "Note"
Since v4.7.7, `script_tag()` automatically escapes `src` and non-`null` attribute values using `esc()`. Do not pass values that are already HTML-escaped, as they will be escaped again.

#### ul($list[, $attributes = ''])

- **Parameters**
Expand Down
11 changes: 11 additions & 0 deletions docs/installation/upgrade_477.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ With the default configuration most applications are unaffected, because `/`, `\

## Breaking Enhancements

## Behavior Changes

### HTML Helper Escaping in `img()` and `script_tag()`

`img()` and `script_tag()` now escape `src` and attribute values using `esc()`:

- `img()` escapes `src` (including relative URIs) and attribute values from the `$src` array, matching `stringify_attributes()` used for `$attributes`.
- `script_tag()` escapes `src` and non-`null` attribute values from `$src`.

Previously, `img()` did not escape relative `src` strings or values in the `$src` array, and `script_tag()` did not escape `src` or any attribute values. Applications that passed already HTML-escaped values (for example, pre-escaped with `esc()` or `htmlspecialchars()`) will now see them double-escaped (such as `&amp;` becoming `&amp;amp;`). Pass raw, unescaped values instead so the helpers escape them once.

## Project Files

Some files in the **project space** (root, app, public, writable) received updates. Due to these files being outside of the **system** scope they will not be changed without your intervention.
Expand Down
12 changes: 6 additions & 6 deletions system/Helpers/html_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,17 @@ function img($src = '', bool $indexPage = false, $attributes = ''): string
// Check for a relative URI
if (preg_match('#^([a-z]+:)?//#i', $src['src']) !== 1 && ! str_starts_with($src['src'], 'data:')) {
if ($indexPage) {
$img .= ' src="' . site_url($src['src']) . '"';
$img .= ' src="' . esc(site_url($src['src'])) . '"';
} else {
$img .= ' src="' . slash_item('baseURL') . $src['src'] . '"';
$img .= ' src="' . esc(slash_item('baseURL') . $src['src']) . '"';
}

unset($src['src']);
}

// Append any other values
foreach ($src as $key => $value) {
$img .= ' ' . $key . '="' . $value . '"';
$img .= ' ' . $key . '="' . esc($value) . '"';
}

// Prevent passing completed values to stringify_attributes
Expand Down Expand Up @@ -204,13 +204,13 @@ function script_tag($src = '', bool $indexPage = false): string
foreach ($src as $k => $v) {
if ($k === 'src' && preg_match('#^([a-z]+:)?//#i', $v) !== 1) {
if ($indexPage) {
$script .= 'src="' . site_url($v) . '" ';
$script .= 'src="' . esc(site_url($v)) . '" ';
} else {
$script .= 'src="' . slash_item('baseURL') . $v . '" ';
$script .= 'src="' . esc(slash_item('baseURL') . $v) . '" ';
}
} else {
// for attributes without values, like async or defer, use NULL.
$script .= $k . (null === $v ? ' ' : '="' . $v . '" ');
$script .= $k . (null === $v ? ' ' : '="' . esc($v) . '" ');
}
}

Expand Down
40 changes: 40 additions & 0 deletions tests/system/Helpers/HTMLHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,26 @@ public function testIMGXHTMLWithIndexpage(): void
$this->enableHtml5();
}

public function testImgEscapesAttributesAndSrc(): void
{
$payload = 'x" onmouseover="alert(1)" data-x="';

$expectedArray = '<img src="https://example.com/a.png" alt="x&quot; onmouseover=&quot;alert(1)&quot; data-x=&quot;">';
$this->assertSame($expectedArray, img(['src' => 'https://example.com/a.png', 'alt' => $payload]));

$expectedAbsolute = '<img src="https://example.com/a.pngx&quot; onmouseover=&quot;alert(1)&quot; data-x=&quot;" alt="">';
$this->assertSame($expectedAbsolute, img('https://example.com/a.png' . $payload));

$expectedAbsoluteArray = '<img src="https://example.com/a.png?a=1&amp;b=2" alt="x&quot; onmouseover=&quot;alert(1)&quot; data-x=&quot;">';
$this->assertSame($expectedAbsoluteArray, img(['src' => 'https://example.com/a.png?a=1&b=2', 'alt' => $payload]));

$expectedRelative = '<img src="http://example.com/a.pngx&quot; onmouseover=&quot;alert(1)&quot; data-x=&quot;" alt="">';
$this->assertSame($expectedRelative, img('a.png' . $payload));

$expectedIndexPage = '<img src="http://example.com/index.php/a.pngx%22%20onmouseover=%22alert%281%29%22%20data-x=%22" alt="">';
$this->assertSame($expectedIndexPage, img('a.png' . $payload, true));
}

public function testImgData(): void
{
$expected = 'data:image/gif;base64,' . $this->imgData;
Expand Down Expand Up @@ -359,6 +379,26 @@ public function testScriptTagWithoutAnyArg(): void
$this->assertSame($expected, script_tag());
}

public function testScriptTagEscapesAttributesAndSrc(): void
{
$payload = 'x" onmouseover="alert(1)" data-x="';

$expectedArray = '<script src="https://example.com/a.js" data-x="x&quot; onmouseover=&quot;alert(1)&quot; data-x=&quot;" async></script>';
$this->assertSame($expectedArray, script_tag(['src' => 'https://example.com/a.js', 'data-x' => $payload, 'async' => null]));

$expectedAbsolute = '<script src="https://example.com/a.jsx&quot; onmouseover=&quot;alert(1)&quot; data-x=&quot;"></script>';
$this->assertSame($expectedAbsolute, script_tag('https://example.com/a.js' . $payload));

$expectedAbsoluteArray = '<script src="https://example.com/a.js?a=1&amp;b=2" data-x="x&quot; onmouseover=&quot;alert(1)&quot; data-x=&quot;"></script>';
$this->assertSame($expectedAbsoluteArray, script_tag(['src' => 'https://example.com/a.js?a=1&b=2', 'data-x' => $payload]));

$expectedRelative = '<script src="http://example.com/a.jsx&quot; onmouseover=&quot;alert(1)&quot; data-x=&quot;"></script>';
$this->assertSame($expectedRelative, script_tag('a.js' . $payload));

$expectedIndexPage = '<script src="http://example.com/index.php/a.jsx%22%20onmouseover=%22alert%281%29%22%20data-x=%22"></script>';
$this->assertSame($expectedIndexPage, script_tag('a.js' . $payload, true));
}

public function testLinkTag(): void
{
$target = 'css/mystyles.css';
Expand Down
Loading