Safe vs Unsafe jQuery Methods
We all know XSS is bad and we all use jQuery, unfortunately its API doc doesn't do a very good job explaining what jQuery methods are safe to use for unescaped input, and which are not.
Safe
.text()-
.attr()// still needs to be careful when used in anhref .prop().val()
Unsafe
$("html code").html().append*().insert*().prepend*().wrap*().before().after()
Written by Jimmy Yuen Ho Wong
Related protips
4 Responses
I wouldn't consider .attr as a safe method. Example: .attr('href', 'javascript:alert()')
over 1 year ago
·
Good call!
over 1 year ago
·
Superb post, thank you for this!
over 1 year ago
·
Can you please show an example of the XSS safe DOM insertion. Would it be:
jQuery('<div/>', {
id: 'foo',
href: 'http://google.com',
title: 'Become a Googler',
rel: 'external',
text: 'Go to Google!'
}).appendTo('#mySelector');
?
Thank you
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Jquery
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#