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

I am converting a perl program that uses Net::FTP to use Net::SFTP::Foreign. It is quite nifty and has all manner of fine capabilities. We run the ftp version as root, actually its run out of a non root crontab where the program is owned by root with it's sticky bit set. Running perl as root automatically invokes trust (-T) so I have to untaint all my variables. No problem with the Net::FTP module, but Net::SFTP::Foreign only has one response to trust, FAIL!
GetOptions(
   "r=s" => \$opt_remote_dir,
);

$opt_remote_dir =~ /^(.*)$/;
if ( ! $sftp->setcwd( "$opt_remote_dir" ) ) {

$sftp->error returns:

Insecure argument '/test/in' on method call while running with -T switch at /ftp/sbin/sftp.pl line 1674
Even if I untaint the entire command:
$cd_remote_dir="\$sftp->setcwd( \"$opt_remote_dir\" )";
if ( ! eval $cd_remote_dir )
I get the same error. If I run the script as a non root user with the -T switch I get the same thing. Evidently there is an issue with running Net::SFTP::Foreign under taint. Hope you can help. I invested a lot of time and effort in Net::SFTP::Foreign. I hate to trash it at the last moment.

Thanks

Replies are listed 'Best First'.
Re: untainting Net::SFTP::Foreign
by salva (Canon) on Mar 22, 2011 at 14:34 UTC
    Net::SFTP::Foreign requires that all the arguments passed to its methods are untainted:
    $opt_remote_dir =~ /^(.*)$/; $sftp->setcwd($1); $sftp->error and die "Unable to change directory: " . $sftp->error;