<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/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>Tom Moertel's Weblog: Tag safe</title>
    <link>http://blog.moertel.com/articles/tag/safe?tag=safe</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Quality rants on programming theory and stuff geeks like</description>
    <item>
      <title>Google Web Accelerator vs. unsafe linking: Round Two!</title>
      <description>&lt;p&gt;The good folks at &lt;a href="http://37signals.com"&gt;37signals&lt;/a&gt; are once again up
in arms about &lt;a href="http://webaccelerator.google.com/"&gt;Google Web
Accelerator&lt;/a&gt; (GWA).  David
Heinemeier Hansson (DHH), in particular, writes in a recent post to
&lt;a href="http://37signals.com/svn/"&gt;Signal vs. Noise&lt;/a&gt; that &amp;#8220;[GWA] was evil
enough the first time around, but this time it&amp;#8217;s downright scary.&amp;#8221;&lt;/p&gt;


	&lt;p&gt;The problem, it seems, is that &lt;span class="caps"&gt;GWA&lt;/span&gt; automatically, silently, and
unblockably follows hypertext links to web pages that are linked to by
the pages you visit.  It does this in order to cache those pages so
that if you visit them later, it will have cached copies ready in an
instant, thus &amp;#8220;accelerating&amp;#8221; your web surfing.  But some web
developers use hypertext links to trigger potentially unsafe actions,
such as deleting records in a database, and when &lt;span class="caps"&gt;GWA&lt;/span&gt; automatically
follows such links, it triggers the actions.&lt;/p&gt;


	&lt;p&gt;Oops.&lt;/p&gt;


	&lt;h3&gt;Let&amp;#8217;s do the time warp again&amp;#8230;&lt;/h3&gt;


	&lt;p&gt;Now, if this story sounds familiar, that&amp;#8217;s because half a year
ago, the exact same thing happened.  &lt;span class="caps"&gt;GWA&lt;/span&gt; was unveiled to the public.
People started using it.  And some of those people started losing data
from their accounts with popular web applications, such as
37signal&amp;#8217;s own &lt;a href="http://www.backpackit.com/"&gt;Backpack&lt;/a&gt;.  37signals
&lt;a href="http://37signals.com/svn/archives2/google_web_accelerator_hey_not_so_fast_an_alert_for_web_app_designers.php"&gt;publicized the problem in their blog&lt;/a&gt; and &lt;span class="caps"&gt;DHH&lt;/span&gt; even
&lt;a href="http://www.loudthinking.com/arc/000454.html"&gt;called for a recall on &lt;span class="caps"&gt;GWA&lt;/span&gt;&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;And then the community responses came in.  For the most part, the
responses could be divided into two camps, based on who was
blamed for the problem. The first camp blamed the web designers who used
links to trigger unsafe actions (in violation of applicable standards),
and the second camp blamed Google for unleashing &lt;span class="caps"&gt;GWA&lt;/span&gt; upon a web where
standards aren&amp;#8217;t always followed.&lt;/p&gt;


	&lt;p&gt;Both viewpoints had some merit, but I was in the first camp and thus
argued for following the standards and against unsafe linking
practices:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://community.moertel.com/ss/space/start/2005-05-06/1#Google_Web_Accelerator_offers_web_developers_an_important_opportunity"&gt;Google Web Accelerator offers web developers an important opportunity&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://community.moertel.com/ss/space/start/2005-05-08/1"&gt;Taking the unsafe GETs out of Rails&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;What surprised me was that so many people in the second camp argued in
defense of unsafe linking practices, which I had thought indefensible.
I didn&amp;#8217;t have any problem with arguments against Google&amp;#8217;s unleashing
&lt;span class="caps"&gt;GWA&lt;/span&gt; on an imperfect web, but arguing &lt;em&gt;for&lt;/em&gt; the web&amp;#8217;s imperfections
seemed like an odd way of making the case.  The supportive arguments
boiled down to the following:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;Lots of web sites use action-triggering links, so the practice is &lt;em&gt;de facto&lt;/em&gt; acceptable.&lt;/li&gt;
		&lt;li&gt;The existing palette of user-interface options is too limited for today&amp;#8217;s web applications; thus, designers are justified in breaking the rules.&lt;/li&gt;
		&lt;li&gt;The standards don&amp;#8217;t actually prohibit the practice (they say &amp;#8220;SHOULD &lt;span class="caps"&gt;NOT&lt;/span&gt;,&amp;#8221; not &amp;#8220;MUST &lt;span class="caps"&gt;NOT&lt;/span&gt;&amp;#8221;); thus, the practice is allowable.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;None of the arguments seem to withstand scrutiny.  The first argument
