hardburn has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
•Re: Filepath validation and untainting
by merlyn (Sage) on Feb 12, 2003 at 21:24 UTC | |
by clairudjinn (Beadle) on Feb 13, 2003 at 00:21 UTC | |
by submersible_toaster (Chaplain) on Feb 13, 2003 at 00:59 UTC | |
by jonadab (Parson) on Feb 13, 2003 at 04:41 UTC | |
|
Re: Filepath validation and untainting
by fruiture (Curate) on Feb 12, 2003 at 16:54 UTC | |
by hardburn (Abbot) on Feb 12, 2003 at 17:01 UTC | |
by fruiture (Curate) on Feb 12, 2003 at 18:14 UTC |