diff options
author | Pratik <pratiknaik@gmail.com> | 2015-10-12 18:14:57 -0500 |
---|---|---|
committer | Pratik <pratiknaik@gmail.com> | 2015-10-12 18:14:57 -0500 |
commit | a6ee365703bbee67db7091e6209be29733d034cd (patch) | |
tree | 3588a8b7846209912eb01c67e72d2c32c39a0f56 /test/connection/cross_site_forgery_test.rb | |
parent | d222f7de572f28fc2fa185e9f21cac6f7e6c84f0 (diff) | |
parent | ecab8314eba8519bd593cbc097ef60ee0c285cf2 (diff) | |
download | rails-a6ee365703bbee67db7091e6209be29733d034cd.tar.gz rails-a6ee365703bbee67db7091e6209be29733d034cd.tar.bz2 rails-a6ee365703bbee67db7091e6209be29733d034cd.zip |
Merge pull request #85 from rails/cross-site-forgery-protection
Add support for cross site forgery protection
Diffstat (limited to 'test/connection/cross_site_forgery_test.rb')
-rw-r--r-- | test/connection/cross_site_forgery_test.rb | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/test/connection/cross_site_forgery_test.rb b/test/connection/cross_site_forgery_test.rb new file mode 100644 index 0000000000..6073f89287 --- /dev/null +++ b/test/connection/cross_site_forgery_test.rb @@ -0,0 +1,55 @@ +require 'test_helper' +require 'stubs/test_server' + +class ActionCable::Connection::CrossSiteForgeryTest < ActiveSupport::TestCase + HOST = 'rubyonrails.com' + + setup do + @server = TestServer.new + @server.config.allowed_request_origins = %w( http://rubyonrails.com ) + end + + teardown do + @server.config.disable_request_forgery_protection = false + @server.config.allowed_request_origins = [] + end + + test "disable forgery protection" do + @server.config.disable_request_forgery_protection = true + assert_origin_allowed 'http://rubyonrails.com' + assert_origin_allowed 'http://hax.com' + end + + test "explicitly specified a single allowed origin" do + @server.config.allowed_request_origins = 'http://hax.com' + assert_origin_not_allowed 'http://rubyonrails.com' + assert_origin_allowed 'http://hax.com' + end + + test "explicitly specified multiple allowed origins" do + @server.config.allowed_request_origins = %w( http://rubyonrails.com http://www.rubyonrails.com ) + assert_origin_allowed 'http://rubyonrails.com' + assert_origin_allowed 'http://www.rubyonrails.com' + assert_origin_not_allowed 'http://hax.com' + end + + private + def assert_origin_allowed(origin) + response = connect_with_origin origin + assert_equal -1, response[0] + end + + def assert_origin_not_allowed(origin) + response = connect_with_origin origin + assert_equal 404, response[0] + end + + def connect_with_origin(origin) + ActionCable::Connection::Base.new(@server, env_for_origin(origin)).process + end + + def env_for_origin(origin) + Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket', 'SERVER_NAME' => HOST, + 'HTTP_ORIGIN' => origin + end +end |