breaks down like so: That lots of web sites do it only means that
those sites get away with it, not that the practice is acceptable.
Further, as &lt;span class="caps"&gt;GWA&lt;/span&gt; demonstrates, those sites may not get away with the
practice much longer.&lt;/p&gt;


	&lt;p&gt;The second argument breaks down when one examines the uses of unsafe
linking practices.  Most of them could be replaced by safe practices
through modest UI refactoring.  Given that safe alternatives exist,
the unsafe practices are not justified by virtue of being the only realistic option.&lt;/p&gt;


	&lt;p&gt;The third argument breaks down when one actually reads the relevant
standards.  Then it becomes clear that one should not use links to
trigger potentially unsafe actions.  The wiggle room created
by the use of &amp;#8220;SHOULD &lt;span class="caps"&gt;NOT&lt;/span&gt;&amp;#8221; instead of &amp;#8220;MUST &lt;span class="caps"&gt;NOT&lt;/span&gt;&amp;#8221; does not admit 
the large problems caused by unsafe linking.&lt;/p&gt;


	&lt;p&gt;Finally, even if there were some justification for unsafe linking, the
practice would still be a bad idea: its costs and risks outweigh its
benefits.  Why hold back the potential of efficient caching
architectures for the web?  Why risk data loss for your users?  It&amp;#8217;s
not worth it.&lt;/p&gt;


	&lt;h3&gt;Back to the Future&lt;/h3&gt;


	&lt;p&gt;So where are we now?  Given how little justification there is for
unsafe linking practices, one would hope that we would have abandoned
them by now.  But, as the recent cries about the second coming of &lt;span class="caps"&gt;GWA&lt;/span&gt;
suggest, the web-development community is not yet ready to give up those
sexy, action-triggering links.&lt;/p&gt;


	&lt;p&gt;It&amp;#8217;s not that the means aren&amp;#8217;t available.  Rails, for example, has
plenty of support for sane and safe practices for triggering actions.
Rather, the problem is cultural.  Too many influential people,
especially in the Rails community, are unrepentant users of &amp;#8211; and, dare
I say it, apologists for &amp;#8211; action-triggering links.  Until this changes, I
expect many new web developers to pick up dangerous habits from the
very people they respect most.&lt;/p&gt;


	&lt;p&gt;Fortunately, many other respect-worthy people are pointing toward
a better way:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://www.intertwingly.net/blog/2005/10/24/Theyre-baaaaaaack"&gt;Sam Ruby&lt;/a&gt;: &amp;#8220;I&amp;#8217;m on the other side of this debate. While this appears to be a purely philosophical concern, in reality this stuff matters.&amp;#8221; &lt;/li&gt;
		&lt;li&gt;&lt;a href="http://www.dehora.net/journal/2005/10/hey_im_back.html"&gt;Bill de hÓra&lt;/a&gt;: &amp;#8220;The &lt;span class="caps"&gt;GWA&lt;/span&gt; is back and following &lt;span class="caps"&gt;GET&lt;/span&gt; links again&amp;#8230; The technology itself is interesting insofar as we are going to see more and more highly automated robots enter the web over the next few years&amp;#8230;. Even more interesting is the kind of outrage holding forth in places like Signal v Noise&amp;#8230;.&amp;#8221; &lt;/li&gt;
		&lt;li&gt;&lt;a href="http://bitworking.org/news/Five_months_later_and_web_standards_still_matter"&gt;Joe Gregorio&lt;/a&gt; : &amp;#8220;And now we begin the next chapter in which Pooh discovers that five months after the first time Google turned on &lt;span class="caps"&gt;GWA&lt;/span&gt; that standards still matter.&amp;#8221;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;I hope that this time around the web-development community answers
the wake-up call.  It&amp;#8217;s time to abandon action-triggering links.&lt;/p&gt;</description>
      <pubDate>Tue, 25 Oct 2005 16:12:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:23f7ff724af7f0e12fe123ff3e7362ab</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2005/10/25/google-web-accelerator-vs-unsafe-linking-round-two</link>
      <category>web development</category>
      <category>rails</category>
      <category>rest</category>
      <category>get</category>
      <category>gwa</category>
      <category>safe</category>
      <category>unsafe</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/8</trackback:ping>
    </item>
    <item>
      <title>How to change symlinks atomically</title>
      <description>&lt;p&gt;Many people don&amp;#8217;t realize that changing the target of a symbolic link (symlink) is
