Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sanitizing URL to prevent XSS in Rails

In an rails application, users can create events and post an url to link to the external event site.

How do I sanitize the urls to prevent XSS links?

Thanks in advance,

example of XSS, which is not preventable by rails' sanitize method

@url = "javascript:alert('XSS')"
<a href="<%=sanitize @url%>">test link</a>
like image 525
rickypai Avatar asked Mar 09 '12 13:03

rickypai


2 Answers

Try sanitizing once the href is already in a tag like:

url = "javascript:alert('XSS')"
sanitize link_to('xss link', url)

This gives me:

<a>xss link</a>
like image 110
Ben Avatar answered Oct 12 '22 01:10

Ben


You can use Rails' sanitize method for some basic restriction.

Or you can use rgrove's sanitize gem for more options.

Hope this helps.

like image 43
Phyo Wai Win Avatar answered Oct 12 '22 01:10

Phyo Wai Win