I'm writing a module which (among other things) provides a series of validators on various types of user input. Each validator is a scalar holding a referance to a subroutine. The subroutine returns a list, with the first element as the untainted data, and the second element as a string (any string will do). If the validation fails, the first element is undef and the second element is an error message.
My main problem is with the filepath validator. I need to check for both *nix and Win32 filepaths. I do not need to check if the file exists or if it is among a list of valid filepaths (this is documented behavior for the module).
Current implementation, which hasn't been tested yet:
my $FILE = sub { my $file = shift; if($file =~ m!\A ([A-Za-z]:\\)? # Optional DOS drive (such as 'C:\') ( [/\\]*? # Allow either '/' or '\' as a directory seperator + [-\.\w\s]+? # Allow certain characters as the filename )+ \z!x) { return ($1 . $2, "Passed"); } else { return (undef, "$file is not a valid filepath"); } };
Am I doing anything dangerously naive? Are there modules already available to do this? There are probably a lot more valid chars in many filesystems than what I'm checking above. What are some generally good special chars to check for?
In reply to Filepath validation and untainting by hardburn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |