Skip to content
This repository has been archived by the owner on May 16, 2021. It is now read-only.

Commit

Permalink
Add a report reaction
Browse files Browse the repository at this point in the history
This reaction does not halt the request, but leaves it up to the
app to react on this information. This allows e.g. frameworks to
ignore failures in certain conditions.
  • Loading branch information
skade committed Mar 10, 2013
1 parent dac9197 commit 4775e79
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/rack/protection/base.rb
Expand Up @@ -11,6 +11,7 @@ class Base
:message => 'Forbidden', :encryptor => Digest::SHA1,
:session_key => 'rack.session', :status => 403,
:allow_empty_referrer => true,
:report_key => "protection.failed",
:html_types => %w[text/html application/xhtml]
}

Expand Down Expand Up @@ -63,6 +64,10 @@ def deny(env)
[options[:status], {'Content-Type' => 'text/plain'}, [options[:message]]]
end

def report(env)
env[options[:report_key]] = true
end

def session?(env)
env.include? options[:session_key]
end
Expand Down
12 changes: 12 additions & 0 deletions spec/protection_spec.rb
Expand Up @@ -18,6 +18,18 @@
session.should be_empty
end

it 'passes errors through if :reaction => :report is used' do
mock_app do
use Rack::Protection, :reaction => :report
run proc { |e| [200, {'Content-Type' => 'text/plain'}, [e["protection.failed"].to_s]] }
end

session = {:foo => :bar}
post('/', {}, 'rack.session' => session, 'HTTP_ORIGIN' => 'http://malicious.com')
last_response.should be_ok
body.should == "true"
end

describe "#html?" do
context "given an appropriate content-type header" do
subject { Rack::Protection::Base.new(nil).html? 'content-type' => "text/html" }
Expand Down

0 comments on commit 4775e79

Please sign in to comment.