Skip to content

Harden bot key authentication against a blank token - #447

Merged
flavorjones merged 1 commit into
mainfrom
card-5441-blank-bot-token
Nov 20, 2026
Merged

flavorjones merged 1 commit into
mainfrom
card-5441-blank-bot-token

Conversation

@flavorjones

Copy link
Copy Markdown
Member

Motivation

User.authenticate_bot splits the bot key on - and looks the bot up by id and token, with no check that the token is present (app/models/user/bot.rb):

bot_id, bot_token = bot_key.split("-")
active_bots.find_by(id: bot_id, bot_token: bot_token)

A key of only an id (12, or 12-) leaves bot_token as nil, so the lookup becomes bot_token IS NULL and matches bot 12 if its stored bot_token is NULL. A key like 12--x makes the token "", which matches a stored bot_token of "". User.create_bot! always sets a token, so this needs a row written with a blank token some other way, such as a migration or a console edit.

Since #28, the lookup is scoped to active_bots, so the same keys no longer match non-bot users.

Details

Return nil from User.authenticate_bot when the token is blank. Every key with a non-blank token authenticates as before.

`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.
Copilot AI balanced review requested due to automatic review settings November 19, 2026 20:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approved

No active review issues are recorded for the change that rejects blank bot authentication tokens.

No open issues 🎉

What changed in this PR

Hardens bot authentication beyond the active_bots restriction introduced in #28 by rejecting blank tokens before querying for a bot.

Changes:

  • Returns nil for blank tokens while preserving authentication for non-blank tokens.
  • Adds regression tests for missing and empty tokens against bots with matching blank stored tokens.

[!TIP]
If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

File Description
test/​models/​user/​bot_test.rb Tests rejection of keys with missing or empty tokens.
app/​models/​user/​bot.rb Requires a present token before the authentication lookup.

Review effort: Balanced

@flavorjones
flavorjones merged commit 945c22f into main Nov 20, 2026
14 checks passed
@flavorjones
flavorjones deleted the card-5441-blank-bot-token branch November 20, 2026 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants