用模型属性将不安全反射方法常数化调用



在我的rails应用程序中,我从brakeman获得以下安全警告。不安全反射方法用模型属性常量化调用。下面是我的代码所做的。

<>之前 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 之前从我的研究中,我还没有找到任何具体的解决办法。我听说你可以做一个白名单,但我不确定布雷克曼在寻找什么。我试着创建一个数组,并在调用constantify和breakman之前检查它。如果能帮上忙就太好了。如果你觉得这不是一个必要的修复,你能详细说明为什么不应该关注它吗?

您可以反过来查找名称为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))

这样的话,布雷克曼应该会很高兴,你也会更安全。

相关内容

最新更新