not an atomic operation. &amp;#8220;Changing&amp;#8221; a symlink really means deleting it
and creating a new link with the same file name. For example, if I have a
symlink &lt;em&gt;current&lt;/em&gt; that points to a directory &lt;em&gt;old&lt;/em&gt;, and I want to change
it to point to a directory &lt;em&gt;new&lt;/em&gt;, I might use the following command:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;$ ln -snf new current
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Strace shows what really happens when I run the command:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;$ strace ln -snf new current 2&amp;gt;&amp;#38;1 | grep link
unlink("current")         = 0
symlink("new", "current") = 0
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;First, the existing symlink is deleted via the &lt;em&gt;unlink&lt;/em&gt; system
call. Then a new, identically named symlink is created via the &lt;em&gt;symlink&lt;/em&gt;
system call. It&amp;#8217;s a two-step process, and in between the steps, &lt;strong&gt;there
is no symlink.&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;This can be a problem if you expect the symlink to be there always,
such as when using the link to point to the active version of a live
web site. If you change the symlink while deploying a new version of
your site, for example, the web server might try to dereference the
link during the small window of time when it doesn&amp;#8217;t exist. Oops.&lt;/p&gt;


	&lt;p&gt;The solution to this problem is to effect the change by creating a new
symlink and then renaming it over the old symlink. On Unix-like
systems, renaming is an atomic operation, and thus the symlink
&amp;#8220;change&amp;#8221; will be atomic too. By hand, the process looks like this:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;$ ln -s new current_tmp &amp;#38;&amp;#38; mv -Tf current_tmp current
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;In Ruby, I make atomic symlinking available everywhere by extending
the Pathname class with a new method &lt;em&gt;atomic_symlink&lt;/em&gt;:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;pathname&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;

&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Pathname&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;atomic_symlink&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;old&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="ident"&gt;suffix&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="constant"&gt;Array&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;6&lt;/span&gt;&lt;span class="punct"&gt;){&lt;/span&gt;&lt;span class="ident"&gt;rand&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;256&lt;/span&gt;&lt;span class="punct"&gt;).&lt;/span&gt;&lt;span class="ident"&gt;chr&lt;/span&gt;&lt;span class="punct"&gt;}.&lt;/span&gt;&lt;span class="ident"&gt;join&lt;/span&gt;&lt;span class="punct"&gt;].&lt;/span&gt;&lt;span class="ident"&gt;pack&lt;/span&gt;&lt;span class="punct"&gt;(&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;m&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;).&lt;/span&gt;&lt;span class="ident"&gt;strip&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;tr&lt;/span&gt;&lt;span class="punct"&gt;('&lt;/span&gt;&lt;span class="string"&gt;/&lt;/span&gt;&lt;span class="punct"&gt;','&lt;/span&gt;&lt;span class="string"&gt;_&lt;/span&gt;&lt;span class="punct"&gt;');&lt;/span&gt;
    &lt;span class="ident"&gt;tmplink&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;Pathname&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;self&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;to_s&lt;/span&gt; &lt;span class="punct"&gt;+&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;_&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="punct"&gt;+&lt;/span&gt; &lt;span class="ident"&gt;suffix&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="ident"&gt;tmplink&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;make_symlink&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;old&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="keyword"&gt;begin&lt;/span&gt;
      &lt;span class="ident"&gt;tmplink&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;rename&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="constant"&gt;self&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="keyword"&gt;rescue&lt;/span&gt;
      &lt;span class="comment"&gt;# if rename fails, we must remove the temporary link manually&lt;/span&gt;
      &lt;span class="constant"&gt;File&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;unlink&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;tmplink&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;to_s&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
      &lt;span class="keyword"&gt;raise&lt;/span&gt;
    &lt;span class="keyword"&gt;end&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;This code is nothing more than a robustified version of the by-hand
method. It picks better names for temporary links, and it cleans up
after itself, should something go wrong, but otherwise it does the
same thing.&lt;/p&gt;


	&lt;p&gt;Given how easy it is to change symlinks atomically, why do it any
other way? Life is hard enough without having to worry about another
race condition.&lt;/p&gt;</description>
      <pubDate>Mon, 22 Aug 2005 12:00:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:c36fd19bb1406d2022c4d525c6ddb6fb</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2005/08/22/how-to-change-symlinks-atomically</link>
      <category>programming</category>
      <category>ruby</category>
      <category>web development</category>
      <category>ruby</category>
      <category>safe</category>
      <category>symlink</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/49</trackback:ping>
    </item>
    <item>
      <title>The button_to helper is now part of Rails!</title>
      <description>&lt;p&gt;I am delighted to report that the &lt;a href="http://blog.moertel.com/articles/2005/05/08/taking-the-unsafe-gets-out-of-rails"&gt;button_to
