Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Security with open() in CGI scripts

by Trimbach (Curate)
on Feb 26, 2002 at 04:36 UTC ( #147476=note: print w/replies, xml ) Need Help??

in reply to Security with open() in CGI scripts

...and as an add on to rob_au's excellent post, here's an idea to increase security, especially for open calls and such. Instead of passing directory names and filenames directly from the browser (which means worrying about shell escape characters and such) why not pass tokens that correspond to directories and files that are hard-coded into your script. So instead of
my $template = $theme->{ThemeDir}."index.html";
which makes you worry about $theme->{ThemeDir} why not
my %themes = {1 => '/dir1/', 2 => '/dir2/', 3 => '/dir3/'}; my $theme = param(theme); my $filename = "$themes{$theme}.index.html"; open (FILE, $filename);
Now everything is under control... if $theme ever contains anything it isn't supposed to then the worst thing that can happen is the hash lookup fails and the open fails. No way malicious code can hurt you, all because you insulated your open by using a hash.

Of course, this isn't always practical, but most of the time it's a good idea. This won't work if you let users name their own files, and upload (or create) those files directly on the server, but if you're doing that you're gonna have to jump through more hoops than we've covered here in order to maintain security.

Gary Blackburn
Trained Killer

Replies are listed 'Best First'.
Re: Re: Security with open() in CGI scripts
by brianarn (Chaplain) on Feb 26, 2002 at 05:48 UTC
    Another thought on the token idea here...

    I'd recommend having another file external to the script, which contains some quick perl code to create the token list, such as
    my %tokens = {1 => '/dir1/', 2 => '/dir2/', 3 => '/dir3/};
    <blatently stolen from Gary's reply ;)>

    and then just require this file when you need to reference the token. This way, it's easy to add to the themes without having to find where it's at in some script. I'm personally all about the use of extra files for data like this - it helps keep script sizes smaller and helps to separate the code from the data.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://147476]
[karlgoethebier]: shmem: yes - i f you manage it to get on the bike. and pray if you must stop...
[shmem]: in the french alpes, at the end of a gravel road, we had a beer at a hut - and heard an enduro coming up.
[shmem]: not by the road, but from there below where you wouldn't want to walk.
[shmem]: the biker stopped the machine at the table and took off his helmet.
[shmem]: long white hair, long beard the same, then he proceeded to get off the bike
[shmem]: saying "biking itself isn't that much, but getting up and down - hell!"
[shmem]: up and down: on and off the bike, of course

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2017-06-25 20:08 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (570 votes). Check out past polls.