in reply to Re: isa() and taint checking
in thread isa() and taint checking

Sorry for being too brief.

I've put together a longer example, and tested this. It works without -T, but I can't figure out why it won't work with Taint. Help is much appreciated.

File 1 - example.pl
#!/usr/bin/perl -wT use strict; use lib '.'; use Objects::Parent; loadObjects('Objects'); sub loadObjects { my $directory = shift; opendir(DIR, $directory) or die "Object Templates can't be loa +ded: Can't open $directory: $!"; while (defined(my $file = readdir(DIR))) { if($file =~ /^(.*\.pm)$/) { $file = $1; $file = $directory."/".$file; require $file; $file =~ s/\//::/g; $file =~ s/\.pm$//; if($file->isa('Objects::Parent')) { my $tempObject = $file->new(); $tempObject->doSomething(); } } } }
File 2 - Objects/Parent.pm
package Objects::Parent; sub new { bless {}, shift; } sub doSomething { print "Something\n"; } 1;
File 3 - Objects/Child.pm
package Objects::Child; use base 'Objects::Parent'; 1;

Replies are listed 'Best First'.
(bbfu) (cheap hack) Re3: isa() and taint checking
by bbfu (Curate) on Oct 17, 2002 at 18:45 UTC

    A cheap hack to get around the problem:

    sub loadObjects { my $directory = shift; opendir(DIR, $directory) or die "Object Templates can't be loaded: Can't open $directory: $ +!"; while (defined(my $file = readdir(DIR))) { if($file =~ /^(.*\.pm)$/) { $file = $1; $file = $directory."/".$file; my $file = $file; # Cheap hack require $file; $file =~ s/\//::/g; $file =~ s/\.pm$//; if($file->isa('Objects::Parent')) { my $tempObject = $file->new(); $tempObject->doSomething(); } } }

    Otherwise, it looks to me like a bug in perl. *shrug* Maybe someone better than I can shed some light.

    bbfu
    Black flowers blossum
    Fearless on my breath

      Supposition: taint is associated with the variable, not the value. Assigning to the variable doesn't clear the taint flag. Creating a new variable does. That's why my example worked.

      Solution #1 is to fix Perl, if that's the proper remedy. Solution #2 is not to reuse $file to hold the contents of $1.

        Yep. Assigning to a new variable does work (which is really what my code above does, of course). Problem is, if the taintedness is associated with the variable, and not with the value, there is effectively no way to untaint a variable. Even if you assign a literal to the variable, it is still tainted (and I just tested: it is still tainted). This doesn't seem right to me, and doesn't seem to be what is intended. Especially since the example code in perlsec for untainting data would actually fail.

        From perlsec:

        Here's a test to make sure that the data contains nothing but "word" characters (alphabetics, numerics, and under- scores), a hyphen, an at sign, or a dot. if ($data =~ /^([-\@\w.]+)$/) { $data = $1; # $data now untainted } else { die "Bad data in $data"; # log this somewhere }

        Also, this "persistent variable taintedness" only seems to affect $class->isa().

        #!/usr/bin/perl -wT package Parent; 1; package Child; @ISA = qw(Parent); 1; package main; %ENV = (); use strict; our $isa; $|++; print "Type in something: "; $isa = <STDIN>; $isa = "/bin/ls"; print "(qx) ", (eval { qx($isa); 1 } ? "Untainted" : "Tainted"), "\n" +; $isa = "Child"; print "(isa) ", ($isa->isa("Parent") ? "Untainted" : "Tainted"), "\n";

        Produces:

        Type in something: blah (qx) Untainted (isa) Tainted

        That really doesn't seem right to me.

        bbfu
        Black flowers blossum
        Fearless on my breath

Re: Re: Re: isa() and taint checking
by antifun (Sexton) on Oct 17, 2002 at 19:04 UTC
    OK. Something must be screwing up the tainting of $file, because if you change that isa test into this
    $file =~ /(.*)/; my $ff = $1; if ($ff->isa('Objects::Parent')) {
    it works as you'd expect.

    Now, I don't really know why this is the case. The Taint module's tainted function says $file isn't tainted. No error is ever seen, it just silently fails. My tests are on 5.6.1. Anyone have any better idea what's going on here?
    ---
    "I hate it when I think myself into a corner."
    Matt Mitchell