helper&lt;/a&gt;
has been added to the &lt;a href="http://www.rubyonrails.com/"&gt;Ruby on Rails&lt;/a&gt;
web-development framework.  &lt;a href="http://www.loudthinking.com/"&gt;David&lt;/a&gt;
applied &lt;a href="http://dev.rubyonrails.org/changeset/1437"&gt;the patch&lt;/a&gt; earlier
today, and so &lt;em&gt;button_to&lt;/em&gt; will be in the much-anticipated Rails 1.0
release.&lt;/p&gt;


	&lt;p&gt;David&amp;#8217;s change-log entry summarizes the patch well:&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;Added &lt;em&gt;button_to&lt;/em&gt; as a form-based solution to deal with harmful
actions that should be hidden behind POSTs. This makes it just as
easy as &lt;em&gt;link_to&lt;/em&gt; to create a safe trigger for actions like destroy,
although it&amp;#8217;s limited by being a block element, the fixed look,
and a no-no inside other forms.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;David does a good job of highlighting the helper&amp;#8217;s limitations. I&amp;#8217;ll
take this opportunity to elaborate on each.&lt;/p&gt;


	&lt;h3&gt;It is a block element&lt;/h3&gt;


	&lt;p&gt;The &lt;em&gt;button_to&lt;/em&gt; helper creates a small form, which in &lt;span class="caps"&gt;HTML&lt;/span&gt; is considered
&lt;a href="http://www.w3.org/TR/html4/sgml/dtd.html#block"&gt;block content&lt;/a&gt;, just
like the &lt;em&gt;p&lt;/em&gt;, &lt;em&gt;div&lt;/em&gt;, and &lt;em&gt;blockquote&lt;/em&gt; elements are. Basically, block
content cannot be mixed into runs of text. But links can: links are
&lt;a href="http://www.w3.org/TR/html4/sgml/dtd.html#inline"&gt;inline content&lt;/a&gt;. Thus
&lt;em&gt;button_to&lt;/em&gt; cannot be used as a drop-in replacement for every
occurrence of &lt;em&gt;link_to&lt;/em&gt; that might be unsafe; it works only for those
occurrences within block-accepting contexts.&lt;/p&gt;


	&lt;p&gt;Luckily for us, when designers use links to trigger unsafe actions,
they rarely slip such links into the middle of ordinary looking
text. Naughty uses of &lt;em&gt;link_to&lt;/em&gt; almost always occur within contexts
that accept block content. In Rails-generated scaffolding code, for
instance, the unsafe uses of &lt;em&gt;link_to&lt;/em&gt; occur within table cells, and
table cells have a &lt;a href="http://www.w3.org/TR/html4/sgml/dtd.html#flow"&gt;flow content
model&lt;/a&gt;, which accepts
both inline and block content. So &lt;em&gt;button_to&lt;/em&gt; works great for the
default cases in Rails.&lt;/p&gt;


	&lt;h3&gt;It has a fixed look&lt;/h3&gt;


	&lt;p&gt;As its name implies, &lt;em&gt;button_to&lt;/em&gt; creates buttons. Buttons don&amp;#8217;t look
like links and aren&amp;#8217;t styled the same way that links are. For some
design scenarios, this might be a problem.&lt;/p&gt;


	&lt;p&gt;(My view is that links should not be used to trigger unsafe
actions. In the same way that action-triggering &lt;span class="caps"&gt;GET&lt;/span&gt; requests violate
the spirit of the &lt;span class="caps"&gt;HTTP&lt;/span&gt; standards, action-triggering hypertext links
violate the spirit of the &lt;span class="caps"&gt;HTML&lt;/span&gt; standards. For this reason, I view this
limitation as a feature.)&lt;/p&gt;


	&lt;h3&gt;It is a no-no inside other forms&lt;/h3&gt;


	&lt;p&gt;Forms cannot be nested, and so &lt;em&gt;button_to&lt;/em&gt; cannot be used inside of
forms.&lt;/p&gt;


	&lt;p&gt;Fortunately, this limitation usually doesn&amp;#8217;t matter because when we
are inside of a form, we can use its buttons instead of
&lt;em&gt;button_to&lt;/em&gt;-created buttons to trigger actions. Still, there are some
circumstances where it does matter, such as the &amp;#8220;Amazon.com wish list&amp;#8221; 
scenario. In this scenario, we should consider &lt;a href="http://blog.moertel.com/articles/2005/05/08/taking-the-unsafe-gets-out-of-rails#comment-110"&gt;other
options&lt;/a&gt;.&lt;/p&gt;


	&lt;h3&gt;The bottom line: Pick the low-hanging fruit&lt;/h3&gt;


	&lt;p&gt;While &lt;em&gt;button_to&lt;/em&gt; has its limitations, it does provide a simple solution
