Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何处理Brakeman报告的rails应用程序中的命令注入_Ruby On Rails_Code Injection_Brakeman - Fatal编程技术网

Ruby on rails 如何处理Brakeman报告的rails应用程序中的命令注入

Ruby on rails 如何处理Brakeman报告的rails应用程序中的命令注入,ruby-on-rails,code-injection,brakeman,Ruby On Rails,Code Injection,Brakeman,我在项目的某个库中有以下代码,这些代码在Sideqik Worker上执行: def self.generate_pdf(report) file_name = report['r_file'].gsub('.ric', '') path = "#{Rails.root}/report_files" java_cmd = "./fileprint_linux.sh" if %w(development test).include?(Rails.env) command

我在项目的某个库中有以下代码,这些代码在Sideqik Worker上执行:

def self.generate_pdf(report)
  file_name = report['r_file'].gsub('.ric', '')
  path = "#{Rails.root}/report_files"
  java_cmd = "./fileprint_linux.sh"
  if %w(development test).include?(Rails.env)
      command = "cd #{path}; sh #{java_cmd} silent #{report.r_file.path}"
  else
    temp = Tempfile.new("#{file_name}.tmp")
    File.open(temp.path, 'wb') { |f| f.write(open(report.r_file.url).read) }
    command = "cd #{path}; sh #{java_cmd} silent #{temp.path}"
  end

  stdin, stdout, stderr = Open3.popen3(command.shellescape)

  if stderr.read.blank?
    .......
  end
end
当我在项目上运行Brakeman(3.2.1)时,我得到以下安全警告:

Possible command injection near line 21: Open3.popen3(("cd #{"#{Rails.root}/report_files"}; sh #{"./fileprint_linux.sh"} silent #{report.r_file.path}" or "cd #{"#{Rails.root}/report_files"}; sh #{"./fileprint_linux.sh"} silent #{Tempfile.new("#{report["r_file"].gsub(".ric", "")}.tmp").path}"))
它突出了这一部分,我想这会引起警告:

report['r_file'].gsub('.ric', '')
该警告还链接到此页面以获取有关该警告的更多信息,但我没有找到处理该警告的方法:

我试图通过查看其他帖子和页面来找到解决方法,但运气不好,因此发布了这篇文章

我应该如何处理这种情况以修复Brakeman报告的潜在漏洞


提前谢谢

对于@Gumbo建议在每个参数上使用shellescape的所有建议,修复上述警告的方法是在每个参数上使用shellescape():

"cd #{path.shellescape}; sh #{java_cmd.shellescape} silent #{report.r_file.path.shellescape}" 
然后在调用popen3命令时,我们使用*%W运算符分别传递每个参数,以便轻松地将命令字符串转换为数组:

stdin, stdout, stderr = Open3.popen3(*%W(command))
(在这种情况下,使用%w而不是*%w也有效)


这两种变化的结合解决了前面提到的制动警告问题。仅使用其中一个对我不起作用。

使用。是的,我尝试过,但警告仍然存在。我将编辑以将其添加到代码中,因为问题仍然存在。谢谢你的建议。你需要在每个参数上使用它:
“cd{path.shellescape};sh{java_cmd.shellescape}silent{report.r_file.path.shellescape}”
或者分别传递参数:
Open3.popen3(“sh”,java_cmd,“silent”,report.r_file.path,:chdir=>path)
谢谢@Gumbo的提示。我曾分别尝试过你的两个建议,但同时使用它们解决了制动警告问题。我将添加解释作为asnwer。