<?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>All Things (v8) &#187; JavaScript</title>
	<atom:link href="http://www.anthonyeden.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.anthonyeden.com</link>
	<description>Technology Blog</description>
	<lastBuildDate>Sat, 19 Nov 2011 18:06:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>XSS and Building HTML in JavaScript</title>
		<link>http://www.anthonyeden.com/2010/02/xss-and-building-html-in-javascript/</link>
		<comments>http://www.anthonyeden.com/2010/02/xss-and-building-html-in-javascript/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 22:04:31 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[ujs]]></category>

		<guid isPermaLink="false">http://www.anthonyeden.com/?p=123</guid>
		<description><![CDATA[In my recent article on Rails, JQuery, Unobtrusive JS and Graceful Degradation I implemented the Ajax logic for adding a new entry using JSON returned from the server to build the HTML in JavaScript. An attentive reader brought to my attention the cross-site scripting vulnerability that is possible with this implementation and I think this [...]]]></description>
			<content:encoded><![CDATA[<p>In my recent article on <a href="http://www.anthonyeden.com/2010/02/rails-jquery-unobtrusive-js-and-graceful-degradation/">Rails, JQuery, Unobtrusive JS and Graceful Degradation</a> I implemented the Ajax logic for adding a new entry using JSON returned from the server to build the HTML in JavaScript. An attentive reader brought to my attention the cross-site scripting vulnerability that is possible with this implementation and I think this would be a good time to address the issue and open the comments to other options.</p>
<p>First of all let&#8217;s look at the most obvious attack vector, which is pretty simple: a malicious user would enter a script into the person name field and save that and then it would execute the script when the person is rendered in the person list. First I verified that this attack vector worked, and yes it did work, so then I considered where the value entered by the user needed to be sanitized. It&#8217;s pretty straightforward to sanitize the rendering when Rails is involved, just add the <code>h()</code> helper before the name output in the listing, so line 4 of index.html.erb becomes:</p>
<p><script src="http://gist.github.com/305978.js?file=gistfile1.rhtml"></script></p>
<p>This is a fairly standard idiom in Rails (not necessarily the best way to sanitize user input, but the most common way to do it). This fix only fixes the problem when the page is rendered by Rails, but it does not address the issue for items that are added via Ajax, and here is where things get a bit tricky.</p>
<p>You&#8217;d think that sanitizing user input in JavaScript would be fairly easy, and to be clear you can do it with the JavaScript <code>escape()</code> function, however this results in some pretty ugly percent-encoded content (which may or may not matter to you. I was curious though whether there was something similar to the <code>h()</code> helper in Rails available in JavaScript. There is but finding it proved to be a bit of a challenge.</p>
<p>What I ended up doing what searching <a href="http://stackoverflow.com/">Stack Overflow</a> for solutions that others had come up with and eventually landed upon this bit of code: <a href="http://code.google.com/p/google-caja/source/browse/trunk/src/com/google/caja/plugin/html-sanitizer.js">http://code.google.com/p/google-caja/source/browse/trunk/src/com/google/caja/plugin/html-sanitizer.js</a>. Unfortunately this bit of code by itself doesn&#8217;t work unless you have another JS library (html4) and that library requires building with Ant. Fortunately additional searching led me to a minified version with everything already compiled. I&#8217;ve included this minified library as html-sanitizer-minified.js and added a call to html_santize before rendering the person name in the JavaScript as such:</p>
<p><script src="http://gist.github.com/305989.js?file=gistfile1.js"></script></p>
<p>..and it works. I&#8217;ve deployed this fix to <a href="http://rails-jquery-unobtrusive.heroku.com/">http://rails-jquery-unobtrusive.heroku.com/</a> and have updated the <a href="http://github.com/aeden/unobtrusive-jquery-example">github repo</a> so you can see the changes in action.</p>
<p>So is this the best solution for handling user input and avoiding XSS when you want to build HTML in JavaScript? I&#8217;m not sure. It&#8217;s probably not the only solution so feel free to add yours in the comments below. Also, consider this a warning if you are developing JavaScript code that builds and renders HTML: tainted user input is a threat and you need to deal with it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anthonyeden.com/2010/02/xss-and-building-html-in-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rails, JQuery, Unobtrusive JS and Graceful Degradation</title>
		<link>http://www.anthonyeden.com/2010/02/rails-jquery-unobtrusive-js-and-graceful-degradation/</link>
		<comments>http://www.anthonyeden.com/2010/02/rails-jquery-unobtrusive-js-and-graceful-degradation/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 21:50:24 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[ujs]]></category>

		<guid isPermaLink="false">http://www.anthonyeden.com/?p=115</guid>
		<description><![CDATA[I&#8217;m always on the lookout for ways to improve my web applications. To that end I wanted to see if I could come up with a way to marry jQuery and Rails in a way that was both unobtrusive and a way that would degrade gracefully in a Rails 2.3.5 application. I&#8217;m looking forward to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m always on the lookout for ways to improve my web applications. To that end I wanted to see if I could come up with a way to marry jQuery and Rails in a way that was both unobtrusive and a way that would degrade gracefully in a Rails 2.3.5 application. I&#8217;m looking forward to seeing how UJS with jQuery in Rails looks as of Rails 3.0, however in the meantime the method I describe below may prove to be useful.</p>
<p>If you want to go straight to the code it is available on Github: <a href="http://github.com/aeden/unobtrusive-jquery-example">http://github.com/aeden/unobtrusive-jquery-example</a>. I have also put a live demo on this up on Heroku: <a href="http://rails-jquery-unobtrusive.heroku.com">http://rails-jquery-unobtrusive.heroku.com</a>.</p>
<p>Rather than going through all of the steps needed to set up a Rails app, the best thing to do is to follow along in the code from the example above.</p>
<p>First let&#8217;s take a look at the PeopleController (app/controllers/people_controller.rb):</p>
<p><script src="http://gist.github.com/296266.js?file=gistfile1.rb"></script></p>
<p>This looks like a fairly normal RESTful controller. The one exception is the delete method, which will be used by browsers that have JavaScript turned off to provide a page that 1.) confirms that the record should be deleted and 2.) has a form that can post to the destroy method.</p>
<p>Next, let&#8217;s take a look at the index view:</p>
<p><script src="http://gist.github.com/296269.js?file=gistfile1.rhtml"></script></p>
<p>There is a list of people with each person having a delete link next to their name. The delete link points to /person/:id/delete which will execute the delete action that I mentioned above (I&#8217;ll show you how to remove the &#8220;/delete&#8221; part of that link with JavaScript later). There is also a form for adding a new person. Note that there is no JavaScript in this page, just normal HTML elements with classes.</p>
<p>While we&#8217;re looking at views, let&#8217;s look at the only other view, the delete view:</p>
<p><script src="http://gist.github.com/296270.js?file=gistfile1.rhtml"></script></p>
<p>This is just a simple form that confirms that they want to delete the user and provides a cancel link back to the people_url if they don&#8217;t. Note that the form tag uses the REST-ful URL for accessing the resource and sets &#8216;_method&#8217; to &#8216;delete&#8217; in the hidden field tag.</p>
<p>Finally, let&#8217;s take a look at the most complicated bit, the jQuery JavaScript. To make things easier to follow I&#8217;ll go through it piece by piece, starting with the document ready handler:</p>
<p><script src="http://gist.github.com/296271.js?file=gistfile1.js"></script></p>
<p>This is the jQuery way for executing code when the document has finished loading. All it does is call two methods, one for connecting up the add form and one for connecting up the delete links. To keep things organized and namespaced a bit I placed these two methods onto a JS object called &#8216;Actions&#8217;. First, the connectAddForm implementation:</p>
<p><script src="http://gist.github.com/296272.js?file=gistfile1.js"></script></p>
<p>This function finds the form element with the class &#8220;people&#8221; and attaches an event handler to the submit event. When the form is submitted an AJAX POST is made. The data type is JSON, which will call the &#8216;create&#8217; action in the people controller with the accepts type set to a value that triggers Rails to render JSON as the response. The form is serialized and passed as data and when a successful response is received a bit of HTML is appended to the people list and the person name field is cleared.</p>
<p>Now for the connect delete links JavaScript:</p>
<p><script src="http://gist.github.com/296273.js?file=gistfile1.js"></script></p>
<p>Here each element that is in the people list that matches the selector a.delete and adds a click handler to it. Notice that I used the jQuery live method here, the benefit being that as new records are added, and new elements that match the selector expression are added automatically get the click handler that I define here. This saves me from the trouble of having to add the handler each time that a record is added to the page. The click handler first finds the li that it may eventually delete, displays a confirmation message to the viewer and if they confirm that they want to delete the record it will post via AJAX to the /person/:id URL with the method &#8216;delete&#8217;. The URL is pulled from the anchor&#8217;s href attribute and the &#8216;/delete&#8217; is removed using a simple regular expression replace. Upon a successful delete the li that was found earlier is hidden using the slideToggle() function.</p>
<p>That&#8217;s it. You can try this in a browser either with JavaScript turned on, in which case it&#8217;ll use all of the JavaScript goodness, or you can turn of JavaScript and see the magic of graceful degradation. I hope you&#8217;ve enjoyed reading this little tutorial and if you have any suggestions on how to improve the code in the project please feel free to fork the github project, make your changes and send me a pull request. I&#8217;ll add any cool mods into the tutorial as I can.</p>
<p><b>Update:</b> I&#8217;ve <a href="http://www.anthonyeden.com/2010/02/xss-and-building-html-in-javascript/">published a follow-on article</a> that talks about changes to the code for this tutorial to handle sanitizing user input both in Rails and in JavaScript. I suggest reading it to understand XSS vulnerabilities that you need to deal with when accepting user input.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anthonyeden.com/2010/02/rails-jquery-unobtrusive-js-and-graceful-degradation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Displaying A Default Image</title>
		<link>http://www.anthonyeden.com/2009/03/displaying-a-default-image/</link>
		<comments>http://www.anthonyeden.com/2009/03/displaying-a-default-image/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 15:38:58 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.anthonyeden.com/?p=49</guid>
		<description><![CDATA[Have you ever wanted to display a default image if the img tag source references a file that cannot be found? You might think that you could use the onerror event on the img tag, but don&#8217;t be fooled into thinking it&#8217;s that easy! No, it isn&#8217;t, because most browsers don&#8217;t support that event in [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever wanted to display a default image if the img tag source references a file that cannot be found? You might think that you could use the onerror event on the img tag, but don&#8217;t be fooled into thinking it&#8217;s that easy! No, it isn&#8217;t, because most browsers don&#8217;t support that event in the way you expect.</p>
<p>Here&#8217;s a little bit of JavaScript that I have used to successfully replace a broken image with a default (assumes <a href="http://prototypejs.org/">prototype</a> is used). Also note that this has been extracted somewhat from my implementation and thus may have gathered bugs during the extraction, but the concept should still be clear.</p>
<pre>
$$('img.my_class_name').each(function(e) {
  var i = new Image();
  i.onerror = function() {
    e.src = "error.png";
    e.onerror = "";
    return true;
  };
  i.src = e.src;
});
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.anthonyeden.com/2009/03/displaying-a-default-image/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