to the unsafe-GET problem for most real-world cases. I am glad that it
is now a part of Rails, and I offer a big thank-you to David for
accepting the patch.&lt;/p&gt;</description>
      <pubDate>Thu, 16 Jun 2005 12:00:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:29f013421357d69f430f725d21062807</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2005/06/16/the-button_to-helper-is-now-part-of-rails</link>
      <category>web development</category>
      <category>rails</category>
      <category>rails</category>
      <category>get</category>
      <category>gwa</category>
      <category>safe</category>
      <category>unsafe</category>
      <category>post</category>
      <category>link_to</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/46</trackback:ping>
    </item>
    <item>
      <title>Taking the unsafe GETs out of Rails</title>
      <description>&lt;div class="update"&gt;

	&lt;p&gt;&lt;em&gt;Update 2005-06-17:&lt;/em&gt; The &lt;em&gt;button_to&lt;/em&gt; helper, introduced below, has been incorporated into the Rails framework and will be a part of the Rails 1.0 release. See &lt;a href="http://community.moertel.com/ss/space/start/2005-06-16/1"&gt;Good news: The button_to helper is now part of Rails!&lt;/a&gt; for more.&lt;/p&gt;


	&lt;p&gt;&lt;em&gt;Update 2005-05-28:&lt;/em&gt; I now have &lt;a href="http://community.moertel.com/ss/space/start/2005-05-08/1/button_to.rb"&gt;a more-recent version of the &lt;em&gt;button_to&lt;/em&gt; code&lt;/a&gt;, which adds support for the disabled &lt;span class="caps"&gt;HTML&lt;/span&gt; attribute. Thanks to Sean T Allen for the great idea and initial implementation.&lt;/p&gt;


&lt;/div&gt;

	&lt;p&gt;As I &lt;a href="http://community.moertel.com/ss/space/start/2005-05-06/1#Google_Web_Accelerator_offers_web_developers_an_important_opportunity"&gt;wrote earlier&lt;/a&gt;, it&amp;#8217;s time for web developers to do away with the fundamentally broken practice of using hypertext links to trigger dangerous events such as deleting things. One of the first places we ought to clean house is in the burgeoning &lt;a href="http://www.rubyonrails.org/"&gt;Rails web-application framework&lt;/a&gt;, where this practice is pervasive.&lt;/p&gt;


	&lt;p&gt;The primary culprit in Rails is the all-too-easy &lt;em&gt;link_to&lt;/em&gt; method, which is (presently) the orthodox means of creating links to any action, even unsafe ones. For example:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;link_to "Destroy", :controller =&amp;gt; 'accounts',
        :action =&amp;gt; 'destroy', :id =&amp;gt; 6
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The above code generates the following &lt;span class="caps"&gt;HTML&lt;/span&gt; hypertext link, which when followed will merrily delete account number 6:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;&amp;lt;a href="/accounts/destroy/6"&amp;gt;Destroy&amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Because this practice is dangerous and contrary to the decade-old convention that links be safe, the &lt;em&gt;link_to&lt;/em&gt; method thoughtfully lets us request that a Javascript confirmation dialog be tacked onto the link for added protection:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;link_to "Destroy", ...,  :confirm =&amp;gt; "Are you sure?" 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The resulting &amp;#8220;safe&amp;#8221; &lt;span class="caps"&gt;HTML&lt;/span&gt;:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;&amp;lt;a href="/accounts/destroy/6" 
   onclick="return confirm('Are you sure?');"&amp;gt;Destroy&amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Unfortunately, the Javascript protection doesn&amp;#8217;t work. First, not all web browsers care about it. Lots of people surf with Javascript turned off. Second, a whole slew of things besides web browsers live on the Internet, and almost all of them are oblivious to Javascript. Web crawlers fall into this category. They will be more than happy to follow any link you feed to them. &amp;#8220;Hey, Googlebot just deleted every account in our database!&amp;#8221; Oops.&lt;/p&gt;


	&lt;p&gt;Thus another layer of protection is commonly used: authorization. The theory is that dangerous links can be safely corralled in the private parts of a web application, where the public and web crawlers cannot go. Only authorized users can get into those parts, and those users will be smart enough not to click on the truly dangerous links unless they really mean it.&lt;/p&gt;


	&lt;p&gt;The problem is, any number of intermediary agents can be operating &lt;em&gt;on behalf of an authorized user&lt;/em&gt;, and these agents are free to do anything the user is allowed to do, such as follow dangerous links. Google&amp;#8217;s Web Accelerator is one such agent. It tries to make your surfing faster by (among other things) pre-fetching the resources that are linked to on the pages you visit. And what happens if you, an authorized user, visit a page containing dangerous links? That&amp;#8217;s right, Web Accelerator will fetch the &amp;#8220;resources&amp;#8221; those links point to &amp;#8211; and delete a bunch of your stuff.&lt;/p&gt;


	&lt;p&gt;I hope by this point that I have argued convincingly that using links for unsafe actions is a bad idea. Even if you feel justified in ignoring the applicable parts of the &lt;span class="caps"&gt;HTTP&lt;/span&gt; RFCs, it&amp;#8217;s a bad idea. Even if you tack on Javascript confirmations and hide your links in authorization-protected zones of your site, it&amp;#8217;s a bad idea. It is, all around, a bad idea. Don&amp;#8217;t do it.&lt;/p&gt;


	&lt;p&gt;So what alternatives are there?  Read on for one possibility, &lt;em&gt;button_to&lt;/em&gt;.&lt;/p&gt;&lt;h3&gt;A &lt;em&gt;link_to&lt;/em&gt; alternative: &lt;em&gt;button_to&lt;/em&gt;&lt;/h3&gt;


	&lt;p&gt;If you shouldn&amp;#8217;t use links for unsafe actions, what should you use instead? Form buttons. Forms can be submitted via &lt;span class="caps"&gt;HTTP POST&lt;/span&gt; requests, and &lt;span class="caps"&gt;POST&lt;/span&gt; requests are understood to do potentially unsafe things. Web crawlers will not try to click your buttons. Intermediary user agents will not try to pre-submit your forms.&lt;/p&gt;


	&lt;p&gt;So, how do we make doing the right thing as easy as creating a link? My answer is 
