2011-06-19 15:26:39 +02:00
|
|
|
describe Rack::Protection::JsonCsrf do
|
|
|
|
it_behaves_like "any rack application"
|
2011-06-20 15:47:58 +02:00
|
|
|
|
2014-02-28 13:34:13 +01:00
|
|
|
module DummyAppWithBody
|
|
|
|
module Closeable
|
|
|
|
def close
|
|
|
|
@closed = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def closed?
|
|
|
|
@closed
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.body
|
|
|
|
@body ||= begin
|
|
|
|
body = ['ok']
|
|
|
|
body.extend(Closeable)
|
|
|
|
body
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.call(env)
|
|
|
|
Thread.current[:last_env] = env
|
|
|
|
[200, {'Content-Type' => 'application/json'}, body]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-20 15:47:58 +02:00
|
|
|
describe 'json response' do
|
|
|
|
before do
|
|
|
|
mock_app { |e| [200, {'Content-Type' => 'application/json'}, []]}
|
|
|
|
end
|
|
|
|
|
|
|
|
it "denies get requests with json responses with a remote referrer" do
|
2014-09-03 01:54:36 +02:00
|
|
|
expect(get('/', {}, 'HTTP_REFERER' => 'http://evil.com')).not_to be_ok
|
2011-06-20 15:47:58 +02:00
|
|
|
end
|
|
|
|
|
2014-02-28 13:34:13 +01:00
|
|
|
it "closes the body returned by the app if it denies the get request" do
|
|
|
|
mock_app DummyAppWithBody do |e|
|
|
|
|
[200, {'Content-Type' => 'application/json'}, []]
|
|
|
|
end
|
|
|
|
|
|
|
|
get('/', {}, 'HTTP_REFERER' => 'http://evil.com')
|
|
|
|
|
2016-07-26 21:05:55 +09:00
|
|
|
expect(DummyAppWithBody.body).to be_closed
|
2014-02-28 13:34:13 +01:00
|
|
|
end
|
|
|
|
|
2017-03-12 23:00:26 -04:00
|
|
|
it "accepts requests with json responses with a remote referrer when allow_if is true" do
|
|
|
|
mock_app do
|
|
|
|
use Rack::Protection::JsonCsrf, :allow_if => lambda{|env| env['HTTP_REFERER'] == 'http://good.com'}
|
|
|
|
run proc { |e| [200, {'Content-Type' => 'application/json'}, []]}
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(get('/', {}, 'HTTP_REFERER' => 'http://good.com')).to be_ok
|
|
|
|
end
|
|
|
|
|
2012-09-05 10:08:09 +02:00
|
|
|
it "accepts requests with json responses with a remote referrer when there's an origin header set" do
|
2014-09-03 01:54:36 +02:00
|
|
|
expect(get('/', {}, 'HTTP_REFERER' => 'http://good.com', 'HTTP_ORIGIN' => 'http://good.com')).to be_ok
|
2012-09-05 10:08:09 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "accepts requests with json responses with a remote referrer when there's an x-origin header set" do
|
2014-09-03 01:54:36 +02:00
|
|
|
expect(get('/', {}, 'HTTP_REFERER' => 'http://good.com', 'HTTP_X_ORIGIN' => 'http://good.com')).to be_ok
|
2012-09-05 10:08:09 +02:00
|
|
|
end
|
|
|
|
|
2011-06-20 15:47:58 +02:00
|
|
|
it "accepts get requests with json responses with a local referrer" do
|
2014-09-03 01:54:36 +02:00
|
|
|
expect(get('/', {}, 'HTTP_REFERER' => '/')).to be_ok
|
2011-06-20 15:47:58 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "accepts get requests with json responses with no referrer" do
|
2014-09-03 01:54:36 +02:00
|
|
|
expect(get('/', {})).to be_ok
|
2011-06-20 15:47:58 +02:00
|
|
|
end
|
2013-03-01 15:43:27 +11:00
|
|
|
|
|
|
|
it "accepts XHR requests" do
|
2014-09-03 01:54:36 +02:00
|
|
|
expect(get('/', {}, 'HTTP_REFERER' => 'http://evil.com', 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')).to be_ok
|
2013-03-01 15:43:27 +11:00
|
|
|
end
|
2013-04-08 11:15:32 +02:00
|
|
|
|
2011-06-20 15:47:58 +02:00
|
|
|
end
|
2011-08-11 09:38:46 -05:00
|
|
|
|
|
|
|
describe 'not json response' do
|
|
|
|
|
|
|
|
it "accepts get requests with 304 headers" do
|
|
|
|
mock_app { |e| [304, {}, []]}
|
2014-09-03 01:54:36 +02:00
|
|
|
expect(get('/', {}).status).to eq(304)
|
2011-08-11 09:38:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2013-04-08 11:15:32 +02:00
|
|
|
|
|
|
|
describe 'with drop_session as default reaction' do
|
2013-10-21 11:38:36 +02:00
|
|
|
it 'still denies' do
|
2013-04-08 11:15:32 +02:00
|
|
|
mock_app do
|
|
|
|
use Rack::Protection, :reaction => :drop_session
|
|
|
|
run proc { |e| [200, {'Content-Type' => 'application/json'}, []]}
|
|
|
|
end
|
|
|
|
|
|
|
|
session = {:foo => :bar}
|
|
|
|
get('/', {}, 'HTTP_REFERER' => 'http://evil.com', 'rack.session' => session)
|
2014-09-03 01:54:36 +02:00
|
|
|
expect(last_response).not_to be_ok
|
2013-04-08 11:15:32 +02:00
|
|
|
end
|
|
|
|
end
|
2011-06-19 15:26:39 +02:00
|
|
|
end
|