2
\$\begingroup\$

I am creating a function for sanitising user inputs and user inputted data outputs.

Please offer advise on any improvements which could be made:

function cleanse($input) {

    $search = array(
        '@<script[^>]*?>.*?</script>@si',   // Strip out javascript
        '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
        '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
        '@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments
    );

    $output = preg_replace($search, '', $input);
    return $output;
}

function sanitise($input) {
    if (is_array($input)) {
        foreach($input as $var=>$val) {
            $output[$var] = sanitize($val);
        }
    } else {
        $input  = cleanse($input);
        $output = htmlspecialchars($input, ENT_QUOTES,'UTF-8',false);
    }
    return $output;
}
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Personally I tend to work with a white-list of characters.

So instead of sanitizing and removing bad stuf, I simply only accept good stuff.
e.g. [0-9]+ when validating an age field

This approach is less error prone because you don't have to think of all the bad things one can enter. And you know that the data you are serving is actualy what you tink it is (e.g. a number when referencing age).

\$\endgroup\$
2
  • \$\begingroup\$ I'm working with sensitive information and nearly all inputted data is inputted and outputted as strings, all the inputs I have that are numerical use validation to ensure they are numeric for example. In the situation described, am I taking an approach which is not optimal? \$\endgroup\$ Commented May 14, 2014 at 16:25
  • \$\begingroup\$ @danielsmile It's not that it isn't optimal, it makes things easier for you to maintain in the long run if you only have to think about data that is allowed rather than not. \$\endgroup\$ Commented May 14, 2014 at 16:37
0
\$\begingroup\$

Please see discussions on StackOverflow regarding parsing all HTML with regular expressions such as - https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags -- The gist of it being: you can't do it. I would recommend using actual HTML parsing for this purpose, such as HTMLPurifier: http://htmlpurifier.org/

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.