&lt;em&gt;button_to&lt;/em&gt;, a method that takes the same parameters as the ever-popular &lt;em&gt;link_to&lt;/em&gt; but creates a tiny form that contains a single button instead of a link:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;button_to "Destroy", { :action =&amp;gt; 'destroy', :id =&amp;gt; 6 },
          :confirm =&amp;gt; "Are you sure?" 
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The resulting &lt;span class="caps"&gt;HTML &lt;/span&gt;(reformatted for your viewing pleasure):&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;&amp;lt;form method="post" action="/accounts/destroy/6" class="button-to"&amp;gt;
  &amp;lt;div&amp;gt;&amp;lt;input onclick="return confirm('Are you sure?');" 
              value="Destroy" type="submit"&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The forms I create are given the class &lt;em&gt;button-to&lt;/em&gt;, which makes it easy to apply styles to them. With a little work, the buttons can look pretty darn good:&lt;/p&gt;


	&lt;p&gt;&lt;img src="http://community.moertel.com/ss/space/start/2005-05-08/1/button_to.png" title="screen capture showing Edit and Destroy buttons created with the button_to method" alt="screen capture showing Edit and Destroy buttons created with the button_to method" /&gt;&lt;/p&gt;


	&lt;p&gt;So that&amp;#8217;s my plea: Use a button. It&amp;#8217;s a simple solution to a potentially ugly problem. There&amp;#8217;s no need for Ajax or other non-portable Javascript trickery. Just use a button.&lt;/p&gt;


	&lt;p&gt;And it&amp;#8217;s easy, too. In a few minutes, I was able to &amp;#8220;clean house&amp;#8221; on the Rails application I&amp;#8217;m developing.&lt;/p&gt;


	&lt;h3&gt;The code&lt;/h3&gt;


	&lt;p&gt;If you&amp;#8217;re interested, here&amp;#8217;s the code for &lt;em&gt;button_to&lt;/em&gt;. It&amp;#8217;s only ten lines, but the docs make it look much longer.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;# Generates a form containing a sole button that submits to the URL
# given by _options_.  Use this method instead of +link_to+ for
# dangerous actions that do not have the safe HTTP GET semantics
# implied by using a hypertext link.
#
# The parameters are the same as for +url_to+.  Any _html_options_
# that you pass will be applied to the inner +input+ element.  The
# generated form element is given the class 'button-to', to which
# you can attach CSS styles for display purposes.
#
# Example 1:
#
#   # inside of controller 'feeds'
#   button_to "Edit", :action =&amp;gt; 'edit', :id =&amp;gt; 3
#
# Generates the following HTML (sans formatting):
#
#   &amp;lt;form method="post" action="/feeds/edit/3" class="button-to"&amp;gt;
#     &amp;lt;div&amp;gt;&amp;lt;input value="Edit" type="submit"&amp;gt;&amp;lt;/div&amp;gt;
#   &amp;lt;/form&amp;gt;
#
# Example 2:
#
#   button_to "Destroy", { :action =&amp;gt; 'destroy', :id =&amp;gt; 3 },
#             :confirm =&amp;gt; "Are you sure?" 
#
# Generates the following HTML (sans formatting):
#
#   &amp;lt;form method="post" action="/feeds/destroy/3" class="button-to"&amp;gt;
#     &amp;lt;div&amp;gt;&amp;lt;input onclick="return confirm('Are you sure?');" 
#                 value="Destroy" type="submit"&amp;gt;
#     &amp;lt;/div&amp;gt;
#   &amp;lt;/form&amp;gt;
# 
# *NOTE*: This method generates HTML code that represents a form.
# Forms are "block" content, which means that you should not try to
# insert them into your HTML where only inline content is expected.
# For example, you can legally insert a form inside of a +div+ or +td+
# element or in between +p+ elements, but not in the middle of a run
# of text.  (Bottom line:  Always validate your HTML before going
# public, especially if this paragraph seems confusing.)

