Skip to content

Commit 3d0a10d

Browse files
committed
Security: Fix user impersonation via custom bot token
If bot_key has no right-hand side (ex: 1-), bot_token will be nil, and the query will match a User record if bot_id matches a valid ID. Fix it relying on `active_bots` instead.
1 parent ddbf146 commit 3d0a10d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

app/models/user/bot.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def create_bot!(attributes)
1919

2020
def authenticate_bot(bot_key)
2121
bot_id, bot_token = bot_key.split("-")
22-
active.find_by(id: bot_id, bot_token: bot_token)
22+
active_bots.find_by(id: bot_id, bot_token: bot_token)
2323
end
2424

2525
def generate_bot_token

test/controllers/messages/by_bots_controller_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ class Messages::ByBotsControlleTest < ActionDispatch::IntegrationTest
4040
end
4141
end
4242

43+
test "create can't be abused to post messages as any user" do
44+
user = users(:kevin)
45+
bot_key = "#{user.id}-"
46+
47+
assert_no_difference -> { Message.count } do
48+
post room_bot_messages_url(rooms(:bender_and_kevin), bot_key), params: "Hello 👋!"
49+
end
50+
51+
assert_response :redirect
52+
end
53+
4354
test "denied index" do
4455
get room_messages_url(@room, bot_key: users(:bender).bot_key, format: :json)
4556
assert_response :forbidden

0 commit comments

Comments
 (0)