Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnecessary use of innerHTML #5115

Closed
ManuGowda opened this issue Jun 26, 2023 · 0 comments
Closed

Unnecessary use of innerHTML #5115

ManuGowda opened this issue Jun 26, 2023 · 0 comments

Comments

@ManuGowda
Copy link
Contributor

ManuGowda commented Jun 26, 2023

Expected behavior

Modify the code to use .innerText instead of .innerHTML . This will accomplish the same without the risk of XSS.

Review every other HTML sink, and, where they are really necessary, add a comment explaining why, and why unsanitized attacker input cannot reach it.

Actual behavior

The code unnecessarily uses the .innerHTML function to append simple text to the DOM, as shown in figure 59.1. This function allows users to modify the HTML of an element; however, in this case, this is unnecessary because the data-name attribute of icon elements is always simple text without HTML tags. Using .innerText is sufficient.

Steps to reproduce

<script>
(function () {
document.getElementById('icons').onclick = function (e) {
e = e || window.event;
var name =
e.target.getAttribute('data-name') || e.target.parentNode.getAttribute('data-name');
document.getElementById('name').innerHTML = name;
};
})();
</script>

Reference

https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/innerText
https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development

No branches or pull requests

2 participants