javascript - Sanitizing innerHTML while still executing innerHTML change? -


i'm creating alert plugin (pure javascript, no jquery) allow users input custom html alert boxes. however, xss attacks , dom attacks major concern because use .innerhtml achieve this. attempt @ input sanitation below. problem html tags included in output. there way prevent against these attacks while still performing requested innerhtml change , have no tags showing in output?

 <!--example-->   //html  <div id='div'></div>   //javascript  var name = '<h1>james</h1>';   function sanitizehtml(unsafestring) {    return unsafestring.replace(/&/g, '&amp;')   .replace(/</g, '&lt;')   .replace(/>/g, '&gt;')   .replace(/\"/g, '&quot;')   .replace(/\'/g, '&#39;')   .replace(/\//g, '&#x2f;'); };  document.getelementbyid('div').innerhtml = sanitizehtml(name);  //outputs '<h1>james</h1>' tags showing 


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -