It depends on what you do with the variable name
. If you are going to eval it, than the XSS is possible.
Supportingly, If the user input is alert(1)
and you are going to eval it without curing its value. i.e.
eval(name);
Or if you are going to inject the name
into the DOM the XSS is possible as well.
Have a look at the below example.
const first = 'Wes';
const User_input = `I love to do evil <img src="http://unsplash.it/100/100?random" onload="alert('you got hacked');" />`;
const html = `
<h3>${first}</h3>
<p>${User_input}</p>
`;
const bio = document.body;
bio.innerHTML = html;
But if you are properly sanitizing the user_input you can reduce the chances to XSS attack.
There are ways to sanitize the user_input. How are you sanitizing? Can you Show?