Good advice. Security can also be improved by using the safer "flavor" of system. If you pass system a string, it will use a sub-shell to figure out what that string means. This can easily be abused and is almost always overkill anyway.

It's safer to pass system a list. The first element will be treated as the program name to fork, the rest are params to said program. Perhaps a concrete example is best:

#!/usr/bin/perl -w use strict; # malicious input my $dir = '/tmp; echo "GOT YA"'; # system using shell print "AS STRING\n"; system("ls $dir"); # system w/o the shell print "\nAS LIST\n"; system('ls', $dir); =OUTPUT AS STRING [snip -- same as `ls /tmp`] GOT YA AS LIST ls: /tmp; echo "GOT YA": No such file or directory
Notice how 'ls /tmp; echo "GOT YA"' is parsed into two separate commands by the shell. By calling system in a safer way we can force everything in $dir to be treated as a filename.

-Blake


In reply to Re: Apple, quoting, and system() by blakem
in thread Apple, quoting, and system() by Masem

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.