<?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>the evolving ultrasaurus &#187; code</title>
	<atom:link href="http://www.ultrasaurus.com/sarahblog/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ultrasaurus.com</link>
	<description>Sarah Allen's reflections on internet software and other topics</description>
	<lastBuildDate>Mon, 30 Jan 2012 19:26:43 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>cucumber and custom rspec matchers with rails 3.1</title>
		<link>http://www.ultrasaurus.com/sarahblog/2012/01/cucumber-and-custom-rspec-matchers-with-rails-3-1/</link>
		<comments>http://www.ultrasaurus.com/sarahblog/2012/01/cucumber-and-custom-rspec-matchers-with-rails-3-1/#comments</comments>
		<pubDate>Sun, 22 Jan 2012 16:40:52 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.ultrasaurus.com/?p=3548</guid>
		<description><![CDATA[I&#8217;m working my way through an epic Rails 3.1 upgrade and some of my cucumber features were failing because I was using a custom RSpec matcher and the method wasn&#8217;t found.
My custom matcher looks something like this:

module CustomMatchers

  class XmlSubsetMatcher
      :
  end

  def be_xml_subset_of(expected)
    [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working my way through an epic Rails 3.1 upgrade and some of my cucumber features were failing because I was using a custom RSpec matcher and the method wasn&#8217;t found.</p>
<p>My custom matcher looks something like this:</p>
<pre>
module CustomMatchers

  class XmlSubsetMatcher
      :
  end

  def be_xml_subset_of(expected)
    XmlSubsetMatcher.new(expected)
  end
</pre>
<p>and when I ran my feature I was getting this failure:<br />
<code><br />
      undefined method `xml_subset_of?' for #<String:0x007f9839d30378> (NoMethodError)<br />
</code></p>
<p>As it turns out, in my zeal to make sure everything was using the latest and great new stuff, I had forgotten to move over this critical configuration line in cucumbers env.rb:</p>
<p><code><br />
World(CustomMatchers)<br />
</code></p>
<p>Now, my cucumber feature is happily failing cuz my code doesn&#8217;t work. Whew.  I couldn&#8217;t find this documented anywhere and I&#8217;m not even sure where this documentation would belong.  I found a hint on the <a href="https://github.com/cucumber/cucumber/wiki/RSpec-Expectations">cucumber wiki rspec expectations page</a>, but none of the code on that page is actually needed when using cucumber with Rails, so I decided not to touch it and just write this blog post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ultrasaurus.com/sarahblog/2012/01/cucumber-and-custom-rspec-matchers-with-rails-3-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ffmpeg on osx lion</title>
		<link>http://www.ultrasaurus.com/sarahblog/2012/01/ffmpeg-on-osx-lion/</link>
		<comments>http://www.ultrasaurus.com/sarahblog/2012/01/ffmpeg-on-osx-lion/#comments</comments>
		<pubDate>Sun, 08 Jan 2012 05:40:37 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.ultrasaurus.com/?p=3537</guid>
		<description><![CDATA[I found that I needed to convert an m4a audio file (which is what QuickTime saves when I record audio) to a wav file, so I decided to use my favorite &#8220;can opener.&#8221; The versatile open source ffmpeg tool has always seemed to be able to convert anything to anything in audio-video formats.
I decided to [...]]]></description>
			<content:encoded><![CDATA[<p>I found that I needed to convert an m4a audio file (which is what QuickTime saves when I record audio) to a wav file, so I decided to use my favorite &#8220;can opener.&#8221; The versatile open source <a href="http://ffmpeg.org">ffmpeg</a> tool has always seemed to be able to convert anything to anything in audio-video formats.</p>
<p>I decided to pull the source from git:<br />
<code><br />
$ git clone git://source.ffmpeg.org/ffmpeg.git<br />
$ cd ffmpeg/<br />
</code></p>
<p>Stable versions are tagged (which I could see with &#8220;git tag -l&#8221;).  I don&#8217;t need to live on the edge right now, so I switched to the tag &#8220;n0.9.1&#8243; which I assume is for the latest stable build &#8220;harmony&#8221; 0.9.1 and made a local branch based on that.<br />
<code><br />
$ git co n0.9.1<br />
$ git checkout -b n0.9.1<br />
</code></p>
<p>Instructions for building ffmpeg are in the &#8220;INSTALL&#8221; file.  I discovered I needed yasm, which I could install with brew.  Here&#8217;s what I did:<br />
<code><br />
$  brew install yasm<br />
$  ./configure<br />
$ make<br />
CC	libavdevice/alldevices.o<br />
CC	libavdevice/avdevice.o<br />
CC	libavdevice/lavfi.o<br />
AR	libavdevice/libavdevice.a<br />
CC	libavfilter/af_aconvert.o<br />
libavfilter/af_aconvert.c:53: warning: function declaration isn’t a prototype<br />
libavfilter/af_aconvert.c:105: warning: function declaration isn’t a prototype<br />
CC	libavfilter/af_aformat.o<br />
CC	libavfilter/af_anull.o<br />
CC	libavfilter/af_aresample.o<br />
    :<br />
   :<br />
ffserver.c: In function ‘parse_ffconfig’:<br />
ffserver.c:4236: warning: ‘avcodec_get_context_defaults2’ is deprecated (declared at ./libavcodec/avcodec.h:3948)<br />
ffserver.c:4237: warning: ‘avcodec_get_context_defaults2’ is deprecated (declared at ./libavcodec/avcodec.h:3948)<br />
LD	ffserver_g<br />
CP	ffserver<br />
STRIP	ffserver<br />
</code><br />
I saw a lot of warnings, but they didn&#8217;t seem to negatively affect what I was trying to do.  I found a <a href="http://www.catswhocode.com/blog/19-ffmpeg-commands-for-all-needs">nice blog post from catswhocode</a> to remind me of the usage, and was able to use this simple command:</p>
<p><code><br />
$ ./ffmpeg -i frog.m4a frog.wav<br />
ffmpeg version 0.9.1, Copyright (c) 2000-2012 the FFmpeg developers<br />
  built on Jan  7 2012 21:19:08 with llvm_gcc 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)<br />
  configuration:<br />
  libavutil    51. 32. 0 / 51. 32. 0<br />
  libavcodec   53. 42. 4 / 53. 42. 4<br />
  libavformat  53. 24. 2 / 53. 24. 2<br />
  libavdevice  53.  4. 0 / 53.  4. 0<br />
  libavfilter   2. 53. 0 /  2. 53. 0<br />
  libswscale    2.  1. 0 /  2.  1. 0<br />
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'frog.m4a':<br />
  Metadata:<br />
    major_brand     : M4A<br />
    minor_version   : 0<br />
    compatible_brands: M4V M4A mp42isom<br />
    creation_time   : 2012-01-08 05:09:05<br />
  Duration: 00:00:07.22, start: 0.000000, bitrate: 206 kb/s<br />
    Stream #0:0(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, s16, 201 kb/s<br />
    Metadata:<br />
      creation_time   : 2012-01-08 05:09:05<br />
      handler_name    :<br />
Output #0, wav, to 'frog.wav':<br />
  Metadata:<br />
    major_brand     : M4A<br />
    minor_version   : 0<br />
    compatible_brands: M4V M4A mp42isom<br />
    creation_time   : 2012-01-08 05:09:05<br />
    encoder         : Lavf53.24.2<br />
    Stream #0:0(und): Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s<br />
    Metadata:<br />
      creation_time   : 2012-01-08 05:09:05<br />
      handler_name    :<br />
Stream mapping:<br />
  Stream #0:0 -> #0:0 (aac -> pcm_s16le)<br />
Press [q] to stop, [?] for help<br />
size=    1244kB time=00:00:07.22 bitrate=1411.3kbits/s<br />
video:0kB audio:1244kB global headers:0kB muxing overhead 0.003611%</p>
<p>$ ls<br />
frog.m4a  frog.wav<br />
</code></p>
<p>Success!!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ultrasaurus.com/sarahblog/2012/01/ffmpeg-on-osx-lion/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>rails 3.0 and rake 0.9.2</title>
		<link>http://www.ultrasaurus.com/sarahblog/2011/09/rails-3-0-and-rake-0-9-2/</link>
		<comments>http://www.ultrasaurus.com/sarahblog/2011/09/rails-3-0-and-rake-0-9-2/#comments</comments>
		<pubDate>Sun, 11 Sep 2011 16:37:17 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.ultrasaurus.com/?p=3401</guid>
		<description><![CDATA[I really want to upgrade a Rails 3.0 project to Rails 3.1, but I&#8217;ve done a few spikes and it lacks test coverage, so I decided to pull in cucumber and write some features before moving forward.
I added cucumber-rails to my gemfile, and ran &#8220;bundle&#8221; and got this error:

/Users/sarah/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/specification.rb:289:in `load': uninitialized constant Psych::Syck (NameError)

What I [...]]]></description>
			<content:encoded><![CDATA[<p>I really want to upgrade a Rails 3.0 project to Rails 3.1, but I&#8217;ve done a few spikes and it lacks test coverage, so I decided to pull in cucumber and write some features before moving forward.</p>
<p>I added cucumber-rails to my gemfile, and ran &#8220;bundle&#8221; and got this error:</p>
<pre>
/Users/sarah/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/specification.rb:289:in `load': uninitialized constant Psych::Syck (NameError)
</pre>
<p>What I really needed was to <a href="http://blog.rubygems.org/2011/08/31/shaving-the-yaml-yacc.html">update my Ruby Gems</a> (bundle update &#8211;system) but before I discovered that I did &#8220;bundle update&#8221; which moved me forward to rake 0.9.2, so I started getting these warnings:</p>
<pre>
/Users/sarah/.rvm/gems/ruby-1.9.2-p290@pie-bakery/gems/psych-1.2.1/lib/psych.rb:93: warning: already initialized constant VERSION
/Users/sarah/.rvm/gems/ruby-1.9.2-p290@pie-bakery/gems/psych-1.2.1/lib/psych.rb:96: warning: already initialized constant LIBYAML_VERSION
WARNING: Global access to Rake DSL methods is deprecated.  Please include
...  Rake::DSL into classes and modules which use the Rake DSL methods.
WARNING: DSL method Bakery::Application#task called at /Users/sarah/.rvm/gems/ruby-1.9.2-p290@pie-bakery/gems/railties-3.0.0/lib/rails/application.rb:214:in `initialize_tasks'
</pre>
<p>So, I&#8217;ve learned from google, stackoverflow, various blogs and my twitter friend <a href="http://twitter.com/#!/excid3">@excid3</a> that I need to update my Rakefile to include:</p>
<pre>
require 'rake/dsl_definition'
require 'rake'
include Rake::DSL
</pre>
<p>That lets me use rake (yay!).  I still have the following two warnings:</p>
<pre>
/Users/sarah/.rvm/gems/ruby-1.9.2-p290@pie-bakery/gems/psych-1.2.1/lib/psych.rb:93: warning: already initialized constant VERSION
/Users/sarah/.rvm/gems/ruby-1.9.2-p290@pie-bakery/gems/psych-1.2.1/lib/psych.rb:96: warning: already initialized constant LIBYAML_VERSION
</pre>
<p>which I&#8217;m hoping will go away with the Rails 3.1 upgrade, but I thought I would write up the rest of it in case it helps other wayward souls on their journey.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ultrasaurus.com/sarahblog/2011/09/rails-3-0-and-rake-0-9-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>what exactly does rake spec do?</title>
		<link>http://www.ultrasaurus.com/sarahblog/2011/05/what-exactly-does-rake-spec-do/</link>
		<comments>http://www.ultrasaurus.com/sarahblog/2011/05/what-exactly-does-rake-spec-do/#comments</comments>
		<pubDate>Sun, 08 May 2011 21:41:35 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.ultrasaurus.com/?p=3164</guid>
		<description><![CDATA[$ rake spec
(in /Users/sarah/src/../my_app)
You have 1 pending migrations:
  20110416135407 CreateCourses
The rake spec command reminds us that we need to run our migration before running
our tests. In fact, it does a whole lot more than that. There are a whole bunch of best practices rolled in that one
command. To see exactly what is going on, [...]]]></description>
			<content:encoded><![CDATA[<p>$ rake spec<br />
(in /Users/sarah/src/../my_app)<br />
You have 1 pending migrations:<br />
  20110416135407 CreateCourses</p>
<p>The <commandname>rake spec</commandname> command reminds us that we need to run our migration before running<br />
our tests. In fact, it does a whole lot more than that. There are a whole bunch of best practices rolled in that one<br />
command. To see exactly what is going on, we can run <commandname>rake spec</commandname> with the <ic>&#8211;trace</ic><br />
option:</p>
<p><code language="session"><br />
$ rake spec --trace<br />
(in /Users/sarah/src/tfr/svn/Book/code/class_app_new_source)<br />
** Invoke spec (first_time)<br />
** Invoke db:test:prepare (first_time)<br />
** Invoke db:abort_if_pending_migrations (first_time)<br />
** Invoke environment (first_time)<br />
** Execute environment<br />
** Execute db:abort_if_pending_migrations<br />
** Execute db:test:prepare<br />
** Invoke db:test:load (first_time)<br />
** Invoke db:test:purge (first_time)<br />
** Invoke environment<br />
** Execute db:test:purge<br />
** Execute db:test:load<br />
** Invoke db:schema:load (first_time)<br />
** Invoke environment<br />
** Execute db:schema:load<br />
** Execute spec<br />
</code></p>
<p>When it says <ic>invoke</ic> it is calling a particular rake task, but then it will call its dependencies. To really see what is happening in what order, check out the <ic>execute</ic> commands.  The commands db:test:prepare and db:test:load don’t do much themselves, aside from setting up the environment and executing another task or two.  We can see from the output that rake is actually executing the following steps:</p>
<ol>
<li>
<p>
Don’t run the specs if there are pending migrations in the development database. (db:abort_if_pending_migrations)
</p>
</li>
<li>
<p>
Drop the test database (db:test:purge)
</p>
</li>
<li>
<p>
Load the schema into the test database (db:schema:load in environment “test”)
</p>
</li>
</ol>
<p>These steps make sure that we are always testing in a clean environment, so we know exactly what we’re testing when we run our specs.</p>
<p>The code that makes this happen in Rails 3, can now be found in <a href="https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake#L455">railties</a>.  (Thanks to <a href="http://twitter.com/pixeltrix">@pixeltrix</a> for pointing me to it.)/62206174505873408</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ultrasaurus.com/sarahblog/2011/05/what-exactly-does-rake-spec-do/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>repl rspec mocks</title>
		<link>http://www.ultrasaurus.com/sarahblog/2011/04/repl-rspec-mocks/</link>
		<comments>http://www.ultrasaurus.com/sarahblog/2011/04/repl-rspec-mocks/#comments</comments>
		<pubDate>Tue, 05 Apr 2011 01:59:19 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.ultrasaurus.com/?p=3127</guid>
		<description><![CDATA[REPL (Read-Eval-Print-Loop) is a great way to learn.  With Ruby, the experience is enabled with irb.  Sometimes, to do this we need to peek into the innards of things, which I find to be an extremely effective way to explain mocks and stubs.  It&#8217;s a regular part of my Ruby curriculum, even [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Read-eval-print_loop">REPL</a> (Read-Eval-Print-Loop) is a great way to learn.  With Ruby, the experience is enabled with irb.  Sometimes, to do this we need to peek into the innards of things, which I find to be an extremely effective way to explain mocks and stubs.  It&#8217;s a regular part of my Ruby curriculum, even though I have needed to figure out the syntax three times in the last couple of years.  (Many thanks to Jen-Mei Wu for the most recent iteration.)  I still think it is worth it, even though it seems to change with crazy frequency.  </p>
<p>Just in case anyone else ever wants to do this with current or previous versions of RSpec, I thought I would write it down before old versions become lost in the mists of time:</p>
<h2>RSpec 2.5</h2>
<pre>
>> require 'rspec/mocks'
>> include RSpec::Mocks::Methods
>>Time.stub(:now).and_return(10,20)
>>Time.now
10
>>Time.now
20
>>Time.now
20
</pre>
<h2>RSpec 2.0</h2>
<pre>
>> require 'rspec/mocks'
>> require 'rspec/mocks/extensions/object'
>>Time.stub(:now).and_return(10,20)
>>Time.now
10
>>Time.now
20
>>Time.now
20
</pre>
<h2>RSpec 1.3</h2>
<pre>
>> require 'spec'
>> require 'spec/mocks'
>>Time.stub(:now).and_return(10,20)
>>Time.now
10
>>Time.now
20
>>Time.now
20
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.ultrasaurus.com/sarahblog/2011/04/repl-rspec-mocks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>rails 3 vs. rails 2 validation errors</title>
		<link>http://www.ultrasaurus.com/sarahblog/2010/09/rails-3-vs-rails-2-validation-errors/</link>
		<comments>http://www.ultrasaurus.com/sarahblog/2010/09/rails-3-vs-rails-2-validation-errors/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 10:14:06 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.ultrasaurus.com/?p=2795</guid>
		<description><![CDATA[Not sure if this is a bug or a feature.  I&#8217;d guess it is here for a reason, and maybe I&#8217;m late for noticing, but Rails 3 errors now provides an array for each attribute, whereas in Rails 2.3 it was just a string.  Here&#8217;s the output from two almost identical applications&#8230;
Loading development environment [...]]]></description>
			<content:encoded><![CDATA[<p>Not sure if this is a bug or a feature.  I&#8217;d guess it is here for a reason, and maybe I&#8217;m late for noticing, but Rails 3 errors now provides an array for each attribute, whereas in Rails 2.3 it was just a string.  Here&#8217;s the output from two almost identical applications&#8230;</p>
<p>Loading development environment (Rails 2.3.8)<br />
&gt;&gt; person = Person.new<br />
=&gt; #&lt;Person id: nil, first_name: nil, last_name: nil, present: nil&#8230;<br />
&gt;&gt; person.valid?<br />
=&gt; false<br />
&gt;&gt; person.errors<br />
=&gt; #&lt;ActiveRecord::Errors:0&#215;1034d8f10 @errors=#&lt;OrderedHash &#8230;<br />
&gt;&gt; person.errors[:first_name]<br />
=&gt; &#8220;can&#8217;t be blank&#8221;</p>
<p>Loading development environment (Rails 3.0.0)<br />
&gt;&gt; person = Person.new<br />
=&gt; #&lt;Person id: nil, first_name: nil, last_name: nil, present: nil&#8230;<br />
&gt;&gt; person.valid?<br />
=&gt; false<br />
&gt;&gt; person.errors<br />
=&gt; {:first_name=&gt;["can't be blank"]}<br />
&gt;&gt; person.errors.class<br />
=&gt; ActiveModel::Errors<br />
&gt;&gt; person.errors[:first_name]<br />
=&gt; ["can't be blank"]</p>
<p>I didn&#8217;t see that in the release notes, but it failed my tests for ActiveRecord class.  Someone else must have a list of these details, yes?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ultrasaurus.com/sarahblog/2010/09/rails-3-vs-rails-2-validation-errors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>rails security review checklist</title>
		<link>http://www.ultrasaurus.com/sarahblog/2010/01/rails-security-review-checklist/</link>
		<comments>http://www.ultrasaurus.com/sarahblog/2010/01/rails-security-review-checklist/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 08:00:12 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.ultrasaurus.com/?p=2378</guid>
		<description><![CDATA[I&#8217;m reviewing the security of a web app built with Ruby on Rails, so I put together a checklist for a security audit.  This isn&#8217;t a bank or high security situation, but there were a number of engineers and quite a bit of open source code, so I thought a few checks were in [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m reviewing the security of a web app built with Ruby on Rails, so I put together a checklist for a security audit.  This isn&#8217;t a bank or high security situation, but there were a number of engineers and quite a bit of open source code, so I thought a few checks were in order.</p>
<p>Here&#8217;s the list I came up with that I thought other folks might appreciate as a starting point (special thanks to the sfruby list, <a href="http://afreshcup.com/">Mike Gunderloy</a>, and Scott Bronson for feedback):</p>
<p>0) Make sure your Rails and gems are up to date for latest security patches (see <a href="http://groups.google.com/group/rubyonrails-security">rails security mailing list</a> for recent advisory notes)</p>
<p>1) Active Record audit:<br />
&nbsp;&nbsp;A) SQL injection:<br />
&nbsp;&nbsp;&nbsp;&nbsp;(i) whole word search for &#8220;find&#8221;, &#8220;first&#8221;, and &#8220;all&#8221; then visually inspect all instances of ActiveRecord find calls for potential SQL injection vulnerability (also search for &#8220;sql&#8221; not whole work search to find find_by_sql and &#8220;execute&#8221; to find cases where raw sql is executed.<br />
&nbsp;&nbsp;&nbsp;&nbsp;(ii) search your models for &#8220;named_scope&#8221; and check :conditions<br />
&nbsp;&nbsp;B) check for <a href="http://railspikes.com/2008/9/22/is-your-rails-application-safe-from-mass-assignment">mass assignment</a>  Either disable mass assignment as Eric suggests in his article, or audit its use.  If doing an audit, check every model to make sure it declares which attributes are settable with attr_accessible.  (While attr_protected may technically work, a white list approach is recommended by security experts and the <a href="http://groups.google.com/group/rubyonrails-security/browse_thread/thread/42c4d5d3b7354735">rails security advisory</a> on this topic)</p>
<p>2) Scripting attack: search all eRB files for &lt;%= and ensure that if dynamically generated text was originally entered by the user, it is HTML escaped. Consider <a href="http://github.com/nzkoz/rails_xss">rails_xss</a></p>
<p>3) Secure Access: If some of the site does not have public access, check controllers and ensure that public actions are specifically allowed and that protected access is the default</p>
<p>4) search for &#8220;eval&#8221; (whole word) and verify that  usages are safe (assume javascript eval is ok)</p>
<p>5) search for &#8220;forgery&#8221; (not whole word), make sure that<br />
config.action_controller.allow_forgery_protection    = false<br />
is only disabled in test config<br />
 protect_from_forgery should be in the ApplicationController, unless there is a good reason for it not to be</p>
<p>6) check user auth and review that controller actions are limited to expected use</p>
<p>7) passwords: not saved as clear-text in the db, not logged</p>
<p>8) check that private data is not stored in cookies </p>
]]></content:encoded>
			<wfw:commentRss>http://www.ultrasaurus.com/sarahblog/2010/01/rails-security-review-checklist/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>markdown to textile with vim regex</title>
		<link>http://www.ultrasaurus.com/sarahblog/2009/12/markdown-to-textile-with-vim-regex/</link>
		<comments>http://www.ultrasaurus.com/sarahblog/2009/12/markdown-to-textile-with-vim-regex/#comments</comments>
		<pubDate>Wed, 30 Dec 2009 08:36:20 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.ultrasaurus.com/?p=2331</guid>
		<description><![CDATA[So, I needed to change markdown to textile and google didn&#8217;t yield any handy scripts, so I sharpened my vim fu with Rubular, my favorite regular expression tester and came up with a few substitutions that took care of everything but lists and code blocks.
In vi, type ESC to go into command mode, then :%s/one/two/g [...]]]></description>
			<content:encoded><![CDATA[<p>So, I needed to change markdown to textile and google didn&#8217;t yield any handy scripts, so I sharpened my vim fu with <a href="http://www.rubular.com/">Rubular</a>, my favorite regular expression tester and came up with a few substitutions that took care of everything but lists and code blocks.</p>
<p>In vi, type ESC to go into command mode, then :%s/one/two/g will find every instance of &#8220;one&#8221; and replace it with &#8220;two&#8221;</p>
<p>First the easy stuff, headers.  ^ finds the beginning of the line.</p>
<pre>
:%s/^# /h1. /g
:%s/^## /h2. /g
:%s/^### /h3. /g
</pre>
<p>To replace images, I needed to replace ![alt-text](link) with !link! so I needed to capture text.  I suppose I didn&#8217;t really need the first capture, but I was working on the replace expression for a regular link when I realized it would be easier to do the images first.  To understand the expression below, you need to know that \(stuff\) captures some text which can be inserted in the replacement text with \1 and \2, etc.  So to get everything between square brackets, I use [\(.*\)] </p>
<pre>
:%s/!\[\(.*\)](\(.*\))/!\2!/g
</pre>
<p>All of my images appeared on a single line, so I didn&#8217;t catch a potential issue in the above expression until I got to replacing text links.  I needed to use a non &#8220;greedy&#8221; capture so that I wouldn&#8217;t pull in text after the link that happened to include a parenthetical comment.  Normally, in reg ex I would use (.*?) but in vim I needed to write \(.\{-}\) &#8230;wtf?</p>
<pre>
:%s/\[\(.*\)](\(.\{-}\))/"\1":\2/g
</pre>
<p>Special thanks to Adam Wolf&#8217;s tip via <a href="http://www.sharegrove.com">ShareGrove</a> which helped me document these steps.</p>
<blockquote><p>you can put VIM in a mode where the <a href="http://vimdoc.sourceforge.net/htmldoc/cmdline.html#Command-line">command history</a> is just like another buffer. Not in insert mode, try q: </p></blockquote>
<blockquote><p>You should get a new buffer that you can edit with the command history in it, so &#8220;*yy would yank the current line into the system clipboard, etc.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.ultrasaurus.com/sarahblog/2009/12/markdown-to-textile-with-vim-regex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>creating a custom rake task</title>
		<link>http://www.ultrasaurus.com/sarahblog/2009/12/creating-a-custom-rake-task/</link>
		<comments>http://www.ultrasaurus.com/sarahblog/2009/12/creating-a-custom-rake-task/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 17:30:51 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.ultrasaurus.com/?p=2236</guid>
		<description><![CDATA[There&#8217;s a nice Railscast introduction to rake for Rails, which goes into a number of other important details that aren&#8217;t covered in this post.  Below is a little tutorial of creating a Rails rake task and getting it to run remotely on heroku.
Introduction to Rake
In lib/tasks, create a file called greet.rake

task :greet do
  [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a nice <a href="http://railscasts.com/episodes/66-custom-rake-tasks">Railscast introduction to rake</a> for Rails, which goes into a number of other important details that aren&#8217;t covered in this post.  Below is a little tutorial of creating a Rails rake task and getting it to run remotely on heroku.</p>
<h2>Introduction to Rake</h2>
<p>In lib/tasks, create a file called greet.rake</p>
<pre>
task :greet do
   puts "Hello world"
end
</pre>
<p>By naming the task .rake and putting it in this special place rails will automatically pick it up and make it available to you.  You can see it listed if you type: rake -T on the command line.  To run it:</p>
<pre>
rake greet
</pre>
<p>which will print &#8220;Hello world&#8221;</p>
<p>to run one task before another, specify a dependency like this (multiple tasks may be specified in the same file):</p>
<pre>
task :ask => :greet do
   puts "How are you?"
end
</pre>
<h2>Writing a Practical Rake Task</h2>
<p>Now for the task at hand, I&#8217;m going to create a rake task which creates a bunch of fake data for me to test with.  First I&#8217;ll create a little experimental app:</p>
<pre>
rails rake_example
cd rake example
script/generate scaffold person first_name:string last_name:string
rake db:migrate
</pre>
<p>Here&#8217;s the rake task (lib/tasks/fake_people.rake):</p>
<pre>
require 'faker'

namespace :admin  do
  desc "create some fake data"
  task :fake_people => :environment do
    print "How many fake people do you want?"
    num_people = $stdin.gets.to_i
    num_people.times do
      Person.create(:first_name => Faker::Name.first_name,
                    :last_name => Faker::Name.last_name)
    end
    print "#{num_people} created.\n"
  end
end
</pre>
<p>Note that I&#8217;m using the faker gem (<a href="http://faker.rubyforge.org/rdoc/">docs here</a>) and I created a task dependency on loading the rails environment so I could access my Person model.</p>
<p>Now I can run</p>
<pre>
rake admin:fake_people
</pre>
<p>and it will prompt me to ask how many I want and then it will create them.  Cool goodness, yes?</p>
<h2>Running Remotely on Heroku</h2>
<p>We&#8217;re not done yet.  I want to deploy this on heroku and be able to run the task remotely.  For this, there are two gotchas, first I can&#8217;t run an interactive script remotely; also I need to tell heroku that I am using the fake gem and make sure it is installed.</p>
<h3>1) removing interactivity</h3>
<p>Instead of an interactive script, we can set an environment variable or command line argument (thanks to a <a href="http://groups.google.com/group/heroku/browse_thread/thread/775f445e5b11e498/">tip by Adam Wiggins</a>).  </p>
<p>My modified task looks like this:</p>
<pre>
require 'faker'

namespace :admin  do
  desc "create some fake data"
  task :fake_people => :environment do
    num_people = ENV['NUM_RECORDS'].to_i
    num_people.times do
      Person.create(:first_name => Faker::Name.first_name,
                    :last_name => Faker::Name.last_name)
    end
    print "#{num_people} created.\n"
  end
end
</pre>
<p>which I can call locally from the command line like this:</p>
<pre>
rake admin:fake_people NUM_RECORDS=1
</pre>
<h3>2) adding gem to heroku</h3>
<p>I need to create a <a href="http://blog.heroku.com/archives/2009/3/10/gem_manifests/">gems manifest</a>, which sounds fancy, but is simply creating a .gems file at the root of my app with contents similar to what I would put in my config environment.rb to specify that my app requires a gem:</p>
<pre>
faker --version ">=0.3.1"
</pre>
<h3>3) Deploy and Run</h3>
<p>So I can deploy my app to heroku with the usual steps</p>
<pre>
git init
git add .
git commit -m "example app for rake script testing"
heroku create
git push heroku master
heroku rake db:migrate
</pre>
<p>and run the task remotely:</p>
<pre>
heroku rake admin:fake_people NUM_RECORDS=1
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.ultrasaurus.com/sarahblog/2009/12/creating-a-custom-rake-task/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>rails exceptions in xml</title>
		<link>http://www.ultrasaurus.com/sarahblog/2009/09/rails-exceptions-in-xml/</link>
		<comments>http://www.ultrasaurus.com/sarahblog/2009/09/rails-exceptions-in-xml/#comments</comments>
		<pubDate>Sun, 13 Sep 2009 18:13:57 +0000</pubDate>
		<dc:creator>Sarah</dc:creator>
				<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.ultrasaurus.com/?p=2058</guid>
		<description><![CDATA[We ran into an issue last week where our XML APIs were returning HTML under certain error conditions, rather than the expected XML.  Our solution was to add the following code to the ApplicationController:

  rescue_from Exception do &#124;exception&#124;
    respond_to do &#124;format&#124;
      format.xml  { render [...]]]></description>
			<content:encoded><![CDATA[<p>We ran into an issue last week where our XML APIs were returning HTML under certain error conditions, rather than the expected XML.  Our solution was to add the following code to the ApplicationController:</p>
<pre>
  rescue_from Exception do |exception|
    respond_to do |format|
      format.xml  { render :xml =&gt;
           "&lt;error&gt;Internal Server Error #{exception.message}&lt;/error&gt;",
           :status => 500 }
      format.html { render :html =&gt; {:file =&gt; 'public/500.html'}, :status =&gt; 500 }
      format.json { render :json =&gt;
            {:error =&gt; "Internal Server Error #{exception.message}"}.to_json,
             :status => 500 }
    end
  end
</pre>
<p>We might have also declared a rescue_action, and I&#8217;m not sure of the benefits of one over the other, except that perhaps we needed to implement a general form of rescue_from since we had another more specific form already declared.</p>
<p>It seemed to me that this should be the default behavior in rails, so I decided to dig into it a little more and see what I could discover.  I started by making a little test app to reproduce the exception.  The particular case from last week was a database limit that wasn&#8217;t being caught in the app with a length validation.  When I tried to re-create the error in MySql, I noticed that no exception is thrown since MySql will just truncate the data (although perhaps that is only because I am not running MySql in strict mode).  In PostgreSQL, the database layer will throw an exception.</p>
<p>Test app setup:</p>
<pre>
rails -d postgresql test_postgresql
cd test_postgresql/
script/generate scaffold person first:string last:string present:boolean
</pre>
<p>Edit the migration to create a database limit:</p>
<pre>
class CreatePeople &lt; ActiveRecord::Migration
  def self.up
    create_table :people do |t|
      t.string :first, :limit => 40
      t.string :last, :limit => 40
      t.boolean :present

      t.timestamps
    end
  end

  def self.down
    drop_table :people
  end
end
</pre>
<p>Create the postgres user.  Note double-quotes around user, single quotes around password. It has to be that way.  Go figure.</p>
<pre>
$ sudo su postgres -c psql
postgres=# create user "test_postgresql" with superuser password 'password';
CREATE ROLE
postgres=# \q
</pre>
<p>Finally create the database, run migration, and start the server:</p>
<pre>
rake db:create:all
rake db:migrate
./script/server
</pre>
<p>If you point your browser at http://localhost:3000/people and try to create a person with more that 40 characters in the first name, you will see the following error:</p>
<pre>
ActiveRecord::StatementInvalid in PeopleController#create
PGError: ERROR:  value too long for type character varying(40)
</pre>
<p>That is all well and good; however, if you do the same in XML, you will get the same error in <strong>HTML</strong>.</p>
<pre width="80">
$ curl -X POST -d &quot;&lt;person&gt;&lt;first&gt;This is a first name that is too long for the database limit&lt;/first&gt;&lt;/person&gt;&quot; -H &quot;Content-Type: application/xml&quot; http://localhost:3000/people.xml
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
  &lt;title&gt;Action Controller: Exception caught&lt;/title&gt;
  &lt;style&gt;
    body { background-color: #fff; color: #333; }

    body, p, ol, ul, td {
      font-family: verdana, arial, helvetica, sans-serif;
      font-size:   13px;
      line-height: 18px;
    }
</pre>
<p>That seems like a bug to me.  Perhaps this should be a lighthouse ticket rather than a blog post.. still not confident in identifying bugs in Rails, so I figured I&#8217;d post here first.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ultrasaurus.com/sarahblog/2009/09/rails-exceptions-in-xml/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

