2017-07-16 13:10:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:15:15 -04:00
|
|
|
require "test_helper"
|
|
|
|
require "stubs/test_server"
|
2015-10-07 12:24:20 -04:00
|
|
|
|
2015-10-15 22:11:49 -04:00
|
|
|
class ActionCable::Connection::CrossSiteForgeryTest < ActionCable::TestCase
|
2016-08-06 13:15:15 -04:00
|
|
|
HOST = "rubyonrails.com"
|
2015-10-07 12:24:20 -04:00
|
|
|
|
2015-10-15 22:11:49 -04:00
|
|
|
class Connection < ActionCable::Connection::Base
|
|
|
|
def send_async(method, *args)
|
|
|
|
send method, *args
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-07 12:24:20 -04:00
|
|
|
setup do
|
|
|
|
@server = TestServer.new
|
2015-10-12 19:14:14 -04:00
|
|
|
@server.config.allowed_request_origins = %w( http://rubyonrails.com )
|
2016-10-10 22:21:10 -04:00
|
|
|
@server.config.allow_same_origin_as_host = false
|
2015-10-07 12:24:20 -04:00
|
|
|
end
|
|
|
|
|
2015-10-12 19:14:14 -04:00
|
|
|
teardown do
|
|
|
|
@server.config.disable_request_forgery_protection = false
|
|
|
|
@server.config.allowed_request_origins = []
|
2016-10-10 22:21:10 -04:00
|
|
|
@server.config.allow_same_origin_as_host = true
|
2015-10-07 12:24:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "disable forgery protection" do
|
|
|
|
@server.config.disable_request_forgery_protection = true
|
2016-08-06 13:15:15 -04:00
|
|
|
assert_origin_allowed "http://rubyonrails.com"
|
|
|
|
assert_origin_allowed "http://hax.com"
|
2015-10-07 12:24:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "explicitly specified a single allowed origin" do
|
2016-08-06 13:15:15 -04:00
|
|
|
@server.config.allowed_request_origins = "http://hax.com"
|
|
|
|
assert_origin_not_allowed "http://rubyonrails.com"
|
|
|
|
assert_origin_allowed "http://hax.com"
|
2015-10-07 12:24:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "explicitly specified multiple allowed origins" do
|
2015-10-12 19:14:14 -04:00
|
|
|
@server.config.allowed_request_origins = %w( http://rubyonrails.com http://www.rubyonrails.com )
|
2016-08-06 13:15:15 -04:00
|
|
|
assert_origin_allowed "http://rubyonrails.com"
|
|
|
|
assert_origin_allowed "http://www.rubyonrails.com"
|
|
|
|
assert_origin_not_allowed "http://hax.com"
|
2015-10-07 12:24:20 -04:00
|
|
|
end
|
|
|
|
|
2015-12-05 16:58:31 -05:00
|
|
|
test "explicitly specified a single regexp allowed origin" do
|
|
|
|
@server.config.allowed_request_origins = /.*ha.*/
|
2016-08-06 13:15:15 -04:00
|
|
|
assert_origin_not_allowed "http://rubyonrails.com"
|
|
|
|
assert_origin_allowed "http://hax.com"
|
2015-12-05 16:58:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "explicitly specified multiple regexp allowed origins" do
|
2016-08-06 13:15:15 -04:00
|
|
|
@server.config.allowed_request_origins = [/http:\/\/ruby.*/, /.*rai.s.*com/, "string" ]
|
|
|
|
assert_origin_allowed "http://rubyonrails.com"
|
|
|
|
assert_origin_allowed "http://www.rubyonrails.com"
|
|
|
|
assert_origin_not_allowed "http://hax.com"
|
|
|
|
assert_origin_not_allowed "http://rails.co.uk"
|
2015-12-05 16:58:31 -05:00
|
|
|
end
|
|
|
|
|
2016-09-21 08:55:25 -04:00
|
|
|
test "allow same origin as host" do
|
|
|
|
@server.config.allow_same_origin_as_host = true
|
|
|
|
assert_origin_allowed "http://#{HOST}"
|
|
|
|
assert_origin_not_allowed "http://hax.com"
|
|
|
|
assert_origin_not_allowed "http://rails.co.uk"
|
|
|
|
end
|
|
|
|
|
2015-10-07 12:24:20 -04:00
|
|
|
private
|
|
|
|
def assert_origin_allowed(origin)
|
|
|
|
response = connect_with_origin origin
|
2015-12-17 10:23:36 -05:00
|
|
|
assert_equal(-1, response[0])
|
2015-10-07 12:24:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_origin_not_allowed(origin)
|
|
|
|
response = connect_with_origin origin
|
|
|
|
assert_equal 404, response[0]
|
|
|
|
end
|
|
|
|
|
|
|
|
def connect_with_origin(origin)
|
2015-10-15 22:11:49 -04:00
|
|
|
response = nil
|
|
|
|
|
|
|
|
run_in_eventmachine do
|
|
|
|
response = Connection.new(@server, env_for_origin(origin)).process
|
|
|
|
end
|
|
|
|
|
|
|
|
response
|
2015-10-07 12:24:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def env_for_origin(origin)
|
2016-08-06 13:15:15 -04:00
|
|
|
Rack::MockRequest.env_for "/test", "HTTP_CONNECTION" => "upgrade", "HTTP_UPGRADE" => "websocket", "SERVER_NAME" => HOST,
|
|
|
|
"HTTP_HOST" => HOST, "HTTP_ORIGIN" => origin
|
2015-10-07 12:24:20 -04:00
|
|
|
end
|
|
|
|
end
|