Repository navigation
Conversation
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.
rahul05ranjan
left a comment
There was a problem hiding this comment.
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 " and ', 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 inparse_form_attributes()andform_label(), so a key likex" 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$labelTextalso 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.
|
Thanks for the thorough review and for verifying the escaping semantics, @rahul05ranjan! You're completely right on both points:
Appreciate the feedback! |
Description
Fixes #10812
In
form_helper,parse_form_attributes()only escaped thevalueattribute, leaving all other associative array attributes (such asclass,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$idand$attributesraw,form_datalist()rendered datalist IDs and option values unescaped, andform_dropdown()concatenated optgroup labels unescaped.This PR:
parse_form_attributes().$idand attribute values inform_label().form_datalist().form_dropdown().FormHelperTest.docs/changelogs/v4.7.7.md.Checklist: