Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 03-23-2006, 09:29 PM   #1 (permalink)
Addict
 
soma's Avatar
 
Location: USA
[php]regex: most effective validation?

Does using regex for all form and URL variables insure the maximum security for a website? I'm about to implement some form/url validation and regex is the most secure thing I am aware of in php...

Any thoughts or advice would be great.

Thanks.
__________________
Having Girl Problems?
soma is offline  
Old 03-24-2006, 06:55 AM   #2 (permalink)
Darth Papa
 
ratbastid's Avatar
 
Location: Yonder
Well, look.... Using regular expressions doesn't magically make your input secure. That would be like saying, I want to make a pie. I have apples. Do apples make a good pie? Like, yes, but there's a whole lot more to it than that.

Anything you could do with a regular expression you could do with a whole bunch of strpos() calls. WAY less efficiently, but... preg_match() is a powerful tool, but it's really just another tool.

There are LOTS of php snippets out there that sanitize inputs. I recommend you google a little bit and see what you find. You really don't want to be rolling your own, when it comes to security. Especially if you're at the stage where you're asking questions like this--no offense, but the question tells me you don't have a huge amount of experience with this. Go find a script written by an expert; it's not smart to take chances with security.
ratbastid is offline  
Old 03-24-2006, 02:20 PM   #3 (permalink)
paranoid
 
Silvy's Avatar
 
Location: The Netherlands
Regular expressions are, like ratbastid said, a very powerful tool. And yes, it can help you sanitize the input.
Main question is: what do you plan on using the input for? Make sure that the input values cannot break any place you're using it at.

At least I'd check for:
- length (make sure everyplace you use the input value, that it is not too big, for example: database)
- type (expecting a number? use is_numeric() to find out wether it is one)
- special characters (This is the most important part. use html_entities() to prevent javascript injection for example. But also look out for SQL injections...)

There are many examples found around the 'net to sanitze input. There are also alot more functions avaible to help you.
__________________
"Do not kill. Do not rape. Do not steal. These are principles which every man of every faith can embrace. "
- Murphy MacManus (Boondock Saints)
Silvy is offline  
Old 03-26-2006, 08:01 AM   #4 (permalink)
Addict
 
soma's Avatar
 
Location: USA
What other funcitons should I be aware of. Right now, I sanitize my variables by sending them through the following homemade function:

Quote:
function make_safe($variable) {
$variable = htmlspecialchars(trim(mysql_escape_string($variable)));
return $variable;
}
And now I'm trying to use regex functions to die(); whatever is running if any unwanted characters show up. I'm also going to make different database users instead of using one super user.

That's about the extent of my PHP security knowledge. I know I'm not experienced in this area but am devoting a lot of time to learn more about it. I know not having a super secure site will cause much trouble down the road.
__________________
Having Girl Problems?
soma is offline  
Old 03-26-2006, 06:43 PM   #5 (permalink)
Crazy
 
I've never written in PHP, but another good security tip is to always use bind variables in your sql statements. I do not know if PHP supports this.
__________________
Even if you stop the clock, it gives the right time twice a day.
Once we get out of the eighties, the nineties are going to make the sixties look like the fifties.
kofspades is offline  
Old 03-26-2006, 08:15 PM   #6 (permalink)
Darth Papa
 
ratbastid's Avatar
 
Location: Yonder
Why die()? Usually die() results in a partial page or a server error message being displayed. I doubt that's what you mean to do. Probably you'd like to put the form back out and ask the user to clean up their input. Maybe if you're super anal and the input you caught was an obvious assualt, you log their IP and don't let them see the form again.
ratbastid is offline  
Old 03-27-2006, 06:20 AM   #7 (permalink)
Addict
 
soma's Avatar
 
Location: USA
Yeah, die(); is a bit inconvenient at times. Whenever I have die(); in a function, what I usually do is have an includes footer.php right afterwards. It works, but I don't think it's too terribly ... great. I'll try what you have suggested. I like the idea of logging the IP of potentially malicious users.
__________________
Having Girl Problems?
soma is offline  
Old 05-01-2006, 08:40 PM   #8 (permalink)
Insane
 
trache's Avatar
 
I think you should make some hard and fast rules about your input. Say, length or only numbers and test your input against them. Then start with your regular expressions.

If you receive any errors, push an error string onto an array. When you go and display your page after the user hits submit and the form starts to process the input, if the array is not empty, display the errors, don't die().

If you are a good webmaster (and we know that you are!), you will fill the partial form out with the users input from the submission, as we know that no one likes to input anything twice. :-)

Log the bad submissions because then you can decide whether or not to add more checks into your code.

Also, it is good to use the (add|strip)slashes when handling data from your database.

#1 rule? Don't trust the user to do anything you ask. That includes adding things like if ($admin == 1) with things like register_globals. I can easily add ?admin=1 to the form and have superuser access to your website. Remember to turn register_globals off and use _GET, _POST etc with the _SERVER["REQUEST_METHOD"] variable.

Check things like referrers (not always accurate or should be taken into consideration).

Put a hidden variable on your form and put that variable into a database. When the user submits the form, make sure that variable is in the database (for a short time). That will cut down on spoofing.
__________________
"You looked at me as if I was eating runny eggs in slow motion." - Gord Downie of The Tragically Hip

Last edited by trache; 05-01-2006 at 08:48 PM..
trache is offline  
 

Tags
effective, phpregex, validation


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 04:04 AM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76