-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Always return an error when confirmation_token
is blank
#5132
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,24 @@ def setup | |
assert_equal "can't be blank", confirmed_user.errors[:confirmation_token].join | ||
end | ||
|
||
test 'should return a new record with errors when a blank token is given and a record exists on the database' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this no longer needs 'and a record exists on the database' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's better to keep since that's the behavior we're trying to ensure it doesn't happen anymore. |
||
user = create_user(confirmation_token: '') | ||
|
||
confirmed_user = User.confirm_by_token('') | ||
|
||
refute user.reload.confirmed? | ||
assert_equal "can't be blank", confirmed_user.errors[:confirmation_token].join | ||
end | ||
|
||
test 'should return a new record with errors when a nil token is given and a record exists on the database' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this no longer needs 'and a record exists on the database' |
||
user = create_user(confirmation_token: nil) | ||
|
||
confirmed_user = User.confirm_by_token(nil) | ||
|
||
refute user.reload.confirmed? | ||
assert_equal "can't be blank", confirmed_user.errors[:confirmation_token].join | ||
end | ||
|
||
test 'should generate errors for a user email if user is already confirmed' do | ||
user = create_user | ||
user.confirmed_at = Time.now | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As written, this code may leak information about the internal state of the database (that some records have blank tokens). Could we check for presence of confirmation_token before the call to
find_first_by_auth_conditions
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about that. It's the same response as when there are no records with blank tokens. Do you think this is still leaking information?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that's the case then I guess it's probably ok for now, but if the
find_or_initialize_with_error_by
ortoken_generator
methods change, it could unintentionally create an information leakage issue here. I think it'd be safer to just return early (without making any queries) if we detect that the token is blank.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have examples of which kind of changes could cause leaks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any change that would result in a different response being returned when the token is blank and there is no record in the database.
Examples:
TokenGenerator#digest
changes to return a digest even whenvalue
is blank. This may result in a record being initialized and returned with error set to:invalid
instead of:blank
.find_or_initialize_with_error_by
changes to always use the providederror
argument instead of special-casing blank errors.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, get it. Both of these cases would be caught by our integration tests, so I guess this implementation is fine.
But since we already know there's no need to perform the query I guess it's better to return early as you mentioned. It also shows better the intention behind the code, which is to validate whether the parameter is present or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the changes, thanks for the review!