<?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>Moves On Rails: Tag rails</title>
    <link>http://www.movesonrails.com/articles/tag/rails?tag=rails</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>New ways to look at software</description>
    <item>
      <title>User Authorization in Rails</title>
      <description>&lt;p&gt;A lot of rails apps use some form of user login.
Usually a user identifies itself by entering a username/password combination into a form.
The application will then check the combination and use the session of the user to store which user authenticated for that session.&lt;/p&gt;

&lt;p&gt;Plugins like acts_as_authenticated give a nice starting point, but must applications write authorazation themselves as it is quite simple to do in Rails. If you have done this too, then here are a number of security points which such login process should take into account.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Brute forcing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Rails apps are usually tweaked to respond to requests as quickly as possible.
If a response is slow, it will block my mongrel!
This makes Rails apps a very good partner for the brute forcing of passwords.
Action against this is surprisingly simple.
You can count the number of incorrect login attempts.
If this becomes more then 10, let the user enter a captcha.
Or really degrade the login by rejecting every login attempt with a 403, when the last incorrect login attempt was less then two or three seconds ago.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HTTP login posting&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Many sites offer a login box on their front page.
This is a very user friendly and I would be the last person to tell you not to do this.
However, this often means that you are letting a user authenticate over HTTP, which is not very secure.
Anyone snooping the traffic of that user will see a plaintext username and password being posted to your site.
Poke around with Webscarab or Firebug in Firefox to pick up on these things easily.&lt;/p&gt;

&lt;p&gt;Solutions are again easy. If you have a HTTPS part of your site, make sure the login form posts to it.
If you do not have HTTPS, you can encrypt the password in the clients browser with javascript via a public/private key combination. A lot of good examples of this can be found all over the internet.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Session hacking&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At the moment Rails supports two types of sessions.
It can reside serverside in a file or database and clientside in the cookie of the client.&lt;/p&gt;

&lt;p&gt;We will first discuss the server side session, as this was the default before Rails 2.1.
When a user connects to your website, he will automatically receive a unique session id.
This is is the only thing your application can use to distinguish between unique users and their requests.
The default session ids in Rails are generated correctly, so that is very hard for someone to guess ids of other users using your application.
But if a hacker does acquire a session id of another user, Rails will not offer you any protection by default.
And there actually quite a few ways for a hacker to obtain session ids of other users.
The two most commonly used being: Cross Site Scripting exploits (XSS) and code injection.&lt;/p&gt;

&lt;p&gt;Why is this a problem? Well if an attacker shares a session id with another user, and that user logs in, the attacker will share their session.
This means that the attacker will have the same privileges as the user.
Countering this can be a bit of a hassle, but as every application is sure to have a XSS exploit somewhere, it is a hole that is important to plug.&lt;/p&gt;

&lt;p&gt;First you need to reset sessions after a logon. This will counter any creatively crafted URL attacks.
Secondly it is a good idea to fixate your sessions on the ip used in the login attempt and you also should take a look at sesion expiration.&lt;/p&gt;

&lt;p&gt;Clientside sessions have the same problems, so don't count on them as being the ultimate solution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SQL injection&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Initially I wanted to leave this out, i as the problem is still so common, I decided to have it here anyway.
Rail does provide you with SQL injection protection, but you still have to use it correctly in order to be safe.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;User.find(:conditions =&amp;gt;
   ["username = #{username} AND password = MD5(#{password})"])

User.find_by_username_and_password(username, SHA(password))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;They both work, but the first one can be easily exploited through SQL injection.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This will not magically fix all your user authorization problems, but it should point you in some interesting directions.  Want to read more? Here are a few good places to start reading.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://guides.rails.info/securing_rails_applications/security.html"&gt;http://guides.rails.info/securing&lt;em&gt;rails&lt;/em&gt;applications/security.html&lt;/a&gt;
&lt;a href="http://www.rorsecurity.info"&gt;http://www.rorsecurity.info&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Wed, 22 Oct 2008 10:51:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:1749dd8c-eb30-4dfd-9155-409b59f79a72</guid>
      <author>bart.tenbrinke@movesonrails.com (Bart ten Brinke)</author>
      <link>http://www.movesonrails.com/articles/2008/10/22/user-authorization-in-rails</link>
      <category>use</category>
      <category>authorization</category>
      <category>problem</category>
      <category>rails</category>
    </item>
    <item>
      <title>Nested include has major memory leak (Rails 2.0.1).</title>
      <description>&lt;p&gt;As our mongrels were using up quite a lot of memory, so I tried to figure out what was causing this.&lt;/p&gt;

