Ruby on rails 使用模型属性调用Brakeman不安全反射方法constantize
在我的rails应用程序中,我从brakeman收到以下安全警告。使用模型属性调用不安全的反射方法constantize。下面是我的代码所做的Ruby on rails 使用模型属性调用Brakeman不安全反射方法constantize,ruby-on-rails,ruby,Ruby On Rails,Ruby,在我的rails应用程序中,我从brakeman收到以下安全警告。使用模型属性调用不安全的反射方法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 cha
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打电话之前,我试图创建一个数组并进行检查,breakman仍在抱怨。这方面的任何帮助都会很好。如果您觉得这不是一个必要的解决方案,您可以详细说明为什么不应该引起关注吗?您可以反过来,找到名称为
图表类型的类:
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))
这样Brakeman应该会很高兴,你会更安全…我猜这会导致代码速度变慢,尽管正确?该列表[用户、类别、注释、帖子]将是一个动态列表,但这会有问题吗?也就是说我还是要坚持下去,对吗?列表有点长,无法全部打印出来。而且因为它是动态的,我们必须不断更新。你可以试试,我不知道Brakeman会怎么做。。。您可以尝试使用ActiveRecord::Base.substands
(请参见此处:)浏览您的模型