views:

54

answers:

3

I know there is no harm in adding it either way but I'm curious...

If I was to use htmlentities(); with ENT_QUOTES and then mysql_real_escape_string(); the variable before entering it into the Database, then just use html_entity_decode(); along with stripslashes(); to display the information...

Would this still be safe and secure?

+3  A: 

You don't need to use htmlentities before storing data in the database. In fact, it makes things easier later if you don't. Only use htmlentities on strings as you echo them in HTML output (whether you fetched the string from a database or from some other source).

You don't need to apply stripslashes to data after you fetch it from the database. The database has not stored the extra escaping characters -- unless you applied double-escaping by mistake.

Here's the right sequence:

  1. Get data from a form

    $input = $_GET["input"];
    
  2. Apply escaping once.

    $quoted_input = "'" . mysql_real_escape_string($input) . "'";
    
  3. Insert it into the database

    $sql = "INSERT INTO MyTable (column1) VALUES ($quoted_input)";
    $success = mysql_query($sql);
    
  4. Later fetch it from the database

    $sql = "SELECT column1 FROM MyTable";
    $result = mysql_query($sql);
    $row = mysql_fetch_assoc($result);
    $data = $row["column1"];
    
  5. Apply htmlentities once as you output.

    echo htmlentities($data);
    
Bill Karwin
So taking the approach of just using mysql_real_escape_string, then entering to the database, that would be safe?What about XSS too? I thought htmlentities would help protect against that?
Joe
not for xss attacks. I had a demo with a form and users added html code with a link to a xss javascript file that would run malicious code. I used htmlentities() so the html would be printed and not processed by the browser. of course if you allow html input regex might be better then htmlentities()
krike
`htmlentities` has nothing to do with inserting data into the database. It's for display purposes, for data moving from the database through your app onto the screen.
meagar
So basically, htmlentities and mysql_real_escape_string would stop from injection and XSS JS files? But PHP regex is just extra security to really limit people to what you want them to enter? And JS Regex would just be merely for easier use/navigation on the client side of things? (So their not being directed from one page to another until they get it all right)
Joe
Yes, that's a good understanding. I tend to use regexes to validate that the input is in the right format, not for security.
Bill Karwin
A: 

are you asking if you still need regex as form validation next to all those functions?

if that is what you are asking then in my opinion yes, you can never be safe enough. I've just written a validation class with functions that clean up the code and other functions with regex when I need a specific input.

krike
+1  A: 

Maybe you can answer the question on your own if you know what these functions are intended to be used for:

If you just want to protect you from SQL injections, use mysql_real_escape_string for data that is used in MySQL queries. You could also use prepared statements or parameterized query builder (see SQL Syntax for Prepared Statements, PDO – Prepared Statements und Stored Procedures, MySQLi::prepare, et al.).

Gumbo