&lt;p&gt;When running the app locally I found out that one certain page caused the mongrel to grow from 60 to 190 megabytes. A whopping 130 megabytes!&lt;/p&gt;

&lt;p&gt;After commenting out some of the code, I realized that a single line was causing all of the memory usage &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;contracts = Contract.find( :all, 
  :conditions =&amp;gt; ['contracts.employee_id IN (?) ', employees ],
  :include =&amp;gt; [:expertise_profile =&amp;gt; :qualifications ] )
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ouch! The nested include of rails somehow leaks a large amount of memory.
The fix was a piece of cake.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;contracts = Contract.find( :all, 
  :conditions =&amp;gt; ['contracts.employee_id IN (?) ', employees ],
  :include =&amp;gt; :expertise_profile )
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now my mongrel stays at 60 megabytes. I don't know if this issue persists in the new 2.1 Rails, but I'll check that soon!&lt;/p&gt;</description>
      <pubDate>Wed, 02 Jul 2008 15:59:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:1a3ee134-137c-49b9-9d0d-9d868a301f41</guid>
      <author>bart.tenbrinke@movesonrails.com (Bart ten Brinke)</author>
      <link>http://www.movesonrails.com/articles/2008/07/02/nested-include-has-major-memory-leak-rails-2-0-1</link>
      <category>Rails</category>
      <category>find</category>
      <category>nested</category>
      <category>include</category>
      <category>memory</category>
      <category>rails</category>
    </item>
    <item>
      <title>Testing your Application Controller with rSpec</title>
      <description>&lt;p&gt;I was trying to create a function that would check the enforcement of the before filters in my application controller.
After going through a lot of rspec documentation and examples, I found nothing that really suited my needs. After a long google, I found a mention in the &lt;a href="http://rubyforge.org/pipermail/rspec-users/2007-May/001698.html"&gt;rspec mailing list&lt;/a&gt; and a hint to a solution for this. This is a working example of this idea.&lt;/p&gt;

&lt;p&gt;I have in my application controller the following code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class ApplicationController &amp;lt; ActionController::Base
  before_filter :check_authorization
   ....
  # Checks if a user is authorized
  def check_authorization
    User.check_authorization(controller_name, action_name)
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And in spec/controllers/application/application_spec.rb the following description.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;describe "an authorized controller", :shared =&amp;gt; true do

  it "should have the check_authorization set in the before filter" do
    ApplicationController.before_filters.should \
      include(:check_authorization)
  end   
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In all other controller specs I can now do this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require 'application_controller_spec'

describe UnitsController, "in general" do
  it_should_behave_like "an authorized controller"
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;By doing this I now have a spec that ensures that every controller checks authorizations before doing anything else. How cool is that :)!&lt;/p&gt;

&lt;p&gt;It would be nicer if the test handled skip before_filters in some nice way like: it_should_behave_like "a monkey".except_for_action('show'). Has anyone got any ideas for that?&lt;/p&gt;

