fix(webhook): reject empty body with no media
Phillip Davis
created
When text is empty/nil and there is no media, return 400 instead of
falling through to message construction with nil text.
Change summary
sgx-bwmsgsv2.rb | 4 ++++
test/test_webhook_handler.rb | 23 +++++++++++++++++++++++
2 files changed, 27 insertions(+)
Detailed changes
@@ -1046,6 +1046,10 @@ class WebhookHandler < Goliath::API
# TODO: handle group chat, and fix above
text = jparams['text']
+ if text.to_s.empty? && Array(jparams['media']).empty?
+ return [400, {}, "Missing params\n"]
+ end
+
if jparams['to'].length > 1
msg = Blather::Stanza::Message.new(
Blather::JID.new(bare_jid).domain
@@ -375,4 +375,27 @@ class WebhookHandlerTest < Minitest::Test
assert_equal "Check this", text_msg.body
end
em :test_message_received_single_recipient_with_media_stanza
+
+ def test_message_received_empty_body_no_media_returns_400
+ payload = {
+ "type" => "message-received",
+ "to" => "+15550000000",
+ "message" => {
+ "id" => "bw-in-empty-001",
+ "direction" => "in",
+ "owner" => "+15550000000",
+ "from" => "+15551234567",
+ "to" => ["+15550000000"],
+ "time" => "2025-01-13T10:00:00Z",
+ "text" => ""
+ }
+ }
+
+ result = invoke_webhook(payload)
+ assert_equal [400, {}, "Missing params\n"], result
+ assert_empty written
+ entries = REDIS.stream_entries("messages").sync
+ assert_equal 0, entries.length
+ end
+ em :test_message_received_empty_body_no_media_returns_400
end