wal*_*ake 6 ruby ruby-on-rails
在我的rails应用程序中,我收到了来自制动器的以下安全警告.使用模型属性调用不安全的反射方法constantize.这是我的代码正在做的事情.
chart_type = Chart.where(
id: chart_id,
).pluck(:type).first
begin
ChartPresenter.new(chart_type.camelize.constantize.find(chart_id))
rescue
raise "Unable to find the chart presenter"
end
根据我的研究,我没有找到任何具体的解决方案.我听说你可以制作白名单,但我不确定刹车手正在寻找什么.我试图创建一个数组并在调用constantize之前检查它,并且仍然会抱怨破坏者.对此的任何帮助都会很棒.如果你认为这不是一个必要的解决方案,你可以详细说明为什么它不应该成为一个问题吗?
Uri*_*ssi 13
您可以反过来找到名称为的类chart_type:
chart_class = [User, Category, Note, Post].find { |x| x.name == chart_type.classify }
if chart_class.nil?
raise "Unable to find the chart presenter"
end
ChartPresenter.new(chart_class.find(chart_id))
Run Code Online (Sandbox Code Playgroud)
这样Brakeman应该开心,你更安全......