&lt;p&gt;Special thanks to Matthijs Langenberg for his insights.&lt;/p&gt;</description>
      <pubDate>Wed, 23 Jan 2008 14:25:00 +0100</pubDate>
      <guid isPermaLink="false">urn:uuid:69ed57df-1049-44b2-b5af-d01bdc2b72d6</guid>
      <author>bart.tenbrinke@movesonrails.com (Bart ten Brinke)</author>
      <link>http://www.movesonrails.com/articles/2008/01/23/spec-ing-your-application-controller</link>
      <category>Rails</category>
      <category>Rspec</category>
      <category>rails</category>
      <category>applicationcontroller</category>
    </item>
    <item>
      <title>Moving to 2.0</title>
      <description>&lt;p&gt;Issues found so far:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Lots of small routing changes. Thank god for grep! : &lt;code&gt;addresses_path(@employee) =&gt; employee_addresses_path(@employee) &lt;/code&gt;. (Don't forget the *_url methods!)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Several plugins failed due to &lt;code&gt;extract_options_from_args!&lt;/code&gt;. This method has been replaced with the nicer: &lt;code&gt;args.extract_options!&lt;/code&gt; (i.e acts_as_paranoid, acts_as_mappable, paginating_find)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;acts_as_paranoid had minor issues: fix by replacing &lt;code&gt;construct_count_with_legacy_args&lt;/code&gt; to &lt;code&gt;construct_count_options_from_args&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;ActiveResource is now part of rails core. Be sure to freeze edge twice if you are upgrading from 1.2.3 or lower. All of our libXML additions had to be redone. (this time through /patches, tnx fngtps)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Different behavior of the render method. Before you could call &lt;code&gt;render "addresses/show"&lt;/code&gt; if you wanted to, this has been changed to &lt;code&gt;render :template =&gt; "addresses/show"&lt;/code&gt;. This affects several plugins too (like rspec_on_rails)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We no longer need the mysql_tasks plugin, since this functionality is now build right in!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;redhillonrails_core has some issues with connection adapters. Apparently rails 2.0 no longer loads adapters it doesn't need. This creates some issues with the redhills plugin since it tries to include stuff in those adapters. Adding a begin/rescue block around each include solves the issue.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The development environment no longer needs the &lt;code&gt;config.breakpoint_server = true&lt;/code&gt; setting.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Polymorphic models are now saved with the base class as type in external objects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Autocomplete textfields are now a plugin: &lt;code&gt;./script/plugin install auto_complete&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you have overridden the to_json methods, be sure to change that to &lt;code&gt;to_json options={}&lt;/code&gt;, else they might fail.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Migrations no longer working? Try removing duplicate names. We have two migrations with the same name (but different id) and it just skipped the first one (!)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Benefits so far:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Speed! We did expect some speed increase, but this is major! Our pdf generating stuff uses a lot of ActiveRecord and we saw decreases of more than 50% request time.&lt;/li&gt;
&lt;/ul&gt;</description>
      <pubDate>Thu, 18 Oct 2007 15:15:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:7d0e8dbf-83dc-4153-964a-88e93356d528</guid>
      <author>andre.foeken@movesonrails.com (Andre Foeken)</author>
      <link>http://www.movesonrails.com/articles/2007/10/18/moving-to-2-0</link>
      <category>Rails</category>
      <category>Ruby</category>
      <category>update</category>
      <category>2.0</category>
      <category>edge</category>
      <category>rails</category>
      <category>problems</category>
    </item>
    <item>
      <title>Rails Security</title>
      <description>&lt;embed width="477" height="390" allowfullscreen="true" allowscriptaccess="always" quality="high" bgcolor="#FFFFFF" name="player" id="player" src="http://s3.amazonaws.com/slideshare/player.swf?useHttp=1&amp;inContest=0&amp;totalSlides=10&amp;startSlide=1&amp;presentationId=114381&amp;doc=rails-security-bart-ten-brinke1352&amp;321" type="application/x-shockwave-flash"/&gt;
&lt;br/&gt;
&lt;br/&gt;


After a security presentation at RailsConfEurope 2007, I found a lot was missing, so I made this.

I didn’t finish it in time for reject conf, so I posted 
it here. Now it's time for me to go on vacation! See you next week!</description>
      <pubDate>Thu, 20 Sep 2007 19:28:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:1b884a8c-82cd-4c7a-a828-f0c72fb55c32</guid>
      <author>bart.tenbrinke@movesonrails.com (Bart ten Brinke)</author>
      <link>http://www.movesonrails.com/articles/2007/09/20/rails-security</link>
      <category>Rails</category>
      <category>Security</category>
      <category>rails</category>
      <category>XXS</category>
      <category>railsconf</category>
      <category>europe</category>
      <category>2007</category>
    </item>
    <item>
      <title>Gettext Generators 1.2.3 Released</title>
      <description>&lt;p&gt;The longawaited update for the Gettext Generator.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First release as plugin&lt;/li&gt;
&lt;li&gt;Added svn:merge raketask&lt;/li&gt;
&lt;li&gt;Updated generated resource to mimmick scaffold_generator packaged with rails 1.2.3 tag&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So go to &lt;a href="http://rubyforge.org/projects/gettextgnrtrs/"&gt;RubyForge&lt;/a&gt; or take a look &lt;a href="/pages/gettextgenerators"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I'm currently getting the plugin uploaded to rubyforge. As soon as I have that figured out how that works, I'll post the link here.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ruby script/plugin install svn://rubyforge.org/var/svn/gettextgnrtrs/tags\
/Gettext-Generators-1.2.3/&lt;/code&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 28 Aug 2007 20:41:00 +0200</pubDate>
      <guid isPermaLink="false">urn:uuid:b5414d5b-723e-41b4-8248-a117fc4d8d2b</guid>
      <author>bart.tenbrinke@movesonrails.com (Bart ten Brinke)</author>
      <link>http://www.movesonrails.com/articles/2007/08/28/gettext-generators-1-2-3-released</link>
      <category>Project update</category>
      <category>Rails</category>
      <category>Ruby</category>
      <category>gettextgenerators</category>
      <category>rails</category>
      <category>ruby</category>
    </item>
  </channel>
</rss>
