From 00373e1a307a891fd1c3b0798c89cdc8be260011 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Thu, 19 Nov 2026 15:33:10 -0500 Subject: [PATCH] Reject a bot key with a blank token `User.authenticate_bot` searched with a missing or empty token, so a key holding only a bot's id matched that bot if its `bot_token` was `NULL`, and a key with an empty token segment matched a `bot_token` of `""`. Return `nil` when the token is blank. --- app/models/user/bot.rb | 2 +- test/models/user/bot_test.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/models/user/bot.rb b/app/models/user/bot.rb index 2ec003763..4b66ed2f5 100644 --- a/app/models/user/bot.rb +++ b/app/models/user/bot.rb @@ -19,7 +19,7 @@ def create_bot!(attributes) def authenticate_bot(bot_key) bot_id, bot_token = bot_key.split("-") - active_bots.find_by(id: bot_id, bot_token: bot_token) + active_bots.find_by(id: bot_id, bot_token: bot_token) if bot_token.present? end def generate_bot_token diff --git a/test/models/user/bot_test.rb b/test/models/user/bot_test.rb index b4878ce96..a1fa4b4df 100644 --- a/test/models/user/bot_test.rb +++ b/test/models/user/bot_test.rb @@ -31,6 +31,21 @@ class User::BotTest < ActiveSupport::TestCase assert User.authenticate_bot(bot.bot_key) end + test "authenticate rejects a key without a token" do + bot = User.create_bot!(name: "Bender") + bot.update_column(:bot_token, nil) + + assert_nil User.authenticate_bot("#{bot.id}") + assert_nil User.authenticate_bot("#{bot.id}-") + end + + test "authenticate rejects a key with an empty token" do + bot = User.create_bot!(name: "Bender") + bot.update_column(:bot_token, "") + + assert_nil User.authenticate_bot("#{bot.id}--anything") + end + test "deliver message by webhook" do WebMock.stub_request(:post, webhooks(:bender).url).to_return(status: 200)