javascript - Prevent script injection with innerHTML -
i have written micro-templating utility uses innerhtml inject html fragments in web page, based on user input (either plain text strings or html strings).
my main concern risk of malicious script injection. script injected via script tag, or in inline event (img onload, div onmouseover example).
is there way sanitize html string prevent such injections? also, there other script injection methods should aware of?
if want safe, you'll sanitize templates both on client , server. don't write own anti-xss library malicious users bound know exploit haven't accounted for; there many nuances , unless you're xss expert, you're bound miss one.
on client side, google caja has pretty nice html sanitization utility perform robust sanitization on html strings, cleaning malicious attributes or other areas nasty users can nasty things, xss attacks via injecting script tags. scrub attributes , kinds of other xss injection points (object , applet tags, instance), can feel safe.
while should sanitize on server prevent malicious users disabling javascript or overwriting caja's sanitizer, can use caja sanitize both input , output try catch as can.
Comments
Post a Comment