As it is, you find that your data is still tainted after with that regular expression - This is because of the 'valid' parsing of the '/' character by your regular expression. To correctly untaint data, you need to exclude everything except "word" characters (alphabetics, numerics and underscores), hyphens, at characters and a single dot - The inclusion of the forward-slash character allows data to remain tainted because it is exceptionally easy for parsed data to reference files outside the 'permitted scope' of your application - For example, if your routine was supposed to allow the deletion of trees under the directory /home/<user-entered-directory>, a maligned user could easily pervert this code by entering /etc and causing great harm to your system.

The recommended regular expression for untainting data (from perlsec) would be:

if ($data =~ /^([-\@\w.]+)$/) { $data = $1; # $data now untainted } else { die "Bad data in $data"; # log this somewhere }

This may however mean that you will have to modify your code to run under -T and possibly a chroot environment or else you could have potentially nasty users causing harm to your system.

Have a read through the "Laundering and Detecting Tainted Data" and "Cleaning Up Your Path" sections of perlsec.

Update - See davorg's post here for an update on this problem.

 

Ooohhh, Rob no beer function well without!


In reply to Re: Re: Re: Insecure dependency on perl module by rob_au
in thread Insecure dependency on perl module by Anonymous Monk

Title:
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, details, 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, summary, 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.