Skip to content

Commit

Permalink
Make sure cable closes the connection if open when responding to an i…
Browse files Browse the repository at this point in the history
…nvalid request
  • Loading branch information
lifo committed Nov 4, 2015
1 parent df20d87 commit 7c1631f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/action_cable/connection/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ def on_open
server.add_connection(self)
rescue ActionCable::Connection::Authorization::UnauthorizedError
respond_to_invalid_request
close
end

def on_message(message)
Expand Down Expand Up @@ -186,6 +185,8 @@ def respond_to_successful_request
end

def respond_to_invalid_request
close if websocket.alive?

logger.info finished_request_message
[ 404, { 'Content-Type' => 'text/plain' }, [ 'Page not found' ] ]
end
Expand Down
12 changes: 10 additions & 2 deletions test/connection/authorization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@ class Connection < ActionCable::Connection::Base
def connect
reject_unauthorized_connection
end

def send_async(method, *args)
# Bypass Celluloid
send method, *args
end
end

test "unauthorized connection" do
run_in_eventmachine do
server = TestServer.new
env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket'
server.config.allowed_request_origins = %w( http://rubyonrails.com )

env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket',
'HTTP_ORIGIN' => 'http://rubyonrails.com'

connection = Connection.new(server, env)
connection.websocket.expects(:close)

connection.process
connection.send :on_open
end
end
end

0 comments on commit 7c1631f

Please sign in to comment.