gpt4 book ai didi

macos - 在污染模式下读取文件时出错

转载 作者:行者123 更新时间:2023-12-01 19:38:58 25 4
gpt4 key购买 nike

我正在尝试使用污点模式。我想根据用户输入打开一个文件并打开一个文件来读取数据。下面是我的代码

#!/usr/bin/perl -w
use strict;
use warnings;

my $name = $ARGV[0];
my $file = "/Desktop/data/$name";

open MYFILE, "$file" or die $!;


while (<MYFILE>) {
chomp;
print "$_\n";
}
close(MYFILE);

案例 1) 当我运行文件时使用 perl -w 文件名.pl 输入.txt我能够从文件中读取数据。

案例 2)当我改变

#!/usr/bin/perl -w
to
#!/usr/bin/perl -T

并使用运行文件 perl -T 文件名.pl 输入.txt我仍然能够读取数据。

情况 3)当我将文件更改为以写入模式打开并以污染模式运行时,我得到正确的输出,

Insecure dependency in open while running with -t switch at test1.pl line 8.

第二种情况可能有什么问题?还是这是一种正确的行为?

是否允许以污点模式打开文件进行读取?

最佳答案

这是污点模式的正确行为。 The documentation指定:

You may not use data derived from outside your program to affect something else outside your program--at least, not by accident.

[...]

$arg = shift; # $arg is tainted

[...]

If you try to do something insecure, you will get a fatal error saying something like "Insecure dependency" or "Insecure $ENV{PATH}".

(编辑:遗漏了一些东西):

Tainted data may not be used directly or indirectly in any command that invokes a sub-shell, nor in any command that modifies files, directories, or processes, with the following exceptions:

  • Arguments to print and syswrite are not checked for taintedness.

(这就是读取模式示例不提示文件数据的原因。)

命令行参数可能不安全,因此除非另有说明,否则会受到污染。

判断数据是否被污染:

To test whether a variable contains tainted data, and whose use would thus trigger an "Insecure dependency" message, you can use the tainted() function of the Scalar::Util module, available in your nearby CPAN mirror, and included in Perl starting from the release 5.8.0.

清除数据:

[...]the only way to bypass the tainting mechanism is by referencing subpatterns from a regular expression match. Perl presumes that if you reference a substring using $1, $2, etc., that you knew what you were doing when you wrote the pattern. That means using a bit of thought--don't just blindly untaint anything, or you defeat the entire mechanism. It's better to verify that the variable has only good characters (for certain values of "good") rather than checking whether it has any bad characters. That's because it's far too easy to miss bad characters that you never thought of.

(带有对 use locale 的警告):

If you are writing a locale-aware program, and want to launder data with a regular expression containing \w, put no locale ahead of the expression in the same block. See SECURITY in perllocale for further discussion and examples.

关于macos - 在污染模式下读取文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16137142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com