【发布时间】:2020-11-02 01:47:30
【问题描述】:
我创建了这个应用程序来显示节点的用户或所有者,但现在当我尝试登录时,应用程序失败并显示以下消息:
NoMethodError (undefined method `user_id' for nil:NilClass):
这是错误指向的代码行
users_controllers.rb
def authenticate
if params[:authentication] and authenticate = params[:authentication] and authenticate[:email].length > 0 and authenticate[:password].length > 0
username = authenticate[:email]
password = authenticate[:password]
super_user = User.find(TheChosenOne.first.user_id) # This is the problematic line
if username == super_user.email
if Digest::SHA1.hexdigest(password) == TheChosenOne.first.password
session[:user_id] = super_user.id
redirect_to controller: :nodes, action: :list
else
redirect_to controller: :users, action: :login, alert: 'invalid_credentials' and return
end
else
ret_result = nil
command = Thread.new do
ret_result = `cd #{Rails.root}/Auth/src/; java AuthorizationService '#{username}' '#{password}'`
end
command.join
if !ret_result.include? "true"
redirect_to controller: :users, action: :login, alert: 'invalid_credentials' and return
end
user = User.find_by_email(username)
if !User.exists?(user)
user = User.create({email: username})
end
我不确定是否需要在用户模型中创建 user_id 方法?这对我来说似乎没有多大意义。请我感谢我能得到的所有帮助。谢谢
【问题讨论】:
-
您的会话变量未启动。请向我们展示您的整个代码
-
你能验证
TheChosenOne.first是非空的吗?使用像pry这样的调试器工具会有很长的路要走。 -
与您最初的问题无关,但您的 shellout 存在未经身份验证的远程代码执行漏洞。通过使用
whatever'; /path/to/evil; echo '之类的用户名,我可以在您的服务器上执行我想要的任何命令。此外,您不应使用未加盐的 SHA1 来存储您的密码。请改用has_secure_password。 -
我也可以使用任何随机用户名和密码(例如
whatever'; echo 'true)完全跳过身份验证。您可能希望对所有参数使用Shellwords.shellescape,或者对您的外壳使用更安全的方法。 -
正如@maxpleaner 建议的那样,
TheChosenOne.first返回 nil
标签: ruby-on-rails ruby sqlite