Win has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Preventing injection attacks
by Joost (Canon) on Apr 02, 2007 at 14:03 UTC | |
Attempting to catch malicious input is misguided in most cases. You should either only allow known good - verifiable - input or make sure the content of the input doesn't matter. update: I think there is room for a Perl module that can screen against all attacks through stored procedures of any database app.And the problem with that is that you won't know about all potential attacks. Besides that I suspect the code to detect all known attacks would soon be orders of magnitudes larger than the code it's supposed to protect - with all the potential for bugs in and/or security holes caused by the scanning code. | [reply] |
by Ionitor (Scribe) on Apr 02, 2007 at 14:12 UTC | |
Agreed. However, if for some reason your app actually requires the ability to input actual queries (like a SQL tutorial, maybe), you should probably be relying much more on the database's built-in security model. If you give an untrusted user real access to a database that you don't want them altering/destroying, a fragile regex is not going to save you. As an aside for similar efforts, no regex of that length should be a one-liner. At the very least, you should be using the /x switch and commenting what you want to do. Even better would be building the regex in several chunks, so that a syntax error might have a prayer of being found. | [reply] |
| |
|
Re: Preventing injection attacks
by andye (Curate) on Apr 02, 2007 at 15:44 UTC | |
Many people think that, rather than checking for particular things that you don't want, it's better to check that the input is what you do want, and only accept it if it is. For example, instead of saying 'don't accept input with dollar symbols in it', you could say 'only accept input consisting solely of alphanumeric characters and whitespace'. The logic behind this is that, if you try to check for all the possible things that could cause trouble, you're bound to miss some out, perhaps because a particular security problem is only discovered after you write your code - in that situation, there's no way you could have known about it. There are a couple of tools in Perl that can help with this: I can see you've just asked in the Chatterbox, "Why didn't people like my last post?", and the answer, as far as I can tell, is that some Monks are getting impatient with you because, when people answer your questions by telling you about online documentation, you're not going away and reading that documentation for yourself. (Personally I'm a 'no such thing as a stupid question' person, but not everyone is). You could also take a look at the tips in this article, The Help Vampire, a Spotter's Guide, and if you think that you might be doing some of the things they talk about (such as asking the same question multiple times, and not using your own initiative to understand the documentation), then there are some very useful tips for you to follow, on that page. This is all meant in the very friendliest spirit, and I hope that it reads in that way. After all, Perlmonks is here to answer questions, so everyone is absolutely entitled to ask them! Which is lucky for me, otherwise I'd have been stuck many times. :) Do get back to me with any queries about the 'taint mode' and 'placeholders' stuff above.
Hope that helps, and best wishes, (Other relevant nodes: Database access problem, Perl DBI issue) The tips from the 'help vampire' page are:
Now you know. Stop. Of course, it's not just that easy, or nobody would ever be a Help Vampire at all. Before you ask a question in a community, try to find the answer elsewhere. This way you help yourself by stretching your mind and research abilities, and you learn things more thoroughly too. Plus it's good karma. Always try these avenues first: | [reply] [d/l] [select] |
| |
|
Re: Preventing injection attacks
by Bro. Doug (Monk) on Apr 02, 2007 at 16:04 UTC | |
SQL Injection can definately be a thorn in your side. However, most of these problems are eliminated when you use DBI's bind method. DBI does a good job of escaping and db-quoting the things you bind this way. Then you can relax. If you use andye's method for filtering the input and paring away only things you -do- want via your favorite untaint method, you can relax even harder. Peace, monks. Update: wfsp has pointed out that table names shouldn't be bound with bind_params. Turns out he's absolutely correct. It's all documented in perldoc DBI. I've also fixed the code and commented out the garbage. Thanks wfsp.
Bro. Doug :wq
| [reply] [d/l] |
by parv (Parson) on Apr 02, 2007 at 23:07 UTC | |
| [reply] |
|
Re: Preventing injection attacks
by ambrus (Abbot) on Apr 02, 2007 at 17:43 UTC | |
That regexp won't even compile. You have an unescaped slash and an unescaped opening parenthesis in it. But when it does compile, I think it will match too much valid data. Like, what is the branch \s+\s supposed to catch? | [reply] [d/l] |
|
Re: Preventing injection attacks
by robot_tourist (Hermit) on Apr 03, 2007 at 07:51 UTC | |
One good thing that has come out of this discussion is that I understand web security better now. I've developed an internal web app for my department and now that I've got it up and running with just dbh->quote()ing everything possible I think I'll start to bind up my db queries. I have to let my users input backslashes and other potentially dangerous stuff because of the nature of the data. How can you feel when you're made of steel? I am made of steel. I am the Robot Tourist. | [reply] |