I have a Perl script which opens a file, processes it and prints some output. The input file is gzipped.
the path to the $file
is passed to the script as an arugment.
Below is the current solution I'm using:
open(my $fh, "-|", "$gzcat $file") or die("Cannot open $file$!");
The script has failed in Checkmarx's security audit recently, with the following error:
<script> gets user input for the $fh element. This element’s value then flows through the code without being properly sanitized or validated and is eventually displayed to the user in method <method>. This may enable a CrossSite-Scripting attack.
I have tried validating the file exists with perl -f, and also removing unwanted characters using $file =~ s/[^A-Za-z0-9_\-\.\/]//g;
, yet it does not satisfy Checkmarx.
I would like to know what is the proper way of sanitzing an input which contains a path to a file in Perl.