From fb4a935c1261ec17feed3db1334693e1fa1ff8b3 Mon Sep 17 00:00:00 2001 From: banghamid <62824084+banghamid@users.noreply.github.com> Date: Mon, 23 Nov 2026 19:22:33 +0700 Subject: [PATCH] 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. --- docs/changelogs/v4.7.7.md | 1 + system/Helpers/form_helper.php | 17 ++++--- tests/system/Helpers/FormHelperTest.php | 60 +++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 9 deletions(-) diff --git a/docs/changelogs/v4.7.7.md b/docs/changelogs/v4.7.7.md index 2d4d421a770e..d0686f448851 100644 --- a/docs/changelogs/v4.7.7.md +++ b/docs/changelogs/v4.7.7.md @@ -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. diff --git a/system/Helpers/form_helper.php b/system/Helpers/form_helper.php index 19cc8eb949b9..704ea7e00f18 100644 --- a/system/Helpers/form_helper.php +++ b/system/Helpers/form_helper.php @@ -323,7 +323,7 @@ function form_dropdown($data = '', $options = [], $selected = [], $extra = ''): continue; } - $form .= '\n"; + $form .= '\n"; foreach ($val as $optgroupKey => $optgroupVal) { // Keys should always be strings for strict comparison @@ -460,11 +460,11 @@ function form_label(string $labelText = '', string $id = '', array $attributes = $label = ' $val) { - $label .= ' ' . $key . '="' . $val . '"'; + $label .= ' ' . $key . '="' . esc($val) . '"'; } return $label . '>' . $labelText . ''; @@ -492,10 +492,10 @@ function form_datalist(string $name, string $value, array $options): string $out = form_input($data) . "\n"; - $out .= ""; + $out .= ""; foreach ($options as $option) { - $out .= "\n"); @@ -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 . ' '; } diff --git a/tests/system/Helpers/FormHelperTest.php b/tests/system/Helpers/FormHelperTest.php index bfd3d12e0efe..5765592fc572 100644 --- a/tests/system/Helpers/FormHelperTest.php +++ b/tests/system/Helpers/FormHelperTest.php @@ -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 = <<\n + EOH; + $this->assertSame($expected, form_input($data)); + } + public function testFormPassword(): void { $expected = <<assertSame($expected, form_dropdown('foo', $options)); } + public function testFormDropdownEscapesOptgroupLabel(): void + { + $options = [ + 'Group "A"' => [ + '1' => 'One', + ], + ]; + $expected = <<<'EOH' + + + EOH; + $this->assertSame($expected, form_dropdown('shirts', $options)); + } + public function testFormMultiselect(): void { $expected = <<assertSame($expected, form_label('bar', 'foo', $attributes)); } + public function testFormLabelEscapesAttributesAndId(): void + { + $attributes = [ + 'class' => 'label" onmouseover="alert(1)" data-x="', + ]; + $expected = <<<'EOH' + + EOH; + $this->assertSame($expected, form_label('bar', 'foo"bar', $attributes)); + } + public function testFormReset(): void { $expected = <<assertSame($expected, form_datalist('foo', 'bar', $options)); } + public function testFormDatalistEscapesOptionsAndName(): void + { + $options = [ + 'foo"1', + "bar'1", + ]; + $expected = <<<'EOH' + + + + + EOH; + $this->assertSame($expected, form_datalist('foo"bar', 'baz', $options)); + } + public function testSetValue(): void { $_SESSION['_ci_old_input']['post']['foo'] = '