在将字符串传递给系统调用之前,我需要过滤的最小字符集是什么?

kno*_*orv -2 security perl

假设给出了以下Perl代码:

my $user_supplied_string = &retrieved_from_untrusted_user();
$user_supplied_string =~ s/.../.../g; # filtering done here
my $output = `/path/to/some/command '${user_supplied_string}'`;
Run Code Online (Sandbox Code Playgroud)

代码显然不安全,但假设唯一可以改变的是第2行的过滤代码.

我的问题:

  • 需要在第2行过滤的最小字符集是什么才能使上述代码安全?

请注意:

  • 在这种情况下,白名单不是一种选择,因此请将您的答案集中在过滤掉哪些内容以确保其安全.更具体地说; 为了使其安全而过滤掉的最小字符集是什么?其他一切都是偏离主题的.
  • 确保您的答案解决了所述问题(" 第2行需要过滤的最小字符集是什么,以使上述代码安全?").如果您的答案没有解决这个非常具体的问题,那么请不要发帖.谢谢.

Cha*_*ens 5

首先,鉴于您关心安全性,我建议您研究一下污点模式.至于允许shell可见的最小字符集,最好不要让shell看到任何字符:

my $output = do {
    local $/;
    open my $pipe, "-|", "/path/to/some/command", $user_supplied_string
        or die "could not run /path/to/some/command: $!";
    <$pipe>;
};
Run Code Online (Sandbox Code Playgroud)