Negative Captcha Plugin
What is a Negative Captcha?
A negative captcha has the exact same purpose as your run-of-the-mill image captcha: To keep bots from submitting forms. Image (“positive”) captchas do this by implementing a step which only humans can do, but bots cannot: read jumbled characters from an image. But this is bad. It creates usability problems, it hurts conversion rates, and it confuses the shit out of lots of people. Why not do it the other way around? Negative captchas create a form that has tasks that only bots can perform, but humans cannot. This has the exact same effect, with (anecdotally) a much lower false positive identification rate when compared with positive captchas. All of this comes without making humans go through any extra trouble to submit the form. It really is win-win.
How does a Negative Captcha Work?
In a negative captcha form there are two main parts and three ancillary parts. I’ll explain them thusly.
Honeypots are form fields which look exactly like real form fields. Bots will see them and fill them out. Humans cannot see them and thusly will not fill them out. They are hidden indirectly- usually by positioning them off to the left of the browser. They look kind of like this:
<div style="position: absolute; left: -2000px;"><input type="text" name="name" value="" /></div>
These fields are the ones humans will see, will subsequently fill out, and that you’ll pull your real form data out of. The form name will be hashed so that bots will not know what it is. They look kind of like this:
<input type="text" name="685966bd3a1975667b4777cc56188c7e" />
This is a field that is used in the hash key to make the hash different on every GET request, and to prevent replayability.
This is a rotating key that is used in the hash method to prevent replayability. I’m not sold on its usefulness.
This is simply some key that is used in the hashing method to prevent bots from backing out the name of the field from the hashed field name.
How does the Negative Captcha Plugin Work?
Install the plugin
script/plugin install http://code.subwindow.com/negative_captcha
git submodule add git://github.com/subwindow/negative-captcha.git vendor/plugins/negative_captcha
Place this before filter at the top of the controller you are protecting:
before_filter :setup_negative_captcha, :only => [:new, :create]
In the same controller include the following private method:
private def setup_negative_captcha @captcha = NegativeCaptcha.new( :secret => NEGATIVE_CAPTCHA_SECRET, #A secret key entered in environment.rb. 'rake secret' will give you a good one. :spinner => request.remote_ip, :fields => [:name, :email, :body], #Whatever fields are in your form :params => params) end
Modify your POST action(s) to check for the validity of the negative captcha form
def create @comment = Comment.new(@captcha.values) #Decrypted params if @captcha.valid? && @comment.save redirect_to @comment else flash[:notice] = @captcha.error if @captcha.error render :action => 'new' end end
Modify your form to include the honeypots and other fields. You can probably leave any select, radio, and check box fields alone. The text field/text area helpers should be sufficient.
<% form_tag comments_path do -%> <%= negative_captcha(@captcha) %> <ul class="contact_us"> <li> <label>Name:</label> <%= negative_text_field_tag @captcha, :name %> </li> <li> <label>Email:</label> <%= negative_text_field_tag @captcha, :email %> </li> <li> <label>Your Comment:</label> <%= negative_text_area_tag @captcha, :body %> </li> <li> <%= submit_tag %> </li> </ul> <% end -%>
Test and enjoy!
Possible Gotchas and other concerns
- It is still possible for someone to write a bot to exploit a single site by closely examining the DOM. This means that if you are Yahoo, Google or Facebook, negative captchas will not be a complete solution. But if you have a small application, negative captchas will likely be a very, very good solution for you. There are no easy work-arounds to this quite yet. Let me know if you have one.
- I’m not a genius. It is possible that a bot can figure out the hashed values and determine which forms are which. I don’t know how, but I think they might be able to. I welcome people who have thought this out more thoroughly to criticize this method and help me find solutions. I like this idea a lot and want it to succeed.
The idea of a negative captcha is not mine. It originates (I think) from Damien Katz of CouchDB. I (Erik Peterson) wrote the plugin. Calvin Yu wrote the original class which I refactored quite a bit and made into the plugin.
If you have any questions, concerns or have found any bugs, please email me at firstname.lastname@example.org
If you’d like to help improve or refactor this plugin, please create a fork on Github and let me know about it. http://github.com/subwindow/negative-captcha