2022-07-31 08:56:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-02-05 07:32:44 -05:00
|
|
|
RSpec.describe Rack::Protection::HttpOrigin do
|
2022-07-31 08:56:44 -04:00
|
|
|
it_behaves_like 'any rack application'
|
2012-01-30 03:57:10 -05:00
|
|
|
|
2012-05-12 11:23:25 -04:00
|
|
|
before(:each) do
|
|
|
|
mock_app do
|
|
|
|
use Rack::Protection::HttpOrigin
|
|
|
|
run DummyApp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-31 08:56:44 -04:00
|
|
|
%w[GET HEAD POST PUT DELETE].each do |method|
|
2012-01-30 03:57:10 -05:00
|
|
|
it "accepts #{method} requests with no Origin" do
|
2014-09-02 19:54:36 -04:00
|
|
|
expect(send(method.downcase, '/')).to be_ok
|
2012-01-30 03:57:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-31 08:56:44 -04:00
|
|
|
%w[GET HEAD].each do |method|
|
2020-07-11 12:01:34 -04:00
|
|
|
it "accepts #{method} requests with non-permitted Origin" do
|
2014-09-02 19:54:36 -04:00
|
|
|
expect(send(method.downcase, '/', {}, 'HTTP_ORIGIN' => 'http://malicious.com')).to be_ok
|
2012-01-30 03:57:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-31 08:56:44 -04:00
|
|
|
%w[GET HEAD POST PUT DELETE].each do |method|
|
2016-05-29 16:25:07 -04:00
|
|
|
it "accepts #{method} requests when allow_if is true" do
|
|
|
|
mock_app do
|
2022-07-31 08:56:44 -04:00
|
|
|
use Rack::Protection::HttpOrigin, allow_if: ->(env) { env.key?('HTTP_ORIGIN') }
|
2016-05-29 16:25:07 -04:00
|
|
|
run DummyApp
|
|
|
|
end
|
|
|
|
expect(send(method.downcase, '/', {}, 'HTTP_ORIGIN' => 'http://any.domain.com')).to be_ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-31 08:56:44 -04:00
|
|
|
%w[POST PUT DELETE].each do |method|
|
2020-07-11 12:01:34 -04:00
|
|
|
it "denies #{method} requests with non-permitted Origin" do
|
2014-09-02 19:54:36 -04:00
|
|
|
expect(send(method.downcase, '/', {}, 'HTTP_ORIGIN' => 'http://malicious.com')).not_to be_ok
|
2012-01-30 03:57:10 -05:00
|
|
|
end
|
|
|
|
|
2020-09-17 22:11:43 -04:00
|
|
|
it "accepts #{method} requests with permitted Origin" do
|
2012-01-30 03:57:10 -05:00
|
|
|
mock_app do
|
2020-07-12 09:54:08 -04:00
|
|
|
use Rack::Protection::HttpOrigin, permitted_origins: ['http://www.friend.com']
|
2012-01-30 03:57:10 -05:00
|
|
|
run DummyApp
|
|
|
|
end
|
2014-09-02 19:54:36 -04:00
|
|
|
expect(send(method.downcase, '/', {}, 'HTTP_ORIGIN' => 'http://www.friend.com')).to be_ok
|
2012-01-30 03:57:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|