Skip to content

fix(Helpers): escape attribute and option values in form helpers - #10818

Open
banghamid wants to merge 1 commit into
codeigniter4:developfrom
banghamid:fix/form-helper-escape-attributes
Open

banghamid wants to merge 1 commit into
codeigniter4:developfrom
banghamid:fix/form-helper-escape-attributes

Conversation

@banghamid

Copy link
Copy Markdown
Contributor

Description
Fixes #10812

In form_helper, parse_form_attributes() only escaped the value attribute, leaving all other associative array attributes (such as class, id, placeholder, data-*) unescaped. This contradicted the User Guide specification which states that form values passed as an associative array are automatically escaped.

Similarly, form_label() concatenated $id and $attributes raw, form_datalist() rendered datalist IDs and option values unescaped, and form_dropdown() concatenated optgroup labels unescaped.

This PR:

  • Escapes all non-boolean attribute values in parse_form_attributes().
  • Escapes $id and attribute values in form_label().
  • Escapes datalist ID and option values in form_datalist().
  • Escapes optgroup label in form_dropdown().
  • Adds regression tests in FormHelperTest.
  • Adds a changelog entry in docs/changelogs/v4.7.7.md.

Checklist:

  • Securely signed commits
  • Component-specific tests which fail without the change
  • Conforms to style guide and static analysis checks
  • User guide and changelog updated

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.
@carson-codeigniter4 carson-codeigniter4 Bot added the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 23, 2026

@rahul05ranjan rahul05ranjan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No PHP runtime here, so I verified the escaping semantics against the source instead of executing: esc() with its default 'html' context routes to Zend/Laminas Escaper::escapeHtml, which escapes < > & " ' (both quote forms — the new tests' expected output shows &quot; and &#039;, so single-quoted form_datalist attributes are covered too). That's adequate for every attribute context this PR touches, since the helpers always emit quoted attributes.

The fixes all close real attribute-injection paths, and the old parse_form_attributes() behavior explains the rest of the PR: only value was escaped, so the class="form-control" onfocus="alert(1)" payload from your own test sailed straight into the tag. Escaping every non-bool value there, plus the optgroup label, form_label()'s for and attribute values, and the datalist id/options, is consistent and complete for the value side. The name === '' skip and bool-attribute handling are correctly preserved.

Two observations, neither blocking:

  • Attribute names ($key) are still concatenated raw in parse_form_attributes() and form_label(), so a key like x" onfocus="alert(1) would still inject. That's developer-controlled data rather than user data in practice, and it's pre-existing — just noting it as a possible follow-up hardening.
  • form_label()'s $labelText also stays raw. I assume that's deliberate so callers can put markup in labels, and the changelog line correctly scopes this PR to attribute and option values — worth keeping that scope explicit in the changelog wording.

@banghamid

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review and for verifying the escaping semantics, @rahul05ranjan!

You're completely right on both points:

  1. Keeping $labelText raw is indeed deliberate so developers can continue including HTML markup in labels (such as <span class="text-danger">*</span>).
  2. Attribute keys ($key) are developer-controlled, so keeping key sanitization outside this scope makes sense and could be considered as a separate hardening task if needed.

Appreciate the feedback!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Verified issues on the current code behavior or pull requests that will fix them

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Form helpers don't escape attribute values other than value

2 participants