Skip to content

Commit

Permalink
Merge pull request #31 from ehoch/http_headers
Browse files Browse the repository at this point in the history
spam? should send any HTTP_ parameters except COOKIE to akismet
  • Loading branch information
ehoch committed Dec 10, 2013
2 parents dae1619 + 1637258 commit 2eb477f
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,3 +1,5 @@
= 1.5.0
* Send HTTP_ env varialbes to Akismet [Eric Hochberger]
= 1.4.0
* Allow endpoint to be specified with a proc for multitenant applications [Bradly Feeley]
* Add Akistmet permalink attribute [Eric Hochberger]
Expand Down
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -156,6 +156,15 @@ your app initialization:
config.rakismet.use_middleware = false
```

Additionally, the middleware will send along additional env variables starting with
HTTP_ to Akismet. If you wish to block any sensitive user information, use:

```ruby
config.rakismet.excluded_headers = ['HTTP_COOKIE','HTTP_SENSITIVE']
```

excluded_headers will default to ['HTTP_COOKIE']

Testing
-------

Expand Down
11 changes: 9 additions & 2 deletions lib/rakismet.rb
Expand Up @@ -9,11 +9,15 @@
require 'rakismet/railtie.rb' if defined?(Rails)

module Rakismet
Request = Struct.new(:user_ip, :user_agent, :referrer)
Request = Struct.new(:user_ip, :user_agent, :referrer, :http_headers)
Undefined = Class.new(NameError)

class << self
attr_accessor :key, :url, :host, :proxy_host, :proxy_port, :test
attr_accessor :key, :url, :host, :proxy_host, :proxy_port, :test, :excluded_headers

def excluded_headers
@excluded_headers || ['HTTP_COOKIE']
end

def request
@request ||= Request.new
Expand All @@ -26,6 +30,9 @@ def url
def set_request_vars(env)
request.user_ip, request.user_agent, request.referrer =
env['REMOTE_ADDR'], env['HTTP_USER_AGENT'], env['HTTP_REFERER']

# Collect all CGI-style HTTP_ headers except cookies for privacy..
request.http_headers = env.select { |k,v| k =~ /^HTTP_/ }.reject { |k,v| excluded_headers.include? k }
end

def clear_request
Expand Down
5 changes: 3 additions & 2 deletions lib/rakismet/model.rb
Expand Up @@ -38,7 +38,7 @@ def spam?
if instance_variable_defined? :@_spam
@_spam
else
data = akismet_data
data = akismet_data(true) # Only spam? check should include http_headers
self.akismet_response = Rakismet.akismet_call('comment-check', data)
@_spam = self.akismet_response == 'true'
end
Expand All @@ -56,7 +56,7 @@ def ham!

private

def akismet_data
def akismet_data(include_http_headers = false)
akismet = self.class.akismet_attrs.keys.inject({}) do |data,attr|
mapped_field = self.class.akismet_attrs[attr]
data.merge attr => if mapped_field.is_a?(Proc)
Expand All @@ -76,6 +76,7 @@ def akismet_data
Rakismet.request.send(attr)
end
end
akismet.merge! Rakismet.request.http_headers if include_http_headers and Rakismet.request.http_headers
akismet.delete_if { |k,v| v.nil? || v.empty? }
akismet[:comment_type] ||= 'comment'
akismet
Expand Down
2 changes: 1 addition & 1 deletion lib/rakismet/version.rb
@@ -1,3 +1,3 @@
module Rakismet
VERSION = "1.4.0"
VERSION = "1.5.0"
end
11 changes: 11 additions & 0 deletions spec/models/rakismet_model_spec.rb
Expand Up @@ -38,6 +38,17 @@
@model.spam?
end

it "should send http_headers from Rakismet.request if present" do
Rakismet.stub!(:request).and_return(request_with_headers)
Rakismet.should_receive(:akismet_call).
with('comment-check', akismet_attrs.merge(:user_ip => '127.0.0.1',
:user_agent => 'RSpec',
:referrer => 'http://test.host/referrer',
'HTTP_USER_AGENT' => 'RSpec',
'HTTP_REFERER' => 'http://test.host/referrer'))
@model.spam?
end

it "should cache result of #spam?" do
Rakismet.should_receive(:akismet_call).once
@model.spam?
Expand Down
10 changes: 9 additions & 1 deletion spec/rakismet_middleware_spec.rb
Expand Up @@ -2,7 +2,7 @@

describe Rakismet::Middleware do

let(:env) { { 'REMOTE_ADDR' => '127.0.0.1', 'HTTP_USER_AGENT' => 'RSpec', 'HTTP_REFERER' => 'http://test.host/referrer' } }
let(:env) { { 'REMOTE_ADDR' => '127.0.0.1', 'HTTP_USER_AGENT' => 'RSpec', 'HTTP_REFERER' => 'http://test.host/referrer', 'HTTP_COOKIE' => "Don't violate my privacy" } }
let(:app) { double(:app, :call => nil) }
let(:request) { double(:request).as_null_object }

Expand All @@ -18,10 +18,18 @@
@middleware.call(env)
end

it "should set set Rakismet.request http_headers" do
Rakismet.stub(:request).and_return(request)
request.should_receive(:http_headers=).with({ 'HTTP_USER_AGENT' => 'RSpec', 'HTTP_REFERER' => 'http://test.host/referrer' })
@middleware.call(env)
end

it "should clear Rakismet.request after request is complete" do
@middleware.call(env)
Rakismet.request.user_ip.should be_nil
Rakismet.request.user_agent.should be_nil
Rakismet.request.referrer.should be_nil
Rakismet.request.http_headers.should be_nil
end

end
6 changes: 6 additions & 0 deletions spec/rakismet_spec.rb
Expand Up @@ -82,6 +82,12 @@ def mock_response(body)
Rakismet.validate_key
end
end

describe '.excluded_headers' do
it "should default to ['HTTP_COOKIE']" do
Rakismet.excluded_headers.should eq ['HTTP_COOKIE']
end
end

describe ".akismet_call" do
before do
Expand Down
10 changes: 9 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -27,8 +27,16 @@ def request
:referrer => 'http://test.host/referrer')
end

def request_with_headers
OpenStruct.new(:user_ip => '127.0.0.1',
:user_agent => 'RSpec',
:referrer => 'http://test.host/referrer',
:http_headers => { 'HTTP_USER_AGENT' => 'RSpec', 'HTTP_REFERER' => 'http://test.host/referrer' } )
end

def empty_request
OpenStruct.new(:user_ip => nil,
:user_agent => nil,
:referrer => nil)
:referrer => nil,
:http_headers => nil)
end

5 comments on commit 2eb477f

@ehoch
Copy link
Collaborator Author

@ehoch ehoch commented on 2eb477f Dec 10, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joshfrench Mind doing a 1.5.0 gem release when you get the chance? Akismet looked at how I'm sending headers and approved..

@joshfrench
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's your email? I'll just give you push access on rubygems.org.

@joshfrench
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're all set, let me know if not!

@joshfrench
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(PS, thanks for all your work on Rakismet!)

@ehoch
Copy link
Collaborator Author

@ehoch ehoch commented on 2eb477f Dec 11, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay cool. Will let you know if I run into any problems. And no, thank you for this awesome gem!

Please sign in to comment.