Am I doing everything I can to prevent SQL Exploits with this code?

Discussion in 'PHP' started by Rory M, Aug 11, 2008.

  1. #1
    Hi everyone :D

    I know I am keeping you busy with my questions but I think I am learning so I am going to keep on with it :p

    So, with all the threat of SQL exploits I would like to check with those better at PHP that I am correctly protecting myself in this script.

    Every value that is sent to the database is run through this snippet:

    function db_prepare($value)
    {
    	$magic 			= get_magic_quotes_gpc();
    
    	if($magic == 1)
    	{
    		return $value;
    	}
    	else
    	{
    	addslashes($value);
    	return $value;	
    	}
    	
    }
    
    $variable = "Some potentially evil code";
    $safeVariable = mysql_real_escape(db_prepare($variable));
    
    //Then $safeVariable is sent to the DB
    PHP:
    Am I doing it correctly? I didn't know whether real_escape allowed for Magic Quotes so I decided to be sure.

    Thanks (and +REP for everyone who helps as always) :D
    Rory
     
    Rory M, Aug 11, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    addslashes() isn't safe enough. And mysql_real_escape() isn't a function (unless you defined it). And if magic quotes were enabled using your code above, then you'd end up double escaping the data.

    Take a look at the "best practice" example on this page.

    If magic quotes are enabled, strip them off. And then use mysql_real_escape_string().
     
    nico_swd, Aug 11, 2008 IP
  3. Rory M

    Rory M Peon

    Messages:
    1,020
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Sorry, I was in a rush when I wrote that code (it's not the one in my script where I do actually use mysql_real_escape_string). Looking at your "best practice" example I am a bit confused by this bit.

    
    $query = sprintf("INSERT INTO products (`name`, `description`, `user_id`) VALUES ('%s', '%s', %d)",
                        mysql_real_escape_string($product_name, $link),
                        mysql_real_escape_string($product_description, $link),
                        $_POST['user_id']);
    PHP:
    I get that the bit's with % in front of them are then defined afterwards. Why are 2 %s and 1 %d though?
     
    Rory M, Aug 11, 2008 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    That's explained at the sprintf() manual page.

    However, I wasn't referring to this part anyway. I meant this part:
    
    if(get_magic_quotes_gpc()) {
                $product_name        = stripslashes($_POST['product_name']);
                $product_description = stripslashes($_POST['product_description']);
            } else {
                $product_name        = $_POST['product_name'];
                $product_description = $_POST['product_description'];
            }
    
    PHP:
    ... don't rely on magic quotes, as they're not safe. If they've been added by PHP, strip them off and then apply mysql_real_escape_string().
     
    nico_swd, Aug 11, 2008 IP
    Rory M likes this.
  5. Rory M

    Rory M Peon

    Messages:
    1,020
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Okay :D

    Thanks for putting up with me, you have earned your Rep :)
     
    Rory M, Aug 11, 2008 IP
  6. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #6
    LogicFlux, Aug 11, 2008 IP
  7. Rory M

    Rory M Peon

    Messages:
    1,020
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I just noticed on the manual page that MQ is being removed in PHP 6.0. When is that scheduled anyway?
     
    Rory M, Aug 12, 2008 IP
  8. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I don't know. The developers knew about the problem that magic quotes causes years ago. I think it was just left in PHP5 for backwards compatibility. It's great news that PHP 6 is dumping it.
     
    LogicFlux, Aug 12, 2008 IP