Doing a similar thing, I was disappointed to find that none of the File::* modules wouldn't rationlise a path containing relative elements,and came up with this.

#! perl -slw use strict; sub sanitizeDir { require Cwd; my( $path ) = @_; my( $cwd ) = Cwd::getcwd() =~ m[^(.*)$]; return unless do{ local %ENV; $path =~ s[^(.*)$][$1]; chdir $path; + }; my $sanitized = Cwd::getcwd(); chdir $cwd; return $sanitized =~ s[^$cwd][$cwd] ? $sanitized : (); } my $dubiousPath = $ARGV[0]; my $absPath = sanitizeDir( $dubiousPath ); if( defined $absPath ) { print 'Absolute path: ', $absPath; } else { print 'Invalid path: ', $dubiousPath; }

Some tests

P:\test>perl -T junk.pl8 . Absolute path: P:/test P:\test>perl -T junk.pl8 .. Invalid path: .. P:\test>perl -T junk.pl8 ./.. Invalid path: ./.. P:\test>perl -T junk.pl8 ./../. Invalid path: ./../. P:\test>perl -T junk.pl8 ./used Absolute path: P:/test/used P:\test>perl -T junk.pl8 ./used/.. Absolute path: P:/test P:\test>perl -T junk.pl8 ./used/../.. Invalid path: ./used/../.. P:\test>perl -T junk.pl8 ./used/././t/ Absolute path: P:/test/used/t P:\test>perl -T junk.pl8 ./used/././t/../ Absolute path: P:/test/used P:\test>perl -T junk.pl8 ./used/././t/../.. Absolute path: P:/test P:\test>perl -T junk.pl8 ./used/././t/../.././.. Invalid path: ./used/././t/../.././..

The basic idea is to use the OS to rationaise the path and convert it to an absolute path. You can then check that it starts with the root of the subtree you want to expose. In the example, it verifies that the path specified is in the subtree below the current working directory, but you could pass in the required cwd as a second argument to the sub.

It returns a fully rationalised and untainted, absolute path, or undef.

This hasn't been tested on a vunerable system, so you will need to verify it for yourself, but hopefully their are enough experienced eyes here to spot any weakness in the approach or implementation.

I believe it should be portable, but it has only neen tested under Win32.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!
Wanted!


In reply to Re: Cleaning up directory paths. by BrowserUk
in thread Cleaning up directory paths. by SavannahLion

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.