views:

42

answers:

2

Do I need to escape my object data if I'm serializing for mysql injection?

ie:

class Object
{
   public $description;
}

$obj = new Object();
$obj->description = mysql_real_escape_string("this is my crazy string with lot's of bad // characters");

$data = serialize($obj); // <-- $data will be stored in DB

or will this suffice:

class Object
{
   public $description;
}

$obj = new Object();
$obj->description = "this is my crazy string with lot's of bad // characters";

$data = serialize($obj);
+2  A: 

Run mysql_real_escape_string() after you've serialized. That's the string you are going to put in the database after all.

Daniel Egeberg
makes perfect sense
Jascha
Always run mysql_real_escape_string on input, or better yet, use prepared statements. The more of a habit you make it, the better.
Mike Sherov
+3  A: 

Yes, you must escape it (or use prepared statements).

<?php
$obj = (object) array("--'--'" => "--'--");
var_dump(serialize($obj));

yields

string(44) "O:8:"stdClass":1:{s:6:"--'--'";s:5:"--'--";}"

As you can see, there's no escaping.

On a side note, you should use the mysqli extension for new code, not the mysql extension.

Artefacto