in reply to How do you clean up user specified filenames?

See Sanitizing user-provided path/filenames for something I occasionally recommend to sanitize user-provided pathnames. Before that, I recommend using a token system of some kind. Let the user pick which template they want to use, but don't let their input be a path/filename. Let it be something your script sees and maps to a path/filename.

One of the surest ways you can avoid shell metacharacter problems is simply not to use a function that passes them via a shell. Use sysopen instead of open, for example. Also see Avoiding surprises using 'open' for working around unexpected bits of input using Perl's normal open function.

Another common solution is simply to restrict what people can specify in filenames:

tr/A-Za-z_.-//dc;

Replies are listed 'Best First'.
Re: Re: How do you clean up user specified filenames?
by tomhukins (Curate) on Feb 21, 2001 at 21:13 UTC
    The problem with tr/A-Za-z_.-//dc; is that the . character could be used to attempt to open the current or parent directory.

    I'm paranoid enough to do something like

    tr/A-Za-z_.-//dc; s/^\.+$//;
    to make sure the user doesn't enter a string containing only dots.