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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.