leocharre has asked for the wisdom of the Perl Monks concerning the following question:

I need to let users via a web interface, to tell the server to move a file from one place to another (i don't like this either).

This file is on a chunk of filetree that is mounted... So, it seems I need to use backticks and mv command. stuff like File::Copy will not steadily work accross filesystems

I'm running -Tw, and it's getting really angry at me for this.. I'm having a hard time untainting paths like /this/that/that/etc

I get this: Insecure $ENV{PATH} while running with -T switch at move.cgi line 148

This is my untainting and moving..

my %a=(); #their files may have @ signs, yea.. sigh # from if ("$$DMS{CONF}{DOC}/$filepath"=~m/^([\/_\@\w .-]+)$/){ $a{from} = $1; } #to if ("$$DMS{CONF}{DOC}/$$DMS{S}{session_file}/$$DMS{F}{$filepath}{file_ +name}"=~m/^([\/_\@\w .-]+)$/){ $a{to}=$1; } $a{from}=~m/\w/ or die("from failed untaint"); $a{to}=~m/\w/ or die("to failed untaint"); my $err = `mv "$a{from}" "$a{to}"`; # and here we freak out if ($err){ die("mov problem.. [$err]"); }

Is there some other way I should be untainting a path?

Replies are listed 'Best First'.
Re: having horrors untainting a path string for moving a file
by virtualsue (Vicar) on Mar 14, 2006 at 18:39 UTC
    Insecure $ENV{PATH} while running with -T switch at move.cgi line 148

    This error message is trying to tell you to tighten down the $PATH environment setting. What I do is set $ENV{PATH} to '' (clear it) and use a known _absolute_ path for every external program being executed. /bin/mv, /bin/rm, etc. Taint is trying to protect you from being easy prey for a trojan horse attack.

Re: having horrors untainting a path string for moving a file
by ikegami (Patriarch) on Mar 14, 2006 at 18:37 UTC

    You need to untaint $ENV{PATH} because the user could change it to run his mv instead of the system one. This is particularly relevant in setuid scripts.

    In this case, why don't you do

    rename $a{from}, $a{to} or die("mov problem.. [$!]");

      The OP mentioned this may have to work across different mount points, and rename can't do that. And this is why he can't use File::Copy's mv since under the hood that also uses rename.

        Me bad! I didn't notice that. In that case,
        File::Copy::move($a{from}, $a{to});
        or
        system('/bin/mv', $a{from}, $a{to});
        is much safer! He'll might still need to untaint $ENV{PATH} (by setting it to a known value), but there's no shell involved. mv sets the error result, so that can be used instead of capturing the output.