tag:blogger.com,1999:blog-4789037338861756332009-02-20T18:40:24.957-08:00Ruby - Scott SchramThe Ruby Language, Ruby on Rails and programming in general.Scott Schramhttp://www.blogger.com/profile/18320914611696742945noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-478903733886175633.post-63337219711703862262008-02-04T14:41:00.001-08:002008-02-04T14:47:47.942-08:00acts_as_list: Don't use ":null => false" for the position columnWhen using the Rails acts_as_list plugin, you must include a<br />"position" column for any model that is using the plugin.<p>In the migration for that model, I had specified the column,<br />preventing it from being null:</p><p><span class="Apple-style-span" style="font-family:'courier new';">t.integer :position, :null => false</span></p><p>When you try to delete an item from the list, it blows up under<br />SQLite3 with this error:</p><p><span class="Apple-style-span" style="font-family: 'courier new';">SQLite3::SQLException: SQL logic error or missing database: UPDATE<br />audios SET "created_at" = '2008-02-04 16:19:47', "product_id" = 2,<br />"position" = NULL, ...</span></p><p>The plugin is removing the item from the list and setting the position<br />to NULL before deleting it, which causes the SQL update because of<br />this code:</p><p><span class="Apple-style-span" style="font-family:'courier new';"> # Removes the item from the list.<br />def remove_from_list<br /> if in_list?<br /> decrement_positions_on_lower_items<br /> update_attribute position_column, nil<br /> end<br />end</span></p><p>It's not a bug, but it is unexpected, because the very next SQL<br />command deletes that row.</p><p>It's just the nature of having the plugin be called in before_destroy:</p><p><span class="Apple-style-span" style="font-family:'courier new';">before_destroy :remove_from_list</span></p><p>By the way, the acts_as_list is a great example for writing your own<br />plugin because it has all the Ruby language tricks required, and is<br />simple to understand.</p><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/478903733886175633-6333721971170386226?l=ruby.schram.net'/></div>Scott Schramhttp://www.blogger.com/profile/18320914611696742945noreply@blogger.com0tag:blogger.com,1999:blog-478903733886175633.post-28571078587464595852008-01-18T07:55:00.001-08:002008-01-20T18:22:28.084-08:00Attackers automating tests for argument modification / injectionI have on one of my web sites a dynamic page that displays an image supplied as an argument within a web page.<br /><br />For example, this will display a page of a musical score in a web page:<br /><br />viewscore.php?name=scores2002/americathebeautiful_3pm_p1.gif<br /><br />Yes, it's written in PHP, but the language doesn't matter...<br /><br />I thought that I was relatively safe from attacks because this is a custom page, not one that is from an open source project that might develop a known weakness.<br /><br />The page is carefully written to:<br /><br />* Only accept the one "name" argument.<br />* To validate it against a regular expression<br />* To check to see that the score really exists on disk<br />* Some other checks.<br /><br />I have it email me when one of these conditions fails. Particularly if the score page doesn't exist, it could mean that I forgot to post it, or put it in the wrong place.<br /><br />Lately I've been getting a lot of emails like this:<br />========<br />Bad Name<br />Score name: "http://sans-packing.ru/img/jipeqap/ehudute/"<br />Visitor IP: 169.244.70.147<br />========<br /><br />When you go visit the URL the page there shows this PHP (and it's not getting executed):<br /><br /><?php echo md5("just_a_test");?><br /><br />The domains seem legitimate, they'll be unwilling hosts for spammer's images at some point.<br /><br />If the attack had succeeded, my site would become the destination of spam emails, ruining my site's reputation.<br /><br />The moral of the story: Even if you're a little niche custom page, you have to assume that you'll be subject to automated probes for weaknesses.<br /><br />Check your arguments and check them again!<br /><br /><span style="font-weight:bold;">Update:</span> Ok, I think I get it. They want to see if my site will execute the code from the remote site, and that's why the remote site code with "just a test" is plain text php. If my site executes it, it will display a code that they can check for.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/478903733886175633-2857107858746459585?l=ruby.schram.net'/></div>Scott Schramhttp://www.blogger.com/profile/18320914611696742945noreply@blogger.com0tag:blogger.com,1999:blog-478903733886175633.post-64779256308174160822007-12-26T08:22:00.001-08:002007-12-26T08:29:22.865-08:00The limitations of Apple's Ruby DistributionThe Ruby 1.8 included with Apple's Leopard is a great way to get<br />started in Ruby and Rails programming with no installation effort. <br /><a href="http://chadfowler.com/2007/10/28/ruby-on-leopard">They did a very nice job</a>, and you can use RubyGems to update Rails and<br />other gems, and even clean up gems that Apple has included as they<br />become obsolete.<p>However, once you get to a certain depth of involvement in Ruby, you<br />will probably want to use your own installation(s) of Ruby and<br />override the version that Apple provides.</p><p>Among the reasons:</p><p>* Apple updates reinstall old gems. For example, Security Update<br />2007-009 v1.1 re-installed updated versions of Rails 1.2 that I had<br />already removed.</p><p>It usually isn't that much of a problem because you can "clean" them<br />again.</p><p><a href="http://blog.segment7.net/articles/2007/12/20/rubygems-1-0-1">I've updated RubyGems to 1.0.1</a>, and I'm not sure what would happen if<br />Apple updated the 0.9.5 version that was included with the Leopard<br />distribution.</p><p>(Since RubyGems is updated using: gem update --system )</p><p>Once I get into serious development, I'd rather have control over when<br />the language, RubyGems, gems, etc. are updated. I hold all of this<br />stable while I change my code so I know where to look when something<br />breaks. The Apple Ruby updates were bundled with other important<br />security updates, so you have to install them if you want to keep the<br />other things secure.</p><p>* Until RubyGems 1.0.1, updating RubyGems would break Apple's two-<br />directory Gem setup.</p><p>* I don't like mixing third party gem executables with the Apple<br />supplied executables in /usr/bin. They should go in /usr/local/bin. <br />Or if using MacPorts, /opt/local/bin. Etc.</p><p>* The now released Ruby 1.9, will require a separate installation. <br /><a href="http://pragdave.blogs.pragprog.com/pragdave/2007/12/ruby-19right-fo.html">It's not ready for production use</a>, but gem authors and Rails core<br />contributors will want to have this early version for testing.</p><p>As 1.9 and the gems (including Rails) that use it reach production<br />quality, I'll want to install it to make sure my own code works.</p><p>Apple will probably not be in a hurry to offer 1.9 as a package. I<br />can't fault them for that, they should offer stable and well tested<br />distributions and let early adopters compile their own.</p><p>For now, I'm happy to use Apple's pre-installed Ruby (and I'm glad you<br />can count on a certain level of Ruby and a set of gems for scripting,<br />too.)</p><p>But, it won't be long before I'm back to compiling my own.</p><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/478903733886175633-6477925630817416082?l=ruby.schram.net'/></div>Scott Schramhttp://www.blogger.com/profile/18320914611696742945noreply@blogger.com0tag:blogger.com,1999:blog-478903733886175633.post-861423206015006592007-12-23T18:58:00.000-08:002007-12-23T19:41:17.697-08:00Converting Mac Address Book vCards Using RubyFor sending out a large number of Christmas emails to family and friends, I wanted to try the commercial <a href="http://campaignmonitor.com/">Campaign Monitor</a> email service.<div><br /><div> </div><div>The email addresses were in Apple's Address book, in a group called "Christmas 2007" and were on OS 10.4 (Tiger).</div><div><br /></div><div> </div><div>Address Book exports vCards, and Campaign Monitor will let you upload a comma delimited file (cdf) like this:</div><div><br /></div><div> </div><div><span class="Apple-style-span" style="font-family:'courier new';">Scott Schram,scott@some-example-domain.foo </span></div><br />First, I exported the Address Book group to a file vCards.vcf and wrote the Ruby program below to convert them to text format.</div><div><br /></div><div>vCards can include more than one email, and even though there's a way to mark which email you prefer in the group in Address Book, that information is not reflected in the exported vCards.</div><div><br /></div><div>So, the program extracts all emails, and for those few vCards that have more than one, I just manually edited the text file to remove the unwanted emails.</div><div><br /></div><div>The vpim gem had errors with the format of the vCards from the 10.4.x Address Book, so I imported them into a 10.5 (Leopard) Address Book, exported them again, and then everything worked fine.</div><div><br /></div><div>Campaign Monitor worked great, too!</div><div><br /></div><div><span class="Apple-style-span" style="font-family:'courier new';">#!/usr/bin/env ruby -v<br /></span></div><div><span class="Apple-style-span" style="font-family:'courier new';">#</span></div><div><span class="Apple-style-span" style="font-family:'courier new';"># Requires gem vpim (0.360)</span></div><div><span class="Apple-style-span" style="font-family:'courier new';">#</span></div><div><div><span class="Apple-style-span" style="font-family:'courier new';">require 'vpim/vcard'</span></div><div><span class="Apple-style-span" style="font-family:'courier new';"><br /></span></div><div><span class="Apple-style-span" style="font-family:'courier new';">infile = File.open("vCards.vcf")</span></div><div><span class="Apple-style-span" style="font-family:'courier new';">cards = Vpim::Vcard.decode(infile)</span></div><div><span class="Apple-style-span" style="font-family:'courier new';"><br /></span></div><div><span class="Apple-style-span" style="font-family:'courier new';">cards.sort_by{ |card| card['n'] }.each do |card|</span></div><div><span class="Apple-style-span" style="font-family:'courier new';"> emails = card.emails.join(" ")</span></div><div><span class="Apple-style-span" style="font-family:'courier new';"> puts "#{card['fn']},#{emails}"</span></div><div><span class="Apple-style-span" style="font-family:'courier new';">end</span></div><div><br /></div></div><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/478903733886175633-86142320601500659?l=ruby.schram.net'/></div>Scott Schramhttp://www.blogger.com/profile/18320914611696742945noreply@blogger.com3tag:blogger.com,1999:blog-478903733886175633.post-70709116007069032212007-12-23T18:28:00.000-08:002007-12-24T06:50:59.380-08:00Ruby and Ruby on Rails Rails Book Reviews on Amazon<div>As I've been learning Ruby and Ruby on Rails, I've written reviews of the books and posted them on Amazon.<br /></div><br /><div>So far, I've reviewed:</div><br /><div><span class="Apple-style-span" style="font-style: italic;">Everyday Scripting with Ruby: For Teams, Testers and You</span> by Brian Marick</div><div><span class="Apple-style-span" style="font-style: italic;">Learning Ruby</span> by Michael Fitzgerald</div><div><span class="Apple-style-span" style="font-style: italic;">Ruby Pocket Reference</span> by Michael Fitzgerald</div><div><span class="Apple-style-span" style="font-style: italic;">Ruby by Example: Concepts and Code</span> by Kevin Baird<br /><div><br /></div><div>So (along with some other stuff) here's a link to <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&location=http%3A%2F%2Fwww.amazon.com%2Fgp%2Fcdp%2Fmember-reviews%2FA2CNKA2M17J2II%2F&tag=schram-20&linkCode=ur2&camp=1789&creative=9325">my Ruby and Ruby on Rails book reviews at Amazon</a><img src="http://www.assoc-amazon.com/e/ir?t=schram-20&l=ur2&o=1" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />.<br /></div></div><div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/478903733886175633-7070911600706903221?l=ruby.schram.net'/></div>Scott Schramhttp://www.blogger.com/profile/18320914611696742945noreply@blogger.com0