【发布时间】:2020-01-31 18:56:45
【问题描述】:
我有一个 Perl 脚本,它可以打开一个文件、处理它并打印一些输出。 输入文件被压缩。
$file 的路径作为参数传递给脚本。
以下是我正在使用的当前解决方案:
open(my $fh, "-|", "$gzcat $file") or die("Cannot open $file$!");
该脚本最近在Checkmarx的安全审计中失败,报错如下:
<script> gets user input for the $fh element. This element’s value then flows through the code without being properly sanitized or validated and is eventually displayed to the user in method <method>. This may enable a CrossSite-Scripting attack.
我尝试使用 perl -f 验证文件是否存在,并使用 $file =~ s/[^A-Za-z0-9_\-\.\/]//g; 删除不需要的字符,但它不满足 Checkmarx。
我想知道在 Perl 中清理包含文件路径的输入的正确方法是什么。
【问题讨论】:
-
听起来您急需proper shell escaping。 永远不要在没有适当转义的情况下将用户提供的输入转储到 shell 参数中。
-
@tadman 输入由运行在生成文件名的服务器上的不同应用程序提供。
-
你仍然不能相信
$file是无害的,或者没有shell 字符。例如$file = "a space.txt"将不起作用。