Repository navigation
Harden bot key authentication against a blank token - #447
Merged
Merged
Conversation
`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.
Contributor
There was a problem hiding this comment.
🟢 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
nilfor 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 rungh pr ready --undo.
Click "Ready for review" or rungh pr readyto 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
User.authenticate_botsplits 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):A key of only an id (
12, or12-) leavesbot_tokenasnil, so the lookup becomesbot_token IS NULLand matches bot12if its storedbot_tokenisNULL. A key like12--xmakes the token"", which matches a storedbot_tokenof"".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
nilfromUser.authenticate_botwhen the token is blank. Every key with a non-blank token authenticates as before.