From 339f874931484a42d896dd25a13171e80519c91f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Hauge=20Bj=C3=B8rnskov?= <19725+henrikbjorn@users.noreply.github.com> Date: Wed, 30 Sep 2026 10:55:13 +0200 Subject: [PATCH] Fail loudly when a reply doesn't match its command FreeSWITCH answers each command on a connection once, in order, and its replies carry no request id, so librevox hands each reply to the oldest waiting command. Nothing checked that the reply was the kind that command expects. If commands and replies ever fell out of step, a caller could silently get someone else's reply. Each waiting command now records what it expects: an api/response for `api`, a command/reply for everything else. A reply of the other kind raises Librevox::ProtocolError (a ConnectionError) for every waiting command, and the connection is closed. A reply with no waiting command is dropped, as before. Two test fakes replied with the wrong kind, which FreeSWITCH never does. They now match real behaviour. --- lib/librevox.rb | 3 ++ lib/librevox/listener/base.rb | 32 +++++++++-- .../functional/librevox/listener/base_test.rb | 2 +- .../librevox/listener/outbound_apps_test.rb | 3 +- .../librevox/listener/reply_matching_test.rb | 54 +++++++++++++++++++ test/support/listener.rb | 3 ++ 6 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 test/functional/librevox/listener/reply_matching_test.rb diff --git a/lib/librevox.rb b/lib/librevox.rb index 703fc58..836f2ce 100644 --- a/lib/librevox.rb +++ b/lib/librevox.rb @@ -6,6 +6,9 @@ module Librevox class ResponseError < StandardError; end class ConnectionError < StandardError; end + # A reply arrived that isn't the kind its command expects, so replies and + # commands are out of step. The connection can't be trusted any more. + class ProtocolError < ConnectionError; end autoload :Client, 'librevox/client' autoload :CommandSocket, 'librevox/command_socket' diff --git a/lib/librevox/listener/base.rb b/lib/librevox/listener/base.rb index 32a26af..9e97d3d 100644 --- a/lib/librevox/listener/base.rb +++ b/lib/librevox/listener/base.rb @@ -50,7 +50,7 @@ def send_message(msg) # every other sender. @write_lock.acquire do @connection.send_data(msg) - @reply_promises << promise + @reply_promises << [promise, expected_reply(msg)] end reply = promise.wait @@ -82,7 +82,7 @@ def execute_app(app, uuid, args = nil, **params) def receive_message(response) if response.reply? - @reply_promises.shift&.resolve(response) + resolve_reply(response) return end @@ -107,7 +107,7 @@ def receive_message(response) def connection_closed error = ConnectionError.new("Connection closed") - @reply_promises.each { |p| p.reject(error) } + @reply_promises.each { |p, _| p.reject(error) } @app_promises.each_value { |p| p.reject(error) } @reply_promises.clear @@ -133,6 +133,32 @@ def session_complete? private + # FreeSWITCH answers every command on a connection once, in order, and + # its replies carry no request id. So each reply goes to the oldest + # waiting command, and must be the kind that command expects: an + # api/response for `api`, a command/reply for everything else. Anything + # else means commands and replies are out of step, and every waiting + # command is failed rather than handed someone else's reply. + def resolve_reply(response) + promise, expected = @reply_promises.shift + return unless promise + return promise.resolve(response) if response.public_send(expected) + + out_of_step(response, promise) + end + + def expected_reply(msg) + msg.start_with?("api ") ? :api_response? : :command_reply? + end + + def out_of_step(response, promise) + error = ProtocolError.new("reply out of step: got #{response.headers[:content_type]}") + promise.reject(error) + @reply_promises.each { |p, _| p.reject(error) } + @reply_promises.clear + disconnect + end + def on_event(event) end diff --git a/test/functional/librevox/listener/base_test.rb b/test/functional/librevox/listener/base_test.rb index 6f6ad0b..8af3a57 100644 --- a/test/functional/librevox/listener/base_test.rb +++ b/test/functional/librevox/listener/base_test.rb @@ -20,7 +20,7 @@ def test_on_event_with_api_does_not_block_handle_response Sync do event "SOME_EVENT" assert_equal "api test", @listener.outgoing_data.shift - command_reply "Reply-Text" => "+OK" + api_response body: "+OK" end }.join(1) diff --git a/test/functional/librevox/listener/outbound_apps_test.rb b/test/functional/librevox/listener/outbound_apps_test.rb index 4faf8c7..7952ab7 100644 --- a/test/functional/librevox/listener/outbound_apps_test.rb +++ b/test/functional/librevox/listener/outbound_apps_test.rb @@ -60,8 +60,9 @@ def test_events_do_not_advance_app def test_api_responses_do_not_advance_app assert_execute_app @listener, "foo", "1234" + command_reply "Reply-Text" => "+OK" # the sendmsg's own acknowledgement - api_response body: "Foo" + api_response body: "Foo" # unsolicited: no command waits for it, so it's dropped assert_send_nothing @listener end diff --git a/test/functional/librevox/listener/reply_matching_test.rb b/test/functional/librevox/listener/reply_matching_test.rb new file mode 100644 index 0000000..6782237 --- /dev/null +++ b/test/functional/librevox/listener/reply_matching_test.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require_relative '../../../test_helper' +require_relative '../../../support/listener' + +class TestReplyMatching < Minitest::Test + include Librevox::Test::ListenerHelpers + + class Listener < Librevox::Listener::Base + attr_reader :disconnected + + def disconnect + @disconnected = true + end + end + + def setup + @listener = Listener.new(MockConnection.new) + end + + def test_an_api_command_gets_its_api_response + Async do + sending = Async { @listener.send_message("api status") } + api_response body: "UP" + + assert_equal "UP", sending.wait.content + end + end + + def test_other_commands_get_their_command_reply + Async do + sending = Async { @listener.send_message("sendevent CUSTOM") } + command_reply "Reply-Text" => "+OK" + + assert_predicate sending.wait, :command_reply? + end + end + + def test_a_reply_of_the_wrong_kind_fails_every_waiting_command + Async do + api = Async { @listener.send_message("api status") } + other = Async { @listener.send_message("sendevent CUSTOM") } + command_reply "Reply-Text" => "+OK" # meant for neither: the api call expects an api/response + + assert_raises(Librevox::ProtocolError) { api.wait } + assert_raises(Librevox::ProtocolError) { other.wait } + assert @listener.disconnected, "an out-of-step connection is closed" + end + end + + def test_the_error_is_a_connection_error + assert_operator Librevox::ProtocolError, :<, Librevox::ConnectionError + end +end diff --git a/test/support/listener.rb b/test/support/listener.rb index d85c502..f4fbcc9 100644 --- a/test/support/listener.rb +++ b/test/support/listener.rb @@ -17,6 +17,9 @@ def receive_data nil end + def close_write + end + def close end end