<?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>Talk Like A Duck: Danger Will Robinson</title>
    <link>http://talklikeaduck.denhaven2.com/articles/2006/10/05/danger-will-robinson</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>In Ruby, it's not the dog, it's the tricks!</description>
    <item>
      <title>Danger Will Robinson</title>
      <description>&lt;p&gt;Ruby has a nifty method in kernel called open.  It&amp;#8217;s quite powerful in the way it interprets its first argument, a string telling it what to open.  It can open a file, or it can open a pipe to a sub-process it creates to run a command in that string.  It takes quite a bit of &lt;a href="http://www.ruby-doc.org/core/classes/Kernel.html#M001989&gt;documentation&lt;/a&gt; to describe how that first argument is interpreted.&lt;/p&gt;


	&lt;p&gt;This appears to be one of things which Ruby borrowed/stole from perl.  Perl&amp;#8217;s &lt;a href="http://sunsite.ualberta.ca/Documentation/Misc/perl-5.6.1/pod/perlfunc/open.htmlopen"&gt;open function&lt;/a&gt; has a similar interpretation of it&amp;#8217;s first argument. Again it takes quite a bit of documentation to describe.&lt;/p&gt;


	&lt;p&gt;Which is a cause for concern. Most things that powerful can be misused.&lt;/p&gt;


	&lt;p&gt;I use an application called &lt;a href="http://awstats.sourceforge.net/Awstats"&gt;Awstats&lt;/a&gt; to get statistics on my websites.  Awstats is a very popular application which is written in perl.&lt;/p&gt;


	&lt;p&gt;But I long ago disallowed access to Awstats from the outsite world after I found that my system had been compromised by a bad guy &lt;a href="http://www.idefense.com/intelligence/vulnerabilities/display.php?id=185"&gt;exploiting perl&amp;#8217;s open function.&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;Kernel#open, like perl&amp;#8217;s open interprets a path starting with &amp;#8221;|&amp;#8221; as a pipe, and effectively calls IO#popen under the covers, while File#open doesn&amp;#8217;t.&lt;/p&gt;


	&lt;p&gt;This is dangerous if the path argument is coming from a user, say in a web application, because it opens the same security exposure which plagued Awstats, which has had several nasty security bugs because it wasn&amp;#8217;t verifying urls and the nasties were doing things which exploited that like getting it to &amp;#8216;pipe&amp;#8217; to wget to download worms.&lt;/p&gt;


	&lt;p&gt;When I first came across Ruby&amp;#8217;s File#open I was happy to see that IT just treats the name argument as a file path, and chokes if it starts with a &amp;#8221;|&amp;#8221;, and that there was IO#popen which explicitly opens pipes to a command running in a subprocess.&lt;/p&gt;


	&lt;p&gt;I think that it&amp;#8217;s generally better practice in ruby to eschew Kernel#open and use either File#open to open files, and IO#popen to open pipes so that it&amp;#8217;s clear what&amp;#8217;s happening.&lt;/p&gt;


	&lt;p&gt;While the Kernel#open method might be convenient and safe in controlled cases, it seems like it might be the basis of a bad habit when it matters.&lt;/p&gt;</description>
      <pubDate>Thu, 05 Oct 2006 11:23:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:ed94cc5f-09fd-4002-a070-c9de2c3fa7f6</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2006/10/05/danger-will-robinson</link>
      <category>ruby</category>
      <category>perl</category>
      <category>security</category>
      <category>bestpractice</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/45</trackback:ping>
    </item>
  </channel>
</rss>
