Login Register






Improved email and url sanitation filter_list
Author
Message
Improved email and url sanitation #1
Many people just uses PHP’s native functionality blindly thinking that everything will work smoothly, but that isn’t the case. PHP provides the FILTER_SANITIZE_EMAIL and FILTER_SANITIZE_URL filters to use with the filter functions. The problem here is that they also strip unicode characters, meaning that if you’re sanitizing a unicode url you will destroy it. So to solve this we cannot use the native sanitation, but we need to use preg_replace.

Many people is against the use of regular expressions when it comes to working with emails and urls, but keep in mind, we’re not validating. We’re only stripping away illegal characters, so the pattern is not complex at all.

So how is this enabling unicode? Well, we use \pL which accepts letters from all languages.

Email sanitation:
Code:
$pattern = '/[^\pL\pN!#$%&\'\*\+\-\/\=\?\^\_`\{\|\}\~\@\.\[\]]/u'; $email = preg_replace($pattern, '', 'some@email.com');

URL sanitation:
Code:
$pattern = '/[^\pL\pN$-_.+!*\'\(\)\,\{\}\|\\\\\^\~\[\]`\<\>\#\%\"\;\/\?\:\@\&\=\.]/u'; $url = preg_replace($pattern, '', 'http://www.someurl.com');

The patterns has been tested using the urls found here http://idn.icann.org/#The_example.test_names

I've also made a code snippet for each

Email
Code:
function sanitizeEmail($email) { $pattern = '/[^\pL\pN!#$%&\'\*\+\-\/\=\?\^\_`\{\|\}\~\@\.\[\]]/u'; return preg_replace($pattern, '', $email); }

Url
Code:
function sanitizeUrl($url) { $pattern = '/[^\pL\pN$-_.+!*\'\(\)\,\{\}\|\\\\\^\~\[\]`\<\>\#\%\"\;\/\?\:\@\&\=\.]/u'; return preg_replace($pattern, '', $url); }

Original source: My blog
"SQL Injection-a-holic"

Twitter | Security Sucks | My Blog

Reply

RE: Improved email and url sanitation #2
I may not be much of a PHP user but having those regexes is handy.

Oh, and you also just got yourself another blog subscriber. Wink

Reply

RE: Improved email and url sanitation #3
(06-12-2013, 08:22 AM)soh_cah_toa Wrote: I may not be much of a PHP user but having those regexes is handy.

Oh, and you also just got yourself another blog subscriber. Wink

Yeah, this is not applying to PHP only and unicode in domains is no longer an imaginary thing even though many acts like it.

That's great Smile Glad you like my blog
"SQL Injection-a-holic"

Twitter | Security Sucks | My Blog

Reply