def button_to(name, options = {}, html_options = nil)
  html_options = (html_options || {}).stringify_keys
  convert_confirm_option_to_javascript!(html_options)
  url, name = options.is_a?(String) ? 
    [ options,  name || options ] :
    [ url_for(options), name || url_for(options) ]
  html_options.merge!("type" =&amp;gt; "submit", "value" =&amp;gt; name)
  "&amp;lt;form method='post' action='#{h url}' class='button-to'&amp;gt;&amp;lt;div&amp;gt;" +
    tag("input", html_options) + "&amp;lt;/div&amp;gt;&amp;lt;/form&amp;gt;" 
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Thanks for reading and happy unsafe-link hunting!&lt;/p&gt;</description>
      <pubDate>Sun, 08 May 2005 12:00:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:2e55c892774276d8ec6a19d9921e39d7</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2005/05/08/taking-the-unsafe-gets-out-of-rails</link>
      <category>ruby</category>
      <category>web development</category>
      <category>rails</category>
      <category>rails</category>
      <category>get</category>
      <category>gwa</category>
      <category>safe</category>
      <category>unsafe</category>
      <category>post</category>
      <category>link_to</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/41</trackback:ping>
    </item>
    <item>
      <title>Google Web Accelerator offers web developers an important opportunity</title>
      <description>&lt;p&gt;Google Web Accelerator offers web developers an important opportunity
There has been a lot of heat lately about &lt;a href="http://webaccelerator.google.com/"&gt;Google&amp;#8217;s Web Accelerator&lt;/a&gt; (GWA)
&lt;a href="http://www.37signals.com/svn/archives2/google_web_accelerator_hey_not_so_fast_an_alert_for_web_app_designers.php"&gt;exposing serious problems&lt;/a&gt; in some popular web
applications. The problem, in short, is that these applications use
&lt;span class="caps"&gt;GET&lt;/span&gt;-based links that when followed perform dangerous actions such as
deleting records in a database. According to web standards going back
a decade, that is a no-no: Links should be safe to follow. Thus &lt;span class="caps"&gt;GWA&lt;/span&gt;,
expecting links to be safe, tries to help you out by pre-fetching
various resources that are linked to by the pages you
visit. Unfortunately, if the page you happen to be visiting contains
lots of dangerous links, &lt;span class="caps"&gt;GWA&lt;/span&gt; will innocently try to pre-fetch the
&amp;#8220;resources&amp;#8221; that the links point to, and in doing so will accidentally
delete a bunch of stuff. Oops.&lt;/p&gt;


	&lt;p&gt;That&amp;#8217;s the backdrop for our real story, which is the response from the
community of web developers. What I find fascinating, and somewhat
disheartening, is the number of people who say the problem is Google&amp;#8217;s
to fix. Yes, there are a lot of broken web apps out there, and Google
could have been smarter about working around the minefields those apps
represent. But that&amp;#8217;s a side problem. The real problem is that there
are a lot of broken web apps out there, and they &lt;em&gt;do&lt;/em&gt; represent
minefields. Worse, a lot of web developers think it is acceptable to
brush aside fundamental conventions of the web going back a &lt;em&gt;decade&lt;/em&gt;
when they find it sexier to use &lt;span class="caps"&gt;GET&lt;/span&gt; instead of &lt;span class="caps"&gt;POST&lt;/span&gt;.&lt;/p&gt;


	&lt;p&gt;What these developers overlook is that the web is not a bunch of
colorful pages with buttons, clickable links, and pretty
pictures. Rather the web is a distributed collection of hypertext
documents, each of which has a meaning that is given by standards that
most people have agreed to follow. While the collection may &lt;em&gt;look&lt;/em&gt; like
a bunch of colorful pages &lt;em&gt;in one particular visual presentation&lt;/em&gt;, it
really, truly is not.&lt;/p&gt;


	&lt;p&gt;Nevertheless, many web applications are designed with the prevailing
mindset that the meaning of the web is nothing more than how it looks
and behaves in a web browser. Even if those web applications are not
intended for use outside of a few approved browsers &amp;#8211; the escape
hatch that is often used to justify departures from the
standards &amp;#8211; this mindset is wrong.&lt;/p&gt;


	&lt;p&gt;The reason it is wrong is because, like it or not, web applications
