The first character on data attributes values becomes underscore when it is a number.


| Comment | File | Size | Author |
|---|---|---|---|
| #7 | after_xss.png | 108.61 KB | rakesh.regar |
| #7 | after_number_value.png | 99.64 KB | rakesh.regar |
| #7 | before_xss.png | 77.61 KB | rakesh.regar |
| #7 | before_number_value.png | 98.43 KB | rakesh.regar |
| Screenshot 2025-09-04 at 12.21.25 PM.png | 19.27 KB | mjgruta |
Issue fork block_class-3544750
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
kul.pratap commentedComment #4
kul.pratap commentedReplaced
Html::cleanCssIdentifier()withHtml::escape()to prevent numeric data-* attribute values from being prefixed with underscores.Please review.
Comment #5
renatog commentedComment #6
kul.pratap commentedPreviously code uses:
This method is designed to sanitize CSS identifiers (e.g., class names, IDs).
It ensures they are valid by replacing leading numbers and stripping disallowed characters. However, this is too strict when applied to
HTML attribute values (such as `data)', because:
Valid `data-` attribute values may begin with digits (e.g., `550`).
Sanitization with
cleanCssIdentifier()incorrectly rewrites these values (`550 → _550`).Context-appropriate sanitization:
- `Html::escape()` encodes special HTML characters (`<`, `>`, `"`, `'`, `&`).
- This prevents injection into the DOM or breaking out of the attribute context.
- Preserves valid values:
- Digits, letters, and safe symbols remain unchanged (`550` stays `550`).
Security Conclusion- `Html::cleanCssIdentifier()` is only necessary when generating CSS identifiers.
- For attribute values in HTML, `Html::escape()` provides the correct level of protection against XSS.
- Therefore, this change maintains security while restoring correct behavior for numeric and other valid attribute values.
Comment #7
rakesh.regarI have done the testing for this MR and changes are working as expected.
Comment #10
dydave commentedThanks a lot Kul (@kul.pratap) for the great explanation above at #6!
Super nice Security conclusion!
Great job on the merge request as well! 👍
Additionally, I found: https://drupal.stackexchange.com/a/207036
It does make sense to support various types of data other than CSS classes or IDs identifiers.
Since your comments and details made quite a lot of sense, I went ahead and merged the changes above at #9. 🥳
This might need adding tests, but maybe it should be the object of a different ticket.
Marking issue as Fixed for now.
Thanks again everyone!