使用Devise防止密码重用

问题描述 投票:4回答:4

我知道强制密码在用户创建密码后的一段时间后过期,这不是Devise逻辑的一部分,我打算编写自己的代码来实现。

它也看起来强迫用户不要重复使用最后一个X(在我的情况下为10)密码需要手动编码。

我的想法是,我将创建类似user_passwords表的东西,并在我的代码中使用逻辑来确保新密码与该用户的该表中的任何密码都不匹配。同时我会将新密码插入表中,除非该用户已有10条记录,这意味着我将用新值覆盖最旧的密码。表结构将是这样的:

user_passwords

  • 用户身份
  • encrypted_pa​​ssword
  • created_at

如果有人有更好,更优雅的解决方案来处理这个问题,我会很感激。

ruby-on-rails devise
4个回答
6
投票

我知道强制密码在用户创建密码后的一段时间后过期,这不是Devise逻辑的一部分,我打算编写自己的代码来实现。

在实践中,与安全相关的研究发现这是一个坏主意。那是因为每次变化都会收益递减。也就是说,密码开始强大,然后随着用户尝试遵守策略而变得越来越弱。参见Peter Gutmann的Engineering Security和第7章密码。

从书中,其他愚蠢的事情包括复杂性要求。 (在您反对之前,请阅读本书的相关部分)。


...创建类似user_passwords表的东西,并在我的代码中使用逻辑来确保新密码与该用户的该表中的任何密码都不匹配。

一旦你阅读了这一章,我就可以问:为什么你首先允许用户选择一个弱/受伤/破损的密码?当与Mark Brunett's list of 10 million leaked passwords结合使用时,这些60 KB Bloom过滤器看起来非常有用:)


防止密码重用...

重复使用的是跨站点的密码重用。 Brown,Bracken,Zoccoli和Douglas表示Generating and Remembering Passwords的数字约为70%(Applied Cognitive Psychology,Volume 18,Issue 6,pp.641-651)。而Das,Bonneau,Caesar,Borisov和Wang在The Tangled Web of Password Reuse报告的数字约为45%。请注意,Tangled Web研究必须破解密码,因此这些数字可能更高,因为它们无法恢复所有密码

为了使重用成为更严重的问题,用户必须记住大约25个不同站点的密码,根据The Tangled Web of Password Reuse中的Das,Bonneau,Caesar,Borisov和Wang。

几年前我甚至被这个烧了。我在两个低价值账户上使用了相同的密码。然后GNU's Savannah遭到攻击,攻击者能够使用恢复的密码来破坏一个小小的电子邮件帐户。

现在,当我需要凭据时,我只生成一个长的随机字符串。我甚至不打算为大多数网站写下来。当我需要再次访问某个站点时,我只需要完成恢复过程。


2
投票

devise_security_extension似乎适用于我需要的东西。

但是,目前它不支持Devise 2.0或更高版本。我遇到了很多问题,不得不将我的Devise降级到1.5.3。根据他们留言板上的评论,他们目前正致力于将gem移植到Devise 2.0兼容版本。

我为它的password_expirable和password_archivable模块提供了一个旋转。一切似乎都按预期工作。

它还支持secure_validatable,session_limitable和expirable,前两个我将在不久的将来使用它。


0
投票

在PasswordsController中,您可以检查它。

class PasswordsController < Devise::PasswordsController

  def update
    current_user = User.with_reset_password_token(params[:user][:reset_password_token])
    if current_user && previous_and_new_password_is_same?(current_user)
      current_user.errors[:password] << "has been used previously."
      self.resource = current_user
      respond_with resource
    else
      super
    end
  end

  private

  def previous_and_new_password_is_same?(current_user)
    bcrypt       = ::BCrypt::Password.new(current_user.encrypted_password)
    hashed_value = ::BCrypt::Engine.hash_secret([params[:user][:password], Devise.pepper].join, bcrypt.salt)
    hashed_value == current_user.encrypted_password
  end

end

Here是如何禁止以前使用的密码的Devise gem的文档?


-1
投票

devise_security_extension在rails 5中对我不起作用,我创建了我的自定义:

  • 创建一个迁移并添加一个额外的列,如:add_column:users,:old_passwords,:text
  • 在你的模型中添加两​​个回调:after_save:cache_old_pass和before_save:verify_old_pass
  • 创建回调方法: private def verify_old_pass if self.encrypted_password_changed? old_passwords.to_s.split(',').each do |pass_encrypted| if Devise::Encryptor.compare(self.class, pass_encrypted, password) errors.add(:base, 'Your password cannot be previous up to 3 back') return throw(:abort) end end end end def cache_old_pass # cache last 3 passwords if self.encrypted_password_changed? update_column(:old_passwords, ([self.encrypted_password] + old_passwords.to_s.split(',')[0, 3]).join(',')) end end
© www.soinside.com 2019 - 2024. All rights reserved.