Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a bad idea to escape HTML before inserting into a database instead of upon output?

Tags:

I've been working on a system which doesn't allow HTML formatting. The method I currently use is to escape HTML entities before they get inserted into the database. I've been told that I should insert the raw text into the database, and escape HTML entities on output.

Other similar questions here I've seen look like for cases where HTML can still be used for formatting, so I'm asking for a case where HTML wouldn't be used at all.

like image 384
unrelativity Avatar asked Sep 06 '10 00:09

unrelativity


2 Answers

you will also restrict yourself when performing the escaping before inserting into your db. let's say you decide to not use HTML as output, but JSON, plaintext, etc.

if you have stored escaped html in your db, you would first have to 'unescape' the value stored in the db, just to re-escape it again into a different format.

also see this perfect owasp article on xss prevention

like image 147
knittl Avatar answered Oct 01 '22 18:10

knittl


Yes, because at some stage you'll want access to the original input entered. This is because...

  • You never know how you want to display it - in JSON, in HTML, as an SMS?
  • You may need to show it back to the user as is.

I do see your point about never wanting HTML entered. What are you using to strip HTML tags? If it a regex, then look out for confused users who might type something like this...

3<4 :-> 

They'll only get the 3 if it is a regex.

like image 42
alex Avatar answered Oct 01 '22 20:10

alex