Skip to content

Commit fb4a935

Browse files
committed
fix(Helpers): escape attribute and option values in form helpers
Escape non-boolean attribute and option values in parse_form_attributes(), form_label(), form_datalist(), and form_dropdown() optgroups when passed as associative arrays to prevent attribute injection and match the documented contract.
1 parent 08039be commit fb4a935

3 files changed

Lines changed: 69 additions & 9 deletions

File tree

‎docs/changelogs/v4.7.7.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Release Date: Unreleased
3939
- **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.
4040
- **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`).
4141
- **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.
42+
- **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.
4243
- **Helpers:** Fixed escaping of attribute values in HTML media helpers to prevent malformed HTML and attribute injection.
4344
- **HTTP:** Fixed a bug where query-string based routing truncated additional query parameters in `SiteURIFactory::parseRequestURI()` due to incorrect array index access.
4445
- **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.

‎system/Helpers/form_helper.php‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ function form_dropdown($data = '', $options = [], $selected = [], $extra = ''):
323323
continue;
324324
}
325325

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

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

462462
if ($id !== '') {
463-
$label .= ' for="' . $id . '"';
463+
$label .= ' for="' . esc($id) . '"';
464464
}
465465

466466
foreach ($attributes as $key => $val) {
467-
$label .= ' ' . $key . '="' . $val . '"';
467+
$label .= ' ' . $key . '="' . esc($val) . '"';
468468
}
469469

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

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

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

497497
foreach ($options as $option) {
498-
$out .= "<option value='{$option}'>\n";
498+
$out .= "<option value='" . esc($option) . "'>\n";
499499
}
500500

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

787787
foreach ($default as $key => $val) {
788788
if (! is_bool($val)) {
789-
if ($key === 'value') {
790-
$val = esc($val);
791-
} elseif ($key === 'name' && $default['name'] === '') {
789+
if ($key === 'name' && $default['name'] === '') {
792790
continue;
793791
}
794-
$att .= $key . '="' . $val . '"' . ($key === array_key_last($default) ? '' : ' ');
792+
793+
$att .= $key . '="' . esc($val) . '"' . ($key === array_key_last($default) ? '' : ' ');
795794
} else {
796795
$att .= $key . ' ';
797796
}

‎tests/system/Helpers/FormHelperTest.php‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,20 @@ public function testFormInputWithExtra(): void
379379
$this->assertSame($expected, form_input($data, '', $extra));
380380
}
381381

382+
public function testFormInputEscapesAttributes(): void
383+
{
384+
$data = [
385+
'name' => 'username',
386+
'value' => 'johndoe',
387+
'class' => 'form-control" onfocus="alert(1)" data-x="',
388+
'placeholder' => 'Enter "name"',
389+
];
390+
$expected = <<<EOH
391+
<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
392+
EOH;
393+
$this->assertSame($expected, form_input($data));
394+
}
395+
382396
public function testFormPassword(): void
383397
{
384398
$expected = <<<EOH
@@ -666,6 +680,24 @@ public function testFormDropdownWithEmptyArrayOptionValue(): void
666680
$this->assertSame($expected, form_dropdown('foo', $options));
667681
}
668682

683+
public function testFormDropdownEscapesOptgroupLabel(): void
684+
{
685+
$options = [
686+
'Group "A"' => [
687+
'1' => 'One',
688+
],
689+
];
690+
$expected = <<<'EOH'
691+
<select name="shirts">
692+
<optgroup label="Group &quot;A&quot;">
693+
<option value="1">One</option>
694+
</optgroup>
695+
</select>
696+
697+
EOH;
698+
$this->assertSame($expected, form_dropdown('shirts', $options));
699+
}
700+
669701
public function testFormMultiselect(): void
670702
{
671703
$expected = <<<EOH
@@ -859,6 +891,17 @@ public function testFormLabelWithAttributes(): void
859891
$this->assertSame($expected, form_label('bar', 'foo', $attributes));
860892
}
861893

894+
public function testFormLabelEscapesAttributesAndId(): void
895+
{
896+
$attributes = [
897+
'class' => 'label" onmouseover="alert(1)" data-x="',
898+
];
899+
$expected = <<<'EOH'
900+
<label for="foo&quot;bar" class="label&quot; onmouseover=&quot;alert(1)&quot; data-x=&quot;">bar</label>
901+
EOH;
902+
$this->assertSame($expected, form_label('bar', 'foo"bar', $attributes));
903+
}
904+
862905
public function testFormReset(): void
863906
{
864907
$expected = <<<EOH
@@ -913,6 +956,23 @@ public function testFormDatalist(): void
913956
$this->assertSame($expected, form_datalist('foo', 'bar', $options));
914957
}
915958

959+
public function testFormDatalistEscapesOptionsAndName(): void
960+
{
961+
$options = [
962+
'foo"1',
963+
"bar'1",
964+
];
965+
$expected = <<<'EOH'
966+
<input type="text" name="foo&quot;bar" value="baz" list="foo&quot;bar_list">
967+
968+
<datalist id='foo&quot;bar_list'><option value='foo&quot;1'>
969+
<option value='bar&#039;1'>
970+
</datalist>
971+
972+
EOH;
973+
$this->assertSame($expected, form_datalist('foo"bar', 'baz', $options));
974+
}
975+
916976
public function testSetValue(): void
917977
{
918978
$_SESSION['_ci_old_input']['post']['foo'] = '<bar';

0 commit comments

Comments
 (0)