用户正在使用 Rails / Devise 从其他登录用户那里获取会话

Dav*_*lar 5 ruby ruby-on-rails devise

我正在使用 Devise 和 Ruby on Rails 来开发游戏。

用户可以登录并加入游戏。

当他们第一次登录时,一切都很好 - 这表明他们以预期的人身份登录。

但是我很少注意到(在现实世界中使用以及在测试中一次)让我感到困惑的情况。

突然,其中一个用户“变成”了不同的用户。就好像他们以其他用户身份登录一样。他们可以完全访问他们的帐户,Rails 认为他们以不同的用户身份登录。诸如“current_user.name”和“current_user.email”之类的所有内容都取决于其他用户,即他们之前未登录的用户以及他们无权成为的用户。

我无法弄清楚这可能来自我的代码中的哪里。我不想在这里发布我的全部代码,所以我希望能对如何在我的代码中导致这样的事情发生进行一些头脑风暴- 或者我更有可能在寻找一个设计错误设计是否搞砸了它在某些竞争条件下返回的会话?

对于使用 devise 的任何站点,从一般安全角度来看,后者处理起来有些可怕,但我认为其他人现在应该已经看到了。所以我认为它必须在我的代码中 - 但在我的代码中没有任何地方我触摸会话信息,也没有做任何奇怪的事情,比如将 current_user 分配给任何东西。

要非常清楚会发生什么:

  1. “Bob”以“Bob”的身份登录他的设备并看到他是“Bob”(current_user.name)并且可以访问“Bob”的东西。
  2. “Joe”以“Joe”的身份登录他的设备并看到他是“Joe”(current_user.name)并且可以访问“Joe”的东西。
  3. 在跟随任意数量的内部链接之一(不更改/设置用户信息)之后,“Joe”正在使用的设备现在显示为“Bob”(current_user.name)并且可以访问“Bob”的东西(例如编辑他的个人资料页面等。 - 全部由 current_user 保护)。

关于我可能缺少什么的任何想法?

我的设计设置允许使用电子邮件/密码登录以及使用 Facebook 等进行 oauth 登录。

——

根据要求,这是设计迁移:

——

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      t.string   :confirmation_token
      t.datetime :confirmed_at
      t.datetime :confirmation_sent_at
      t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end
Run Code Online (Sandbox Code Playgroud)

Pas*_*cal 3

发生这种情况的方式有多种,显然很难从这里判断出了什么问题。由于 Devise 经过了实际测试,我认为这是您所做的事情,而不是设备正在做的事情(当然,Devise 仍然有可能存在错误,但更可能是您的代码有错误)

你有

  • 保存与用户相关信息的静态变量
  • 用户名不唯一(在数据库级别强制执行)
  • 通过不同的注册机制创建的具有相同电子邮件地址的用户
  • 改变了会话处理中的任何内容??

您能找到在本地重现该错误的方法吗?也许使用一个以不同用户身份登录并检查用户是否保持不变的脚本。它发生在什么服务器上?您使用什么会话存储?