2 # Tests for taint-mode features
7 use Test::More tests => 21;
10 use_ok 'Text::Template' or exit 1;
12 if ($^O eq 'MSWin32') {
13 # File::Temp (for all versions up to at least 0.2308) is currently bugged under MSWin32/taint mode [as of 2018-09]
14 # ... fails unless "/tmp" on the current windows drive is a writable directory OR either $ENV{TMP} or $ENV{TEMP} are untainted and point to a writable directory
15 # ref: [File-Temp: Fails under -T, Windows 7, Strawberry Perl 5.12.1](https://rt.cpan.org/Public/Bug/Display.html?id=60340)
16 ($ENV{TEMP}) = $ENV{TEMP} =~ m/^.*$/gmsx; # untaint $ENV{TEMP}
17 ($ENV{TMP}) = $ENV{TMP} =~ m/^.*$/gmsx; # untaint $ENV{TMP}
20 my $tmpfile = File::Temp->new;
21 my $file = $tmpfile->filename;
23 # makes its arguments tainted
26 $_ .= substr($0, 0, 0); # LOD
30 my $template = 'The value of $n is {$n}.';
32 open my $fh, '>', $file or die "Couldn't write temporary file $file: $!";
33 print $fh $template, "\n";
34 close $fh or die "Couldn't finish temporary file $file: $!";
37 my $obj = Text::Template->new(@_);
38 eval { $obj->fill_in() };
48 my $obj = Text::Template->new(@_);
49 eval { $obj->fill_in() };
58 sub should_be_tainted {
59 ok !Text::Template::_is_clean($_[0]);
63 ok Text::Template::_is_clean($_[0]);
66 # Tainted filename should die with and without UNTAINT option
67 # untainted filename should die without UNTAINT option
68 # filehandle should die without UNTAINT option
69 # string and array with tainted data should die either way
74 should_be_tainted($tfile);
75 should_be_clean($file);
76 should_fail TYPE => 'file', SOURCE => $tfile;
77 should_fail TYPE => 'file', SOURCE => $tfile, UNTAINT => 1;
78 should_fail TYPE => 'file', SOURCE => $file;
79 should_work TYPE => 'file', SOURCE => $file, UNTAINT => 1;
82 open $fh, '<', $file or die "Couldn't open $file for reading: $!; aborting";
83 should_fail TYPE => 'filehandle', SOURCE => $fh;
86 open $fh, '<', $file or die "Couldn't open $file for reading: $!; aborting";
87 should_work TYPE => 'filehandle', SOURCE => $fh, UNTAINT => 1;
91 my $ttemplate = $template;
93 should_be_tainted($ttemplate);
94 should_be_clean($template);
95 should_fail TYPE => 'string', SOURCE => $ttemplate;
96 should_fail TYPE => 'string', SOURCE => $ttemplate, UNTAINT => 1;
97 should_work TYPE => 'string', SOURCE => $template;
98 should_work TYPE => 'string', SOURCE => $template, UNTAINT => 1;
101 my $array = [$template];
102 my $tarray = [$ttemplate];
103 should_fail TYPE => 'array', SOURCE => $tarray;
104 should_fail TYPE => 'array', SOURCE => $tarray, UNTAINT => 1;
105 should_work TYPE => 'array', SOURCE => $array;
106 should_work TYPE => 'array', SOURCE => $array, UNTAINT => 1;
108 # (20-21) Test _unconditionally_untaint utility function
109 Text::Template::_unconditionally_untaint($ttemplate);
110 should_be_clean($ttemplate);
111 Text::Template::_unconditionally_untaint($tfile);
112 should_be_clean($tfile);