are implemented in terms of protocols and languages that &lt;em&gt;mean
something&lt;/em&gt;. The bits and pieces of a web application each &lt;em&gt;mean
something&lt;/em&gt;, even if what they mean is not in harmony with the
designer&amp;#8217;s overall vision. What a web designer may see as a bold, red
link that says &amp;#8220;delete&amp;#8221; and is protected by a Javascript confirmation
dialog, is actually a hypertext reference &amp;#8211; a pointer
to another resource that the standards say should not be dangerous to
follow. Thus there is a fundamental mismatch between what the designer
is trying to say &amp;#8211; &amp;#8220;follow this link only if you
really, no-kidding want to delete something&amp;#8221; &amp;#8211; and
what his markup actually &lt;em&gt;means&lt;/em&gt;: &amp;#8220;this link is safe to follow.&amp;#8221;&lt;/p&gt;


	&lt;p&gt;The bottom line is that hypertext links are supposed to be
safe. Dangerous links can&amp;#8217;t be made &amp;#8220;safe&amp;#8221; by layering tricks on
them. Wrapping them with Javascript confirmation dialogs doesn&amp;#8217;t make
them safe. This trick fails fundamentally because the meaning of a
link doesn&amp;#8217;t change when there is Javascript associated with it. It
fails practically because not all consumers of hypertext evaluate
Javascript, nor do any standards suggest that they should. (This is
also why client-side validation cannot be trusted on the server side.)&lt;/p&gt;


	&lt;p&gt;Hiding dangerous links in authorization-controlled portions of a web
application does not make them safe, either. This trick might shield
the links from external spiders, but the standards allow for any
number of intermediary agents (such as Google&amp;#8217;s Web Accelerator) to
work &lt;em&gt;on behalf of an authorized user.&lt;/em&gt; Anything the user is authorized
to do, so are the user&amp;#8217;s agents. If the user can click the &amp;#8220;delete&amp;#8221; 
link, so can the agents.&lt;/p&gt;


	&lt;h4&gt;Let&amp;#8217;s answer the wake-up call.&lt;/h4&gt;


	&lt;p&gt;&lt;span class="caps"&gt;GWA&lt;/span&gt; is only the first of a new breed of
smarter user agents that promise to make the web a better place for
all of us. If you&amp;#8217;re a web developer, take the slew of problems that
&lt;span class="caps"&gt;GWA&lt;/span&gt; uncovered as a wake-up call. Even if Google works around your
problems, other user agents may not. As developers, it&amp;#8217;s time to admit
our mistakes and fix the stupid things that our web applications do.&lt;/p&gt;


	&lt;p&gt;First, let&amp;#8217;s drive a stake in the notion that dangerous links are OK
if we&amp;#8217;re &amp;#8220;careful.&amp;#8221; They&amp;#8217;re not. Dangerous links are
lazy. &amp;#8220;Confirming&amp;#8221; them with Javascript or &amp;#8220;hiding&amp;#8221; them behind
authorization doesn&amp;#8217;t work.&lt;/p&gt;


	&lt;p&gt;Second, let&amp;#8217;s clean house. Let&amp;#8217;s find those places where we have laced
our web applications with dangerous links and remove them. Break out
the forms! Long live the &lt;span class="caps"&gt;POST&lt;/span&gt;!&lt;/p&gt;


	&lt;p&gt;Third, let&amp;#8217;s take a look at how we got into this mess and try to learn
from our mistakes. Do we value sexiness more than substance? We like
to think that form follows function and that good design and good
implementation go hand in hand, but the reality of this debacle
suggests otherwise. I think the truth is that on the web, sexiness and
hype are what gets attention, and we seek attention more than we like
to admit.&lt;/p&gt;


	&lt;p&gt;Maybe the best thing for us is a good dose of humility. And, come to
think of it, that&amp;#8217;s just what Google&amp;#8217;s Web Accelerator offered us.&lt;/p&gt;</description>
      <pubDate>Fri, 06 May 2005 21:55:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:e542d7e0-94c6-4a4e-a41e-2b7ea7de7494</guid>
      <author>Tom Moertel</author>
      <link>http://blog.moertel.com/articles/2005/05/06/google-web-accelerator-offers-web-developers-an-important-opportunity</link>
      <category>web development</category>
      <category>rails</category>
      <category>get</category>
      <category>gwa</category>
      <category>safe</category>
      <category>unsafe</category>
      <category>post</category>
      <trackback:ping>http://blog.moertel.com/articles/trackback/210</trackback:ping>
    </item>
  </channel>
</rss>
