This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Change #27951 added tests to t/op/taint.t involving taint, opening
[perl5.git] / t / op / taint.t
index 3d333c8..f9bb604 100755 (executable)
@@ -17,8 +17,7 @@ use Config;
 use File::Spec::Functions;
 
 BEGIN { require './test.pl'; }
-plan tests => 245;
-
+plan tests => 255;
 
 $| = 1;
 
@@ -45,6 +44,7 @@ my $Is_MSWin32  = $^O eq 'MSWin32';
 my $Is_NetWare  = $^O eq 'NetWare';
 my $Is_Dos      = $^O eq 'dos';
 my $Is_Cygwin   = $^O eq 'cygwin';
+my $Is_OpenBSD  = $^O eq 'openbsd'
 my $Invoke_Perl = $Is_VMS      ? 'MCR Sys$Disk:[]Perl.' :
                   $Is_MSWin32  ? '.\perl'               :
                   $Is_MacOS    ? ':perl'                :
@@ -1140,12 +1140,81 @@ TERNARY_CONDITIONALS: {
 
 {
     my @a;
+    local $::TODO = 1;
     $a[0] = $^X;
     my $i = 0;
     while($a[0]=~ m/(.)/g ) {
        last if $i++ > 10000;
     }
     cmp_ok $i, '<', 10000, "infinite m//g";
-    
 }
 
+SKIP:
+{
+    my $got_dualvar;
+    eval 'use Scalar::Util "dualvar"; $got_dualvar++';
+    skip "No Scalar::Util::dualvar" unless $got_dualvar;
+    my $a = Scalar::Util::dualvar(3, $^X);
+    my $b = $a + 5;
+    is ($b, 8, "Arithmetic on tainted dualvars works");
+}
+
+# opening '|-' should not trigger $ENV{PATH} check
+
+{
+    SKIP: {
+       skip "fork() is not available", 3 unless $Config{'d_fork'};
+       skip "opening |- is not stable on threaded OpenBSD with taint", 3
+            if $Config{useithreads} && $Is_OpenBSD;
+
+       $ENV{'PATH'} = $TAINT;
+       local $SIG{'PIPE'} = 'IGNORE';
+       eval {
+           my $pid = open my $pipe, '|-';
+           if (!defined $pid) {
+               die "open failed: $!";
+           }
+           if (!$pid) {
+               kill 'KILL', $$;        # child suicide
+           }
+           close $pipe;
+       };
+       test $@ !~ /Insecure \$ENV/, 'fork triggers %ENV check';
+       test $@ eq '',               'pipe/fork/open/close failed';
+       eval {
+           open my $pipe, "|$Invoke_Perl -e 1";
+           close $pipe;
+       };
+       test $@ =~ /Insecure \$ENV/, 'popen neglects %ENV check';
+    }
+}
+
+{
+    package AUTOLOAD_TAINT;
+    sub AUTOLOAD {
+        our $AUTOLOAD;
+        return if $AUTOLOAD =~ /DESTROY/;
+        if ($AUTOLOAD =~ /untainted/) {
+            main::ok(!main::tainted($AUTOLOAD), '$AUTOLOAD can be untainted');
+        } else {
+            main::ok(main::tainted($AUTOLOAD), '$AUTOLOAD can be tainted');
+        }
+    }
+
+    package main;
+    my $o = bless [], 'AUTOLOAD_TAINT';
+    $o->$TAINT;
+    $o->untainted;
+}
+
+{
+    # tests for tainted format in s?printf
+    eval { printf($TAINT . "# %s\n", "foo") };
+    like($@, qr/^Insecure dependency in printf/, q/printf doesn't like tainted formats/);
+    eval { printf("# %s\n", $TAINT . "foo") };
+    ok(!$@, q/printf accepts other tainted args/);
+    eval { sprintf($TAINT . "# %s\n", "foo") };
+    like($@, qr/^Insecure dependency in sprintf/, q/sprintf doesn't like tainted formats/);
+    eval { sprintf("# %s\n", $TAINT . "foo") };
+    ok(!$@, q/sprintf accepts other tainted args/);
+}