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 @@ -39,6 +39,7 @@ Release Date: Unreleased
- **Helpers:** Fixed a bug where `parse_subdomain()` incorrectly treated hostnames with ports (such as IP addresses and two-part TLD domains) as having unexpected subdomains.
- **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 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
17 changes: 8 additions & 9 deletions system/Helpers/form_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ function form_dropdown($data = '', $options = [], $selected = [], $extra = ''):
continue;
}

$form .= '<optgroup label="' . $key . "\">\n";
$form .= '<optgroup label="' . esc($key) . "\">\n";

foreach ($val as $optgroupKey => $optgroupVal) {
// Keys should always be strings for strict comparison
Expand Down Expand Up @@ -460,11 +460,11 @@ function form_label(string $labelText = '', string $id = '', array $attributes =
$label = '<label';

if ($id !== '') {
$label .= ' for="' . $id . '"';
$label .= ' for="' . esc($id) . '"';
}

foreach ($attributes as $key => $val) {
$label .= ' ' . $key . '="' . $val . '"';
$label .= ' ' . $key . '="' . esc($val) . '"';
}

return $label . '>' . $labelText . '</label>';
Expand Down Expand Up @@ -492,10 +492,10 @@ function form_datalist(string $name, string $value, array $options): string

$out = form_input($data) . "\n";

$out .= "<datalist id='" . $name . "_list'>";
$out .= "<datalist id='" . esc($name . '_list') . "'>";

foreach ($options as $option) {
$out .= "<option value='{$option}'>\n";
$out .= "<option value='" . esc($option) . "'>\n";
}

return $out . ("</datalist>\n");
Expand Down Expand Up @@ -786,12 +786,11 @@ function parse_form_attributes($attributes, array $default): string

foreach ($default as $key => $val) {
if (! is_bool($val)) {
if ($key === 'value') {
$val = esc($val);
} elseif ($key === 'name' && $default['name'] === '') {
if ($key === 'name' && $default['name'] === '') {
continue;
}
$att .= $key . '="' . $val . '"' . ($key === array_key_last($default) ? '' : ' ');

$att .= $key . '="' . esc($val) . '"' . ($key === array_key_last($default) ? '' : ' ');
} else {
$att .= $key . ' ';
}
Expand Down
60 changes: 60 additions & 0 deletions tests/system/Helpers/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,20 @@ public function testFormInputWithExtra(): void
$this->assertSame($expected, form_input($data, '', $extra));
}

public function testFormInputEscapesAttributes(): void
{
$data = [
'name' => 'username',
'value' => 'johndoe',
'class' => 'form-control" onfocus="alert(1)" data-x="',
'placeholder' => 'Enter "name"',
];
$expected = <<<EOH
<input type="text" name="username" value="johndoe" class="form-control&quot; onfocus=&quot;alert(1)&quot; data-x=&quot;" placeholder="Enter &quot;name&quot;">\n
EOH;
$this->assertSame($expected, form_input($data));
}

public function testFormPassword(): void
{
$expected = <<<EOH
Expand Down Expand Up @@ -666,6 +680,24 @@ public function testFormDropdownWithEmptyArrayOptionValue(): void
$this->assertSame($expected, form_dropdown('foo', $options));
}

public function testFormDropdownEscapesOptgroupLabel(): void
{
$options = [
'Group "A"' => [
'1' => 'One',
],
];
$expected = <<<'EOH'
<select name="shirts">
<optgroup label="Group &quot;A&quot;">
<option value="1">One</option>
</optgroup>
</select>

EOH;
$this->assertSame($expected, form_dropdown('shirts', $options));
}

public function testFormMultiselect(): void
{
$expected = <<<EOH
Expand Down Expand Up @@ -859,6 +891,17 @@ public function testFormLabelWithAttributes(): void
$this->assertSame($expected, form_label('bar', 'foo', $attributes));
}

public function testFormLabelEscapesAttributesAndId(): void
{
$attributes = [
'class' => 'label" onmouseover="alert(1)" data-x="',
];
$expected = <<<'EOH'
<label for="foo&quot;bar" class="label&quot; onmouseover=&quot;alert(1)&quot; data-x=&quot;">bar</label>
EOH;
$this->assertSame($expected, form_label('bar', 'foo"bar', $attributes));
}

public function testFormReset(): void
{
$expected = <<<EOH
Expand Down Expand Up @@ -913,6 +956,23 @@ public function testFormDatalist(): void
$this->assertSame($expected, form_datalist('foo', 'bar', $options));
}

public function testFormDatalistEscapesOptionsAndName(): void
{
$options = [
'foo"1',
"bar'1",
];
$expected = <<<'EOH'
<input type="text" name="foo&quot;bar" value="baz" list="foo&quot;bar_list">

<datalist id='foo&quot;bar_list'><option value='foo&quot;1'>
<option value='bar&#039;1'>
</datalist>

EOH;
$this->assertSame($expected, form_datalist('foo"bar', 'baz', $options));
}

public function testSetValue(): void
{
$_SESSION['_ci_old_input']['post']['foo'] = '<bar';
Expand Down
Loading