Skip to content
Browse files

Added rack-honeypot from sunlightlabs. Commented out the unindentable

stuff because it didn't work.

github.com/sunlightlabs/rack-honeypot
  • Loading branch information...
1 parent 9400abd commit c8a8b844693715f0b545ba59eb8894fc8f36cf5f @mcfiredrill committed Apr 8, 2011
Showing with 86 additions and 2 deletions.
  1. +2 −0 Gemfile
  2. +2 −2 Gemfile.lock
  3. +10 −0 LICENSE.honeypot
  4. +3 −0 config/application.rb
  5. +69 −0 lib/rack/honeypot.rb
View
2 Gemfile
@@ -10,3 +10,5 @@ gem 'paperclip'
gem 'will_paginate', '>=3.0pre2'
gem 'aws-s3', :require => 'aws/s3'
gem 'haml'
+
+#gem 'unindentable'
View
4 Gemfile.lock
@@ -48,10 +48,10 @@ GEM
activerecord
activesupport
polyglot (0.3.1)
- rack (1.2.1)
+ rack (1.2.2)
rack-mount (0.6.13)
rack (>= 1.0.0)
- rack-test (0.5.6)
+ rack-test (0.5.7)
rack (>= 1.0)
rails (3.0.0)
actionmailer (= 3.0.0)
View
10 LICENSE.honeypot
@@ -0,0 +1,10 @@
+Copyright (c) 2009, Sunlight Foundation
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+ * Neither the name of Sunlight Foundation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
3 config/application.rb
@@ -1,6 +1,7 @@
require File.expand_path('../boot', __FILE__)
require 'rails/all'
+require 'lib/rack/honeypot.rb'
# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
@@ -38,5 +39,7 @@ class Application < Rails::Application
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
+
+ config.middleware.use "Rack::Honeypot", :class_name => "funch"
end
end
View
69 lib/rack/honeypot.rb
@@ -0,0 +1,69 @@
+require 'rubygems'
+#require 'unindentable'
+
+module Rack
+ class Honeypot
+ #include Unindentable
+
+ def initialize(app, options={})
+ @app = app
+ @class_name = options[:class_name] || "phonetoy"
+ @label = options[:label] || "Don't fill in this field"
+ @input_name = options[:input_name] || "email"
+ @input_value = options[:input_value] || ""
+ end
+
+ def call(env)
+ if spambot_submission?(Rack::Request.new(env).params)
+ send_to_dead_end
+ else
+ status, headers, response = @app.call(env)
+ new_body = insert_honeypot(build_response_body(response))
+ new_headers = recalculate_body_length(headers, new_body)
+ [status, new_headers, new_body]
+ end
+ end
+
+ def spambot_submission?(form_hash)
+ form_hash && form_hash[@input_name] && form_hash[@input_name] != @input_value
+ end
+
+ def send_to_dead_end
+ [200, {'Content-Type' => 'text/html', "Content-Length" => "0"}, []]
+ end
+
+ def build_response_body(response)
+ response_body = ""
+ response.each { |part| response_body += part }
+ response_body
+ end
+
+ def recalculate_body_length(headers, body)
+ new_headers = headers
+ new_headers["Content-Length"] = body.length.to_s
+ new_headers
+ end
+
+ def insert_honeypot(body)
+ #css = unindent <<-BLOCK
+ css = <<-BLOCK
+ <style type='text/css' media='all'>
+ div.#{@class_name} {
+ display:none;
+ }
+ </style>
+ BLOCK
+ #div = unindent <<-BLOCK
+ div = <<-BLOCK
+ <div class='#{@class_name}'>
+ <label for='#{@input_name}'>#{@label}</label>
+ <input type='text' name='#{@input_name}' value='#{@input_value}'/>
+ </div>
+ BLOCK
+ body.gsub!(/<\/head>/, css + "\n</head>")
+ body.gsub!(/<form(.*)>/, '<form\1>' + "\n" + div)
+ body
+ end
+
+ end
+end

0 comments on commit c8a8b84

Please sign in to comment.
Something went wrong with that request. Please try again.