ruby exec命令注入(保护!)

时间:2014-07-31 02:53:42

标签: ruby security exec brakeman

我正在为一个项目运行制动器宝石......它抱怨正在运行的一些exec命令。

当前代码:

Process.fork {exec "pdftk #{uncrypted_pdf_file} output #{pdf_file} owner_pw #{password} allow printing"}

Brakeman抱怨说有可能进行命令注射。我尝试了几种不同的调用exec的组合,例如:

Process.fork {exec "pdftk", uncrypted_pdf_file, " output #{pdf_file} ", "owner_pw #{password}", "allow printing"}

但正如你所期望的那样,每个论点都会依次传递给pdftk,所以它就会失败。

是否有办法一次性调用命令并防止命令注入。在我们的具体情况下,当我们控制所有变量时,它足够安全,但知道正确的方法会很好。

1 个答案:

答案 0 :(得分:2)

您需要单独传递每个参数:

exec "pdftk", uncrypted_pdf_file, "output", pdf_file, "owner_pw", password, "allow", "printing"

您可能还需要提供pdftk的完整路径。