From d635d8f714debcc947d7d85e3ba5de6b57a366be Mon Sep 17 00:00:00 2001 From: thomas morgan Date: Tue, 29 Aug 2023 12:06:13 -0600 Subject: [PATCH] improve checks in `stream?` for http1 --- .mailmap | 1 + lib/async/websocket/upgrade_request.rb | 7 ++++--- test/async/websocket/client.rb | 28 ++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/.mailmap b/.mailmap index a55b445..16a64a8 100644 --- a/.mailmap +++ b/.mailmap @@ -1,2 +1,3 @@ Juan Antonio Martín Lucas Aurora Nockert +Thomas Morgan diff --git a/lib/async/websocket/upgrade_request.rb b/lib/async/websocket/upgrade_request.rb index eab7a06..8f3d6ca 100644 --- a/lib/async/websocket/upgrade_request.rb +++ b/lib/async/websocket/upgrade_request.rb @@ -20,9 +20,10 @@ class UpgradeRequest < ::Protocol::HTTP::Request include ::Protocol::WebSocket::Headers class Wrapper - def initialize(response) + def initialize(response, verified:) @response = response @stream = nil + @verified = verified end def close @@ -32,7 +33,7 @@ def close attr_accessor :response def stream? - @response.status == 101 + @response.status == 101 && @verified end def status @@ -74,7 +75,7 @@ def call(connection) end end - return Wrapper.new(response) + return Wrapper.new(response, verified: !!accept_digest) end end end diff --git a/test/async/websocket/client.rb b/test/async/websocket/client.rb index ae71654..c486218 100644 --- a/test/async/websocket/client.rb +++ b/test/async/websocket/client.rb @@ -110,6 +110,34 @@ with 'http/1' do let(:protocol) {Async::HTTP::Protocol::HTTP1} it_behaves_like ClientExamples + + with 'invalid sec-websocket-accept header' do + let(:app) do + Protocol::HTTP::Middleware.for do |request| + Protocol::HTTP::Response[101, {'sec-websocket-accept'=>'wrong-digest'}, []] + end + end + + it 'raises an error' do + expect do + Async::WebSocket::Client.connect(client_endpoint) {} + end.to raise_exception(Async::WebSocket::ProtocolError, message: be =~ /Invalid accept digest/) + end + end + + with 'missing sec-websocket-accept header' do + let(:app) do + Protocol::HTTP::Middleware.for do |request| + Protocol::HTTP::Response[101, {}, []] + end + end + + it 'raises an error' do + expect do + Async::WebSocket::Client.connect(client_endpoint) {} + end.to raise_exception(Async::WebSocket::ProtocolError, message: be =~ /Failed to negotiate connection/) + end + end end with 'http/2' do