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, '&') .replace(/</g, '<') .replace(/>/g, '>') .replace(/\"/g, '"') .replace(/\'/g, ''') .replace(/\//g, '/'); }; document.getelementbyid('div').innerhtml = sanitizehtml(name); //outputs '<h1>james</h1>' tags showing
Comments
Post a Comment