Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct/safest way to escape input in a forum?

I am creating a forum software using php and mysql backend, and want to know what is the most secure way to escape user input for forum posts.

I know about htmlentities() and strip_tags() and htmlspecialchars() and mysql_real_escape_string(), and even javascript's escape() but I don't know which to use and where.

What would be the safest way to process these three different types of input (by process, I mean get, save in a database, and display):

  1. A title of a post (which will also be the basis of the URL permalink).
  2. The content of a forum post limited to basic text input.
  3. The content of a forum post which allows html.

I would appreciate an answer that tells me how many of these escape functions I need to use in combination and why. Thanks!

like image 218
chris Avatar asked Aug 06 '09 21:08

chris


Video Answer


2 Answers

When generating HTLM output (like you're doing to get data into the form's fields when someone is trying to edit a post, or if you need to re-display the form because the user forgot one field, for instance), you'd probably use htmlspecialchars() : it will escape <, >, ", ', and & -- depending on the options you give it.

strip_tags will remove tags if user has entered some -- and you generally don't want something the user typed to just disappear ;-)
At least, not for the "content" field :-)


Once you've got what the user did input in the form (ie, when the form has been submitted), you need to escape it before sending it to the DB.
That's where functions like mysqli_real_escape_string become useful : they escape data for SQL

You might also want to take a look at prepared statements, which might help you a bit ;-)
with mysqli - and with PDO

You should not use anything like addslashes : the escaping it does doesn't depend on the Database engine ; it is better/safer to use a function that fits the engine (MySQL, PostGreSQL, ...) you are working with : it'll know precisely what to escape, and how.


Finally, to display the data inside a page :

  • for fields that must not contain HTML, you should use htmlspecialchars() : if the user did input HTML tags, those will be displayed as-is, and not injected as HTML.
  • for fields that can contain HTML... This is a bit trickier : you will probably only want to allow a few tags, and strip_tags (which can do that) is not really up to the task (it will let attributes of the allowed tags)
    • You might want to take a look at a tool called HTMLPUrifier : it will allow you to specify which tags and attributes should be allowed -- and it generates valid HTML, which is always nice ^^
    • This might take some time to compute, and you probably don't want to re-generate that HTML each time is has to be displayed ; so you can think about storing it in the database (either only keeping that clean HTML, or keeping both it and the not-clean one, in two separate fields -- might be useful to allow people editing their posts ? )


Those are only a few pointers... hope they help you :-)
Don't hesitate to ask if you have more precise questions !

like image 132
Pascal MARTIN Avatar answered Oct 04 '22 00:10

Pascal MARTIN


mysql_real_escape_string() escapes everything you need to put in a mysql database. But you should use prepared statements (in mysqli) instead, because they're cleaner and do any escaping automatically.

Anything else can be done with htmlspecialchars() to remove HTML from the input and urlencode() to put things in a format for URL's.

like image 32
rpjohnst Avatar answered Oct 04 '22 01:10

rpjohnst