Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: So, now what are taints?

by cLive ;-) (Prior)
on Mar 28, 2004 at 22:38 UTC ( #340447=note: print w/replies, xml ) Need Help??

in reply to So, now what are taints?

It's a way of (hopefully) stopping you from making silly mistakes.

Every piece of data that comes to the script that is used outside the script is considered tainted unless you explicitly grab it from a regular expression (I think, there may be other ways to untaint though).

Why is this useful? Let's say you had a script that uploaded a domain from a web page and you wanted to ping that domain.

my $q = CGI->new(); my $domain = $q->param('domain'); my $result = `ping $domain`;
Under taint, this would die because you're trying to pipe some untainted data to an external program. Imagine what would happen if some malicious user uploaded "localhost; rm -rf /" as the domain name!

So, under taint, you would need to explicitly grab the domain from the variable:

my $domain=''; $q->param('domain') =~ /^([a-zA-Z0-9\.]+)$/ and $domain = $1;
That's just a rough expression to grab the domain. The point is that you know that there won't be anything malicious in $domain when it's assigned.

But, untainting data in itself does not protect you. You could, if you wished, untaint it like this:

$q->param('domain') =~ /^(.+)$/ and $domain = $1;
but you won't have added to your security understanding if you do :) There are times though, when you don't care what a value contains and, in those instances, it would be perfectly acceptable to untaint like that. Just as long as you know for sure!

I wrote a little article on it here if you're interested.


cLive ;-)

Replies are listed 'Best First'.
Re: Re: So, now what are taints?
by matija (Priest) on Mar 29, 2004 at 05:54 UTC
    You are forgetting not mentioning that there are actualy two pieces of data there that need untainting: one is the domain parameter obtained from the CGI, but the other is the PATH of your program. If you use backticks like that, and don't set up your PATH explicitly, perl -T will complain.

    That appears not to make sense in a CGI environment, but it makes a lot of sense when you're writing setuid root scripts that can be run from the command line.

      Indeed. Sorry - but it is mentioned in the article I linked to that I wrote on untainting :)

      cLive ;-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://340447]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2018-06-17 23:17 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (107 votes). Check out past polls.