jatill has asked for the wisdom of the Perl Monks concerning the following question:

I have taint mode turned on for a cgi script, and I can verify that it is working with form data. However, when I enter the url my_page.cgi?page_id=123, it doesn't seem like page_id ends up tainted. Since this is user-supplied data, I would have expected taint to kill my script when I try to look up a database record and bind in page_id. When I run this from the command line, taint does trigger. Is it as simple as taint ignores 'get' params, or is there more to this mystery? Thanks ~jeff

Replies are listed 'Best First'.
Re: Taint and get params
by naikonta (Curate) on Aug 22, 2007 at 18:10 UTC
    Did you turn the taint mode on for DBI? Like, specifying the attribute { Taint => 1 } in connect statement?

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

      My DB taint options are: Taint => 0, TaintIn => 1, TaintOut => 0,
        It works for me, I mean I get the error about insecure dependency.
        #!/usr/bin/perl -T use strict; use warnings; use DBI; use CGI; use Data::Dumper; my $cgi = CGI->new; print $cgi->header(-type => 'text/plain'); my $dbh = DBI->connect(qw(dbi:mysql:test user pass), {RaiseError=>1, T +aint=>0, TaintIn=>1, TaintOut=>0}); my $id; ($id = $cgi->param('id')) ? get_user() : normal_page(); sub get_user { my $sth = $dbh->prepare('select * from user where id = ?'); #($id) = $id =~ /^(\d+)$/; $sth->execute($id); my $user = $sth->fetchrow_hashref; $sth->finish; die "There's no such user id ($id)\n" unless defined $user; print Dumper($user); } sub normal_page { print 'Hello there'; }
        And I got this from the error log when calling user.cgi?id=1:
        Insecure dependency in parameter 1 of DBI::st=HASH(0x8265f88)->execute + method call while running with -T switch at /path/to/user.cgi line 2 +0.
        If I uncomment the untainting line, I got this on the browser (as found in the table):
        $VAR1 = { 'pass' => 'perl', 'location' => undef, 'name' => 'perl', 'id' => '1' };

        Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: Taint and get params
by clinton (Priest) on Aug 22, 2007 at 19:35 UTC
    It should be tainted.

    Are you sure you're not untainting somehow (eg with a regex)? Can you present a small CGI program which demonstrates the problem?

    Clint

      Deep within a module I am using I found this code lurking: # Untaint all ENV variables foreach ( keys %ENV ) { $ENV{$_} =~ m/(.*)/; $ENV{$_} = $1; } So it looks like I'm untainting QUERY_STRING, which would be my problem. Thanks for your help pointing me in the right direction.