Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Regex Dangerous??

by mt2k (Hermit)
on Mar 23, 2001 at 04:45 UTC ( [id://66531]=perlquestion: print w/replies, xml ) Need Help??

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

Could the following regular expression prove to be dangerous to the server:

if ($string =~ /$input{'variable'}/gis) { #Some processing }

The $input{'variable'} variable would be a value entered from a textfield from a CGI script.

I don't think entering commands does anything, but I noticed that you can enter special characters, such as charater classes, parentheses, periods, carets, and dollar signs. So is there any danger of files being deleted, or anything else I would want to class as bad?? Or would it just allow some nice restrictions for a search engine??

So if it is dangerous somehow, I should use:

if ($string =~ /\Q$input{'variable'}/) { #blah blah blah... }
right??

Replies are listed 'Best First'.
Re: Regex Dangerous??
by merlyn (Sage) on Mar 23, 2001 at 04:52 UTC
    Amongst other potential dangers, there's a "denial of service" attack trivially possible with a short regex that fails to match, but fails to match in exponentially countable ways. See Jeffrey Friedl's "Mastering Regular Expressions" for a discussion of this.

    Solution: make sure you have a timeout alarm set.

    -- Randal L. Schwartz, Perl hacker

Re: Regex Dangerous??
by mt2k (Hermit) on Mar 23, 2001 at 04:50 UTC
    Though I just found out I'd have to use something like:
    $input{'variable'} =~ s/\\/\\\\//gs;
    because otherwise you get a server error if you simply type in '\'
      Well, you'll get server errors for a lot more than that. Any invalid regex will bollux you. What you need to do is wrap it in an eval block, and capture the $@ variable. Standard exception-handling stuff.

      -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://66531]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-20 04:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found