Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
you cannot directly untaint a hash value in perl 5.6.1.

i should have tested better before i posted. below is a sample script you can run to see for yourself. it creates a file in the current directory named 'file'. then it asks you to type in 'file' (minus the quotes.) this taints whatever variable is assigned the value entered. i test three methods of untainting: direct scalar, temp scalar and reassign, and direct hash value. the direct hash value method fails.

#!/usr/bin/perl -T use strict; use warnings; $ENV{PATH} = ''; { local *FH; open(FH, '>', 'file') or die "can't create 'file'"; } print "type 'file' to test: "; my $file_in = <>; my ($file, %regex, %params); $regex{A} = qr/^(\w+)$/; # untaint scalar ($file) - works fine $file = $file_in; # tainted ( $file ) = ( $file =~ /$regex{A}/ ); # UNTAINTED { local *FH; open( FH, '+>', $file ) or die "$file : untaint \$file"; close FH; } # untaint scalar ($temp) - works fine %params = (); $file = $file_in; # tainted $params{A} = $file; # tainted my $temp = $params{A}; # tainted ( $params{A} ) = ( $temp =~ /$regex{A}/ ); # UNTAINTED { local *FH; open( FH, '+>', $params{A} ) or die "$params{A} : untaint \$temp"; close FH; } # untaint scalar hash value ($params{A}) - fails! %params = (); $file = $file_in; # tainted $params{A} = $file; # tainted ( $params{A} ) = ( $params{A} =~ /$regex{A}/ ); # STILL TAINTED { local *FH; open( FH, '+>', $params{A} ) or die "$params{A} : untaint \$params +{A}"; close FH; }
the error i get on cygwin with perl561 is:

$ type 'file' to test: file Insecure dependency in open while running with -T switch at ./t-taint. +pl line 46, <> line 1.

i have modified my code to untaint a temp scalar and reassign to the hash like so:

# untaint parameters for( keys %params ) { ( display_message( $messages{error} ) && exit ) unless ref($valid_params{$_}) eq 'Regexp'; my $temp = $params{$_}; ### <--- added this variable if( $temp =~ /$valid_params{$_}/ ) ### <--- changed this line { $params{$_} = $1; } else { display_message( $messages{error} ) && exit; } }
and all is well with the world.

~Particle ;

In reply to cannot directly untaint a hash value!!! by particle
in thread proper untainting and use of ref by particle

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (7)
    As of 2020-12-01 16:10 GMT
    Find Nodes?
      Voting Booth?
      How often do you use taint mode?

      Results (12 votes). Check out past polls.