13-taint.t 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!perl -T
  2. # Tests for taint-mode features
  3. use lib 'blib/lib';
  4. use Text::Template;
  5. die "This is the test program for Text::Template version 1.46.
  6. You are using version $Text::Template::VERSION instead.
  7. That does not make sense.\n
  8. Aborting"
  9. unless $Text::Template::VERSION == 1.46;
  10. my $r = int(rand(10000));
  11. my $file = "tt$r";
  12. # makes its arguments tainted
  13. sub taint {
  14. for (@_) {
  15. $_ .= substr($0,0,0); # LOD
  16. }
  17. }
  18. print "1..21\n";
  19. my $n =1;
  20. print "ok ", $n++, "\n";
  21. my $template = 'The value of $n is {$n}.';
  22. open T, "> $file" or die "Couldn't write temporary file $file: $!";
  23. print T $template, "\n";
  24. close T or die "Couldn't finish temporary file $file: $!";
  25. sub should_fail {
  26. my $obj = Text::Template->new(@_);
  27. eval {$obj->fill_in()};
  28. if ($@) {
  29. print "ok $n # $@\n";
  30. } else {
  31. print "not ok $n # (didn't fail)\n";
  32. }
  33. $n++;
  34. }
  35. sub should_work {
  36. my $obj = Text::Template->new(@_);
  37. eval {$obj->fill_in()};
  38. if ($@) {
  39. print "not ok $n # $@\n";
  40. } else {
  41. print "ok $n\n";
  42. }
  43. $n++;
  44. }
  45. sub should_be_tainted {
  46. if (Text::Template::_is_clean($_[0])) {
  47. print "not ok $n\n"; $n++; return;
  48. }
  49. print "ok $n\n"; $n++; return;
  50. }
  51. sub should_be_clean {
  52. unless (Text::Template::_is_clean($_[0])) {
  53. print "not ok $n\n"; $n++; return;
  54. }
  55. print "ok $n\n"; $n++; return;
  56. }
  57. # Tainted filename should die with and without UNTAINT option
  58. # untainted filename should die without UNTAINT option
  59. # filehandle should die without UNTAINT option
  60. # string and array with tainted data should die either way
  61. # (2)-(7)
  62. my $tfile = $file;
  63. taint($tfile);
  64. should_be_tainted($tfile);
  65. should_be_clean($file);
  66. should_fail TYPE => 'file', SOURCE => $tfile;
  67. should_fail TYPE => 'file', SOURCE => $tfile, UNTAINT => 1;
  68. should_fail TYPE => 'file', SOURCE => $file;
  69. should_work TYPE => 'file', SOURCE => $file, UNTAINT => 1;
  70. # (8-9)
  71. open H, "< $file" or die "Couldn't open $file for reading: $!; aborting";
  72. should_fail TYPE => 'filehandle', SOURCE => \*H;
  73. close H;
  74. open H, "< $file" or die "Couldn't open $file for reading: $!; aborting";
  75. should_work TYPE => 'filehandle', SOURCE => \*H, UNTAINT => 1;
  76. close H;
  77. # (10-15)
  78. my $ttemplate = $template;
  79. taint($ttemplate);
  80. should_be_tainted($ttemplate);
  81. should_be_clean($template);
  82. should_fail TYPE => 'string', SOURCE => $ttemplate;
  83. should_fail TYPE => 'string', SOURCE => $ttemplate, UNTAINT => 1;
  84. should_work TYPE => 'string', SOURCE => $template;
  85. should_work TYPE => 'string', SOURCE => $template, UNTAINT => 1;
  86. # (16-19)
  87. my $array = [ $template ];
  88. my $tarray = [ $ttemplate ];
  89. should_fail TYPE => 'array', SOURCE => $tarray;
  90. should_fail TYPE => 'array', SOURCE => $tarray, UNTAINT => 1;
  91. should_work TYPE => 'array', SOURCE => $array;
  92. should_work TYPE => 'array', SOURCE => $array, UNTAINT => 1;
  93. # (20-21) Test _unconditionally_untaint utility function
  94. Text::Template::_unconditionally_untaint($ttemplate);
  95. should_be_clean($ttemplate);
  96. Text::Template::_unconditionally_untaint($tfile);
  97. should_be_clean($tfile);
  98. END { unlink $file }