taint.t 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!perl -T
  2. # Tests for taint-mode features
  3. use strict;
  4. use warnings;
  5. use lib 'blib/lib';
  6. use Test::More tests => 21;
  7. use File::Temp;
  8. use_ok 'Text::Template' or exit 1;
  9. if ($^O eq 'MSWin32') {
  10. # File::Temp (for all versions up to at least 0.2308) is currently bugged under MSWin32/taint mode [as of 2018-09]
  11. # ... 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
  12. # ref: [File-Temp: Fails under -T, Windows 7, Strawberry Perl 5.12.1](https://rt.cpan.org/Public/Bug/Display.html?id=60340)
  13. ($ENV{TEMP}) = $ENV{TEMP} =~ m/^.*$/gmsx; # untaint $ENV{TEMP}
  14. ($ENV{TMP}) = $ENV{TMP} =~ m/^.*$/gmsx; # untaint $ENV{TMP}
  15. }
  16. my $tmpfile = File::Temp->new;
  17. my $file = $tmpfile->filename;
  18. # makes its arguments tainted
  19. sub taint {
  20. for (@_) {
  21. $_ .= substr($0, 0, 0); # LOD
  22. }
  23. }
  24. my $template = 'The value of $n is {$n}.';
  25. open my $fh, '>', $file or die "Couldn't write temporary file $file: $!";
  26. print $fh $template, "\n";
  27. close $fh or die "Couldn't finish temporary file $file: $!";
  28. sub should_fail {
  29. my $obj = Text::Template->new(@_);
  30. eval { $obj->fill_in() };
  31. if ($@) {
  32. pass $@;
  33. }
  34. else {
  35. fail q[didn't fail];
  36. }
  37. }
  38. sub should_work {
  39. my $obj = Text::Template->new(@_);
  40. eval { $obj->fill_in() };
  41. if ($@) {
  42. fail $@;
  43. }
  44. else {
  45. pass;
  46. }
  47. }
  48. sub should_be_tainted {
  49. ok !Text::Template::_is_clean($_[0]);
  50. }
  51. sub should_be_clean {
  52. ok Text::Template::_is_clean($_[0]);
  53. }
  54. # Tainted filename should die with and without UNTAINT option
  55. # untainted filename should die without UNTAINT option
  56. # filehandle should die without UNTAINT option
  57. # string and array with tainted data should die either way
  58. # (2)-(7)
  59. my $tfile = $file;
  60. taint($tfile);
  61. should_be_tainted($tfile);
  62. should_be_clean($file);
  63. should_fail TYPE => 'file', SOURCE => $tfile;
  64. should_fail TYPE => 'file', SOURCE => $tfile, UNTAINT => 1;
  65. should_fail TYPE => 'file', SOURCE => $file;
  66. should_work TYPE => 'file', SOURCE => $file, UNTAINT => 1;
  67. # (8-9)
  68. open $fh, '<', $file or die "Couldn't open $file for reading: $!; aborting";
  69. should_fail TYPE => 'filehandle', SOURCE => $fh;
  70. close $fh;
  71. open $fh, '<', $file or die "Couldn't open $file for reading: $!; aborting";
  72. should_work TYPE => 'filehandle', SOURCE => $fh, UNTAINT => 1;
  73. close $fh;
  74. # (10-15)
  75. my $ttemplate = $template;
  76. taint($ttemplate);
  77. should_be_tainted($ttemplate);
  78. should_be_clean($template);
  79. should_fail TYPE => 'string', SOURCE => $ttemplate;
  80. should_fail TYPE => 'string', SOURCE => $ttemplate, UNTAINT => 1;
  81. should_work TYPE => 'string', SOURCE => $template;
  82. should_work TYPE => 'string', SOURCE => $template, UNTAINT => 1;
  83. # (16-19)
  84. my $array = [$template];
  85. my $tarray = [$ttemplate];
  86. should_fail TYPE => 'array', SOURCE => $tarray;
  87. should_fail TYPE => 'array', SOURCE => $tarray, UNTAINT => 1;
  88. should_work TYPE => 'array', SOURCE => $array;
  89. should_work TYPE => 'array', SOURCE => $array, UNTAINT => 1;
  90. # (20-21) Test _unconditionally_untaint utility function
  91. Text::Template::_unconditionally_untaint($ttemplate);
  92. should_be_clean($ttemplate);
  93. Text::Template::_unconditionally_untaint($tfile);
  94. should_be_clean($tfile);