I am taking in filenames from "the evil user", and would like to know the most effective way to escape them. Basically, I want to avoid accessing files in lower directories using ../ or similar. I am not using the filenames to execute any code via the shell, so I think most metacharacters are OK. My code does the following:
my $filename = take_user_input(); $filename =~ s/\.\./\\\.\\\./g; # this is the part in question open IN, "<", $filename or die "Cannot open file '$filename' for input: $!\n"; print join "", <IN>; close IN;
Do you think this simple regular expression is sufficient, or are there other things I should keep in mind? In case you're wondering: yes this is for a CGI, but I'm not so sure it matters. Nothing further is done with this filename, other than opening it and printing the contents to the browser.
In reply to escaping filenames taken in via user input by revdiablo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |