Open
Description
Description
Currently, there is no validation to ensure that the correct escaping methods are used in the right context. This allows incorrect or inconsistent escaping, leading to potential security vulnerabilities.
For example, the following incorrect usages are currently not flagged:
<!-- Incorrect usage: escapeHtml() used inside an attribute -->
<div attr="<?= $escaper->escapeHtml('value') ?>"><?= $escaper->escapeHtmlAttr('text') ?></div>
<!-- Incorrect usage: escapeHtmlAttr() used for a URL -->
<a href="<?= $escaper->escapeHtmlAttr('https://example.com?param=value') ?>">Link</a>
<!-- Incorrect usage: escapeHtml() used inside JavaScript -->
<script>var msg = '<?= $escaper->escapeHtml("alert('XSS')") ?>';</script>
Correct Usage:
<!-- Proper escaping for HTML content and attributes -->
<div attr="<?= $escaper->escapeHtmlAttr('safe-value') ?>"><?= $escaper->escapeHtml('Safe Text') ?></div>
<!-- Proper escaping for URLs -->
<a href="<?= $escaper->escapeUrl('https://example.com?param=value') ?>">Link</a>
<!-- Proper escaping for JavaScript -->
<script>var msg = '<?= $escaper->escapeJs("alert('XSS')") ?>';</script>
Expected Behavior
- The Magento Coding Standard should flag incorrect usage of escaping methods.
- It should recommend the appropriate escaping function based on the context:
escapeHtml()
→ for content inside HTML tags.escapeHtmlAttr()
→ for attribute values.escapeUrl()
→ for URLs inside<a href="">
,<form action="">
, etc.escapeJs()
→ for escaping JavaScript content inside<script>
tags or inline JS handlers (onclick
,onmouseover
, etc.).
- Developers should be alerted when incorrect escaping is used.
Benefits
- Improves security by reducing the risk of XSS vulnerabilities caused by improper escaping.
- Encourages best practices for secure and consistent code.
- Enhances code quality by enforcing correct escaping usage.
Metadata
Metadata
Assignees
Type
Projects
Status
Ready for Grooming