If i hardcode the values of dir and zip like this cd /dog/cat and unzip rat.zip they work just fine...
I am really confused by this. cd and unzip are not built-in perl functions. As kyle suggested, you normally would do something like this:
chdir($dir) && system("unzip", $unzip);
The && is there to make sure that the unzip command is run only if the chdir succeeds.

However, since you are using these commands in a CGI script, you need to take extra precautions to make sure that users cannot subvert your script to make it do something you don't want them to do. That means you have to validate the input to make sure it looks like what you expect it to look like. For example, I'm sure you don't want users to be able to chdir to just any directory and unzip just any file.

Perl has a nice feature called tainting which helps you keep track of user input which needs to be vetted before it can be passed on to commands like chdir and system. You can enable it using the -T switch in the she-bang line of your script:

#!/usr/bin/perl -T
When enabled, user input (like posted form data) will be marked as tainted, and if you try to pass the data directly to a command like chdir or system, perl will throw an exception. To untaint the data, you have to examine it with a regular expression:
my $dir = $query->param('dir'); my $zip = $query->param('zip'); die "invalid dir" unless ($dir =~ m/^([a-zA-Z0-9]+)\z/); my $valid_dir = $1; # $valid_dir is untainted die "invalid zip file" unless ($zip =~ m/^([a-zA-Z0-9]+)\z/); my $valid_zip = $1; chdir($valid_dir) && system("unzip", $valid_zip);

In reply to Re: HTML Form and Perl by pc88mxer
in thread HTML Form and Perl by workman_m

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.