<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Stubbleblog &#187; xss</title>
	<atom:link href="http://www.stubbleblog.com/index.php/tag/xss/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.stubbleblog.com</link>
	<description>Social Software Enthusiast</description>
	<lastBuildDate>Mon, 09 Aug 2010 21:03:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Rails XSS Filter</title>
		<link>http://www.stubbleblog.com/index.php/2007/06/rails-xss-filte/</link>
		<comments>http://www.stubbleblog.com/index.php/2007/06/rails-xss-filte/#comments</comments>
		<pubDate>Wed, 20 Jun 2007 22:22:41 +0000</pubDate>
		<dc:creator>Tony Stubblebine</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[foocamp]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://www.stubbleblog.com/wp/?p=184</guid>
		<description><![CDATA[I was pushed to put XSS protections into CrowdVine when one of the Foo Camper&#8217;s released this XSS crack into the Foo Camp social network. It causes a person to friend everyone in the network and then inserts itself into your profile. It was brutal. Worse it was a very simple and readable 49 lines [...]]]></description>
			<content:encoded><![CDATA[<p>I was pushed to put XSS protections into CrowdVine when one of the Foo Camper&#8217;s released <a href="http://www.stubbleblog.com/foocamp_xss_hack.js.txt">this XSS crack into the Foo Camp social network</a>. It causes a person to friend everyone in the network and then inserts itself into your profile. It was brutal. Worse it was a very simple and readable 49 lines of code. I took one glance at it and realized that even I know enough javascript to write one of these.</p>
<p>Looking around I saw two approaches for Rails. Run <a href="http://wiki.rubyonrails.org/rails/pages/Safe+ERB">Safe ERB</a> and force yourself to validate each individual input or run <a href="http://www.techno-weenie.net/">Rick Olson&#8217;s</a> <a href="http://svn.techno-weenie.net/projects/plugins/white_list/">white list plugin</a>.</p>
<p>I decided to use the white_list plugin to clean all values in params. It required a little bit of tweaking. Here&#8217;s the details.</p>
<p>Install the white list plugin:</p>
<blockquote><p><code>script/plugin install -x http://svn.techno-weenie.net/projects/plugins/white_list/</code></p></blockquote>
<p>Edit vendor/plugins/white_list/init.rb so that white_list is available in the Controller:</p>
<blockquote>
<pre><code>
require 'white_list_helper'
ActionController::Base.send :include, WhiteListHelper</code></pre>
</blockquote>
<p>Add a filter to application.rb in order to walk the <code>params</code> hash:</p>
<blockquote>
<pre><code>
before_filter :sanitize_params

def sanitize_params
# TODO: 2007-06-20  -- I found that this didn't
# work when called with params instead of @params. I assume
# I'm clueless in some important regard. (Many important regards?)
@params = walk_hash(@params) if @params and !site_owner?
end

def walk_hash(hash)
hash.keys.each do |key|
if hash[key].is_a? String
hash[key] = white_list(hash[key])
elsif hash[key].is_a? Hash
hash[key] = walk_hash(hash[key])
elsif hash[key].is_a? Array
hash[key] = walk_array(hash[key])
end
end
hash
end

def walk_array(array)
array.each_with_index do |el,i|
if el.is_a? String
array[i] = white_list(el)
elsif el.is_a? Hash
array[i] = walk_hash(el)
elsif el.is_a? Array
array[i] = walk_array(el)
end
end
array
end
</code></pre>
</blockquote>
<p>Does this look right to people? Is there a more idiomatic ruby/rails way to do this? I&#8217;m a bit worried about how this will perform on very large chunks of data or on deeply nested hashes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stubbleblog.com/index.php/2007/06/rails-xss-filte/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
