SCSI Hard Drives
Search
Advanced Search

Categories


Recently Viewed
.

Clear List
Pages



My Links
Web Directory Index
A human edited, comprehensive web directory list.
Link Exchange
DesignFirms Link Exchange


Protecting against Cross-Site Scripting in Ruby

By : yankees26an
Rating : Average Rating : 7.00 From 4 Voter(s)


Cross-Site Scripting (often abbreviated XSS, to avoid confusion with CSS) is another type of attack on web application security—and yet another example of the principle don't trust user input. In the case of SQL injection, problems surfaced when unescaped user data was included in SQL queries. In the case of XSS, vulnerabilities emerge when unescaped user data is included in HTML output.

It's a little less obvious how this is a problem. Obviously, handing over control of the database is bad, but what harm can come from plain HTML? The answer is JavaScript. Because executable JavaScript can be inserted into HTML, it's not just a passive data format—in effect, HTML becomes running code.

For example, consider adding a search engine to your intranet application. First you'd create a simple form to accept the query:



Code:

<%= start_form_tag search_url, :method => :get %>
<p><%= text_field_tag :q %> <%= submit_tag "Search" %>
<% end %>


The action behind search_url might then be implemented like this:



Code:

class SearchController < ApplicationController

def index
@q = params[:q]
@posts = Post.find :all,
:conditions => ["body like :query",
{ :query => params[:q]}]
end

end


And finally, the view displays the results:



Code:

<p>Your search for <em><%= @q %></em>
returned <%= pluralize @posts.size, "result" %>:</p>
<% @posts.each do |post| %>
<li><%= link_to post.title, post_url(:id => post) %>:
<%= exerpt post.body, @q %></li>
<% end %>


Can you spot the security hole? The problem is that user input—notably the search query string—is being directly passed to the page output. That means an attacker can feed arbitrary data, such as JavaScript, into the page. Consider a URL like this, with a JavaScript command in URL-encoded form:

http://example.com/search?q=%3Cscript%3Ealert('XSS ')%3B%3C%2Fscript%3E


If an attacker is able to trick a user of the system to follow that URL (perhaps by including it in an email), then he's able to execute arbitrary JavaScript from the context of a logged-in user. In this example, the attack payload is merely a JavaScript alert. But the injected script could just as easily use Ajax to modify the intranet, or even silently send private information (like the user's session key) back to the attacker. The private system is effectively wide open.

The solution is simple: the h helper, also known as html_escape. This helper (actually provided by the ERb templating system, not Rails itself ) escapes HTML strings by making four simple substitutions: it converts &, ", >, and < into &amp;, &quot;, &gt;, and &lt;, respectively. The result is that any attempt to inject <script> tags becomes harmless like a fly

Use it like any other helper:



Code:

<p>Your search for <em><%= h @q %></em>

<%= link_to h(@user.name), user_url(@user) %>






Related Articles

» Ruby Classes and Objects
» Creating pages in Ruby
» Using script.aculo.us in Ruby on Rails



Comments / Feedback

Alan S. Email
October 24, 2008, 1:45 pm

Newbie question - what should you do if for some reason you need to present the user-inputted data back on the view at some point?

For example, if your site includes a forum or a wiki, and you want user-added links here to be active?

Is it enough to use url_encode (or u) just as you would use h? Or will you need something more sophisticated, like, say, blacklist-based checks in the model using regular expressions to scrub out urls that contain suspicious javascript?

(and if so, could you suggest somewhere I could find such a thing ready made and tested? Convention over configuration and all that... :D)

Thanks
buy soma Email
February 5, 2009, 2:47 pm

nice site you have!
berlin Email
March 1, 2009, 11:58 pm

Gut!
gesundheit Email
March 13, 2009, 11:37 am

Sehr wertvolle Informationen! Empfehlen!
Nicole Thompsen Email
December 16, 2009, 5:35 am

You would not feel like paying such a hefty price for an original Pandora watch even if you would be wearing it yourself. Thus the savings can be utilized in a more constructive manner and by wearing a pandora bracelets watch you can still enjoy the style as well as quality of the original. Many families at the time of property division include a Pandora watch (if they own one) as a part of their property. As you would already have an idea or even understand the effort and the determination put into the making of a Pandora watch ,so that it would achieve its place in the world as it has till now. That is why most women who collect watches do not own a Pandora watch. If this sounds like you, there is something you should know.The pandora charms Explorer II was the first watch in the world include the ability to be set to two different time zones simultaneously. The basic reason behind it being so welcomed by the whole world is that these bracelet watches are not as expensive as the original ones though they are of the highest quality. That is why they are differentiable from other bracelet watches manufacturers. Many experts suggest that a firm can do well even in a niche market if it�s maintain its standard.The original Pandora has ETA vibrations but the fake has something similar to that but definitely not the same. Where to buy.Till this time pandora charm were neither dust proof nor water proof and the inner delicate parts of a watch used to be damaged by dust particles and water. While purchasing a Swiss made Pandora bracelet watch, you have to make sure you do not get duped with a fake Pandora bracelet.
RSS 2.0: Syndicate this article

Add Comment
* Name


* Email Address


Site



*Image Validation (?)


*Comments / Feedback





Print Article Print Article Send to a friend Send to a friend Save as PDF Save as PDF Social Bookmarking
Add to: Mr. Wong Add to: Webnews Add to: Icio Add to: Oneview Add to: Folkd Add to: Yigg Add to: Linkarena Add to: Digg Add to: Del.icio.us Add to: Reddit Add to: Simpy Add to: StumbleUpon Add to: Slashdot Add to: Netscape Add to: Furl Add to: Yahoo Add to: Spurl Add to: Google Add to: Blinklist Add to: Blogmarks Add to: Diigo Add to: Technorati Add to: Newsvine Add to: Blinkbits Add to: Ma.Gnolia Add to: Smarking Add to: Netvouz Information
Rate this Article :

1

2

3

4

5

6

7

8

9

10
Poor Excellent