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

Hi Monks.

I've added code to purge files in a temp directory whenever I log into a website as admin.

As I understand it, Perl won't let you unlink files when taint mode is activated in the shebang line.

Aside from running a crontab on the server to periodically tidy up my temp folder, is there another way to do this using Perl (without compromising my site's security by foregoing taint mode)? Or is best practice to use a crontab?

Thank you!

Replies are listed 'Best First'.
Re: Unlinking Files and Taint Mode
by wind (Priest) on Apr 05, 2011 at 19:07 UTC

    You can still unlink files while in taint mode. At least the below script works fine:

    #!/usr/bin/perl -wT use CGI; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use strict; use warnings; unlink "temp.txt" or die "Can't unlink: $!"; print CGI->header(); print q{<html><body>Hello World</body></html>}; 1; __END__

    When perlsec mentions unlink in its example, it's only with regard to the fact that $arg is tainted. If you want to use tainted data, you just need to launder it first.

Re: Unlinking Files and Taint Mode
by chrestomanci (Priest) on Apr 05, 2011 at 21:15 UTC

    Best practice is to do that sort of thing via the crontab if you can. You want to design the systems you administer to look after themselves as much as possible, and removing old temp files is something that ought to be possible to do automatically.

    If you have to log in yourself every few days to manually clean up the temp files, then what happens if you want to take 2 weeks holiday. (Or you have an accident and spend 2 weeks in hospital. You have better things to do than to do manual tidying that the system ought to be able to do for itself.

    For other things that cannot be done automatically, as design pattern I have frequently seen in the past is to have a web front end that interacts with the user and gets a list of what needs doing. The list is then written to disc or into a database. Another more privileged process running as a demon, or from the cron reads the database or files and does the work.

    For example a couple of years ago I was working on a Corporate Intranet site. There was a web based tool for adjusting user accounts, group memberships and privileges. The web front end ran as a mod-perl process in the web server, and was sand-boxed for security reasons. Each user account change was written to an unprivileged database by the web front end. Another demon ran as root, read the queue of pending changes from the database and made the necessary changes to the Unix NIS and Windows Active directory servers.

Re: Unlinking Files and Taint Mode
by Anonymous Monk on Apr 05, 2011 at 21:46 UTC

    Thank you for the help.

    I'm actually using a crontab service on a company intranet now. I built a custom flash widget that talks to NOAA's SOAP server and retrieves XML weather data. I parse the XML and send variables to flash for display in the widget. The crontab runs periodically in the background. It executes a perl script that talks to NOAA and tidys up afterwards. For the most part this service runs fine, but every now and then it fails and then I'll get a call because nobody knows what the weather is.

    I'm going to be in this site all the time, but I hear what you're saying. Maybe I'll do it both ways.

    I just put the taint switch back in the shebang line and ran the program. It appears to be working ... though I could've sworn it wasn't last Friday (until I removed the taint switch).

    Thanks again for your insight guys.