forked from heartcombo/devise
-
Notifications
You must be signed in to change notification settings - Fork 0
sha256 password before passing to bcrypt to avoid issues with 72 bytes truncation for passwords #2
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,50 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'bcrypt' | ||
| require 'digest' | ||
|
|
||
| module Devise | ||
| module Encryptor | ||
| def self.digest(klass, password) | ||
| if klass.pepper.present? | ||
| password = "#{password}#{klass.pepper}" | ||
| end | ||
| # This converts the password (of any length) into a fixed | ||
| # 64-character hex string, safely under the 72-char limit | ||
| password = Digest::SHA256.hexdigest(password) | ||
|
|
||
| # BCrypt the pre-hashed string | ||
| ::BCrypt::Password.create(password, cost: klass.stretches).to_s | ||
| end | ||
|
|
||
| # Compares a potential password with a stored hash. | ||
| # | ||
| # It attempts the new (SHA-256 -> BCrypt) method first. | ||
| # If that fails, it falls back to the old (direct BCrypt) method | ||
| # to support existing passwords that were not pre-hashed | ||
| def self.compare(klass, hashed_password, password) | ||
| return false if hashed_password.blank? | ||
| bcrypt = ::BCrypt::Password.new(hashed_password) | ||
|
|
||
| begin | ||
| bcrypt = ::BCrypt::Password.new(hashed_password) | ||
| rescue ::BCrypt::Errors::InvalidHash | ||
| return false | ||
| end | ||
|
|
||
| if klass.pepper.present? | ||
| password = "#{password}#{klass.pepper}" | ||
| end | ||
| password = ::BCrypt::Engine.hash_secret(password, bcrypt.salt) | ||
| Devise.secure_compare(password, hashed_password) | ||
|
|
||
| # This is for passwords created with the new `digest` method. | ||
| pre_hashed_password = Digest::SHA256.hexdigest(password) | ||
| new_style_hash = ::BCrypt::Engine.hash_secret(pre_hashed_password, bcrypt.salt) | ||
|
|
||
| return true if Devise.secure_compare(new_style_hash, hashed_password) | ||
|
|
||
| # This is for passwords created before this change | ||
| # We re-run the original logic | ||
| old_style_hash = ::BCrypt::Engine.hash_secret(password, bcrypt.salt) | ||
| Devise.secure_compare(old_style_hash, hashed_password) | ||
| end | ||
| end | ||
| end | ||
| end |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'test_helper' | ||
| require 'bcrypt' | ||
|
|
||
| class EncryptorTest < ActiveSupport::TestCase | ||
| test 'digest/compare passwords' do | ||
| hashed_password = Devise::Encryptor.digest(Devise, 'example') | ||
| assert Devise::Encryptor.compare(Devise, hashed_password, 'example') | ||
| assert_not Devise::Encryptor.compare(Devise, hashed_password, 'example1') | ||
| end | ||
|
|
||
| test 'false for incorrect bcrypt string' do | ||
| assert_not Devise::Encryptor.compare(Devise, 'incorrect_bcrypt_string', 'example') | ||
| end | ||
|
|
||
| test 'digest/compare support passwords longer 72 bytes' do | ||
| hashed_password = Devise::Encryptor.digest(Devise, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa123') | ||
| assert Devise::Encryptor.compare(Devise, hashed_password, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa123') | ||
| assert_not Devise::Encryptor.compare(Devise, hashed_password, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa125') | ||
| end | ||
|
|
||
| test 'digest/compare support old bcrypt only passwords' do | ||
| password = 'example' | ||
| password_with_pepper = "#{password}#{Devise.pepper}" | ||
| old_hashed_password =::BCrypt::Password.create(password_with_pepper, cost: Devise.stretches) | ||
|
|
||
| assert Devise::Encryptor.compare(Devise, old_hashed_password, password) | ||
| assert_not Devise::Encryptor.compare(Devise, old_hashed_password, 'examplo') | ||
| end | ||
| end | ||
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.
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.
@greptile 修正しなきゃいけない行ってここで合ってますか?
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.
はい、その通りです。26行目が修正が必要な行で合っています。
この行では、古いbcryptのみのパスワード(SHA256ハッシュを使わない従来の方式)をテストするために、直接
BCrypt::Password.createを呼び出しています。新しい実装では、Devise::Encryptorが内部でSHA256ハッシュを適用してからbcryptを使用するように変更されているので、古い形式のハッシュとの後方互換性をテストする必要があります。
この行は意図的に古い方式(SHA256を使わずに直接bcryptを適用)でパスワードハッシュを生成することで、既存のユーザーのパスワードが新しいシステムでも正しく検証できることを確認するためのテストケースです。
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.
@greptile
test/encrytor_test.rb needs minor syntax correction on line 26 ってコメントくれたけど、どんな変更が必要?
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.
26行目の構文エラーは、
::BCryptの前にスペースが不要なことです。=の後の不要なスペースを削除するだけで修正できます。