<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Wolfmans Howlings: REST scaffold_resource security warning</title>
    <link>http://blog.wolfman.com/articles/2007/06/26/rest-scaffold_resource-security-warning</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>A programmers Blog about Ruby, Rails and a few other issues</description>
    <item>
      <title>REST scaffold_resource security warning</title>
      <description>&lt;p&gt;This one is so blatantly obvious it bit me in the Butt at 4am this
morning when I had to get up and fix it! I am so embarrassed, luckily
no private data got out, as no-one has entered any private data yet.&lt;/p&gt;

&lt;p&gt;I used the script/generate scaffold_resource to get started, and I
left in those nice format.xml things in, thinking I may use them in
the future. For the most part this is not a problem, but one of my
controllers is a profile table. Much of the data in there is public
anyway so no big deal, but a few columns are private data like email,
date of birth, phone numbers etc. These are specifically private and
not viewable publicly. This is enforced but not having a view that
shows any of that stuff to the general public.&lt;/p&gt;

&lt;p&gt;However the tricky little scaffold-generated code...&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;index&lt;/span&gt;
    &lt;span class="attribute"&gt;@profiles&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;Profile&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;find&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="symbol"&gt;:all&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:order&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;first_name, last_name, alias&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;)&lt;/span&gt;

    &lt;span class="ident"&gt;respond_to&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;format&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
      &lt;span class="ident"&gt;format&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;html&lt;/span&gt; &lt;span class="comment"&gt;# index.rhtml&lt;/span&gt;
      &lt;span class="ident"&gt;format&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;xml&lt;/span&gt;  &lt;span class="punct"&gt;{&lt;/span&gt; &lt;span class="ident"&gt;render&lt;/span&gt; &lt;span class="symbol"&gt;:xml&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="attribute"&gt;@profiles&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;to_xml&lt;/span&gt; &lt;span class="punct"&gt;}&lt;/span&gt;
     &lt;span class="keyword"&gt;end&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Has this cool .to_xml stanza, which happily takes every column and
converts it to XML and sends it back as a response to the query
/profiles.xml&lt;/p&gt;

&lt;p&gt;Yikes, I woke up with a start when I realized that, and rushed to test
it and yep it works as it is supposed to.&lt;/p&gt;

&lt;p&gt;Obviously this is easy to fix, Just exclude the attributes you don't
want shown:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@profiles.to_xml(:only =&amp;gt; [:first_name, :last_name])
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But it sure is a nasty back door if you forget!&lt;/p&gt;

&lt;p&gt;Caveat Programmer!&lt;/p&gt;

&lt;p&gt;&lt;a href="http://technorati.com/tag/scaffold_resource" rel="tag"&gt;&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 26 Jun 2007 15:14:04 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:916407f1-5fc4-4464-8a6b-7e98f9e1f146</guid>
      <author>Jim Morris</author>
      <link>http://blog.wolfman.com/articles/2007/06/26/rest-scaffold_resource-security-warning</link>
      <category>Rails</category>
      <category>rails</category>
      <category>scaffold_resource</category>
      <trackback:ping>http://blog.wolfman.com/articles/trackback/323</trackback:ping>
    </item>
    <item>
      <title>"REST scaffold_resource security warning" by Philip (flip) Kromer</title>
      <description>&lt;p&gt;Thanks for this -- I had the same realization, and now I can fix it.&lt;/p&gt;

&lt;p&gt;The other thing to set is filter&lt;em&gt;parameter&lt;/em&gt;logging, which controls what goes into your logs. (Logs should of course be outside the public purview, but 'Defense in Depth' is our creed.)&lt;/p&gt;

&lt;p&gt;If I understand correctly, attr_accessible controls data coming *in* -- it prevents someone setting an attribute by stuffing in a form value.  &lt;/p&gt;

&lt;p&gt;Using the the restful-authentication generator as an example: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In the model file, blacklist fields from the logs:
filter&lt;em&gt;parameter&lt;/em&gt;logging :password, :salt, "activation-code"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Also in the model, whitelist fields the user is allowed to set (this excludes things like confirmation code or usergroup):
attr&lt;em&gt;accessible :login, :email, :password, :password&lt;/em&gt;confirmation&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And, of course, in the controller file whitelist only the fields you wish to xml serialize:
format.xml  { render :xml =&gt; @user.to&lt;em&gt;xml(:only =&gt; [:first&lt;/em&gt;name, :last_name]) }&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;</description>
      <pubDate>Sun, 23 Dec 2007 00:01:01 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:72ba6129-4163-4b82-8eb0-cc12da6a93f0</guid>
      <link>http://blog.wolfman.com/articles/2007/06/26/rest-scaffold_resource-security-warning#comment-185</link>
    </item>
    <item>
      <title>"REST scaffold_resource security warning" by wolfmanjm</title>
      <description>&lt;p&gt;AFAIK &lt;code&gt;attr_protected&lt;/code&gt; and &lt;code&gt;attr_accessible&lt;/code&gt; only protect against mass writes not reads, so that won't work in this case.&lt;/p&gt;</description>
      <pubDate>Sun, 15 Jul 2007 13:29:55 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:dd81f7ac-f237-4957-b175-41a779758cf6</guid>
      <link>http://blog.wolfman.com/articles/2007/06/26/rest-scaffold_resource-security-warning#comment-98</link>
    </item>
    <item>
      <title>"REST scaffold_resource security warning" by Evgeny</title>
      <description>&lt;p&gt;Actually, its probably better to fix that in the model, and not the controller.
Using &lt;code&gt;attr_accessible&lt;/code&gt; or &lt;code&gt;attr_protected&lt;/code&gt;, or something similar - just close those columns for the general public and all your problems disappear. Besides, its the more "right" place for it anyways.&lt;/p&gt;</description>
      <pubDate>Sun, 15 Jul 2007 13:07:44 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:b7a6c700-7b53-4383-8d87-16bb5b9d9f7c</guid>
      <link>http://blog.wolfman.com/articles/2007/06/26/rest-scaffold_resource-security-warning#comment-96</link>
    </item>
    <item>
      <title>"REST scaffold_resource security warning" by K. Adam Christensen</title>
      <description>&lt;p&gt;Nice catch!  Time to go review some code. &lt;/p&gt;</description>
      <pubDate>Wed, 27 Jun 2007 08:44:07 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:cb7d1509-910f-440a-8089-162e9810f2b6</guid>
      <link>http://blog.wolfman.com/articles/2007/06/26/rest-scaffold_resource-security-warning#comment-7</link>
    </item>
  </channel>
</rss>
