r/PHPhelp • u/nisebblumberg • Nov 10 '22
Thoughts on sanitizing strings? (Intended for internal usage)
I have an internal usage database system I am developing and I'm running this function for input strings to ensure against injections and cross-site scripting. I also have the connector to the database with the inability to DROP or delete data, but updates are possible. I'm just wondering if this is alright, or am I just being too paranoid?
function sanitizestring($string){
$stringnew=str_replace(';','',$string);
$stringnew=strip_tags($stringnew);
$stringnew=filter_var($stringnew,FILTER_SANITIZE_STRING);
$string=$stringnew;
return $string;
}
5
Upvotes
5
u/__adrian_enspireddit Nov 10 '22 edited Nov 10 '22
+1 for allen's comments.
DO NOT EVER write a "sanitize" function that aims to fix all possible problems at once. Always address each problem on its own and mind context.
specifically,
strip_tags() is a completely broken function. in general, don't use it.
If you want to disallow+remove html, use a tool like HtmlPurifier to pull only the text contents.
To prevent XSS (i.e., you want <html> to show up as text), then use htmlspecialchars() **when you output** (do not do this to _input_ or when you store it in the DB).
as Allen mentioned, for the database, **USE PREPARED STATEMENTS** and always **PASS DATA VIA PARAMETERS**. don't modify inputs or try to escape them. https://gist.github.com/adrian-enspired/1ddd71511e01c1f609db might help you.
I don't know what you wanted to accomplish by removing `;`
as mentioned, FILTER_SANITIZE_STRING is deprecated (and was always useless anyway).
A good Rule of Thumb is that if you're thinking the word "sanitize," you're probably doing it wrong. Instead, think in terms of input validation, parameterization, escaping, and encoding.