Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Security concerns while using MongoDB PHP driver

I have experiences with securing sql injections on MYSQL, but what should I be careful on MongoDB using php driver? In most of the pages I get data via GET/POST and searching/inserting the system. I search via UDID / other fields, and can insert any string value. Also I get user's cookies via javascript.

  1. So when GET/POST, I'm adding to each variable htmlentities function?

  2. What would replace mysql_real_escape_string? Should I use it?

So, for example, when doing

$download = array( 'url' => $_GET['url'] );

$downloads->insert($download); 

Is this OK?

  1. Is there a way to check if a string is really a UID?

  2. Any think else I should be aware when using MongoDB and PHP? I do get my cookies using javascript, and searching in my DB using the cookies. What about that?

like image 689
Eli_Rozen Avatar asked Jan 21 '12 19:01

Eli_Rozen


2 Answers

So when GET/POST, I'm adding to each variable htmlentities function?

No need to. You should however, use htmlentities when outputting user-generated data to a browser, to prevent XSS attacks.

What would replace mysql_real_escape_string? Should I use it?

You shouldn't use mysql_real_escape_string as it's for MySQL. Nothing replaces this on MongoDB, the driver takes care of escaping the data for you.

Is there a way to check if a string is really a UID?

The only way is to validate it is to query MongoDB with that string and check if it exists.

You can however, validate if the format is correct:

$id = '4f1b166d4931b15415000000';
$a = new MongoId($id);
var_dump($a->{'$id'} == $id); // true

$id = 'foo';
$a = new MongoId($id);
var_dump($a->{'$id'} == $id); // false

Any think else I should be aware when using MongoDB and PHP? I do get my cookies using javascript, and searching in my DB using the cookies. What about that?

Not much. As for any web application, you are very discouraged from storing sensitive data in cookies, such as user identifiers, passwords, etc. as they can easily be tempered with and used to access parts of your application that should be restricted, or impersonate other users.

like image 140
netcoder Avatar answered Oct 26 '22 15:10

netcoder


Btw i think something is missed for example

    yourdomain.com/login?username=admin&passwd[$ne]=1

In Sql this looks like this

    SELECT * FROM collection
    WHERE username="admin",
    AND passwd!=1

The way i know is valid to escape this sutiations is to know what type of data you expect and cast it. Hope the answer was useful

like image 24
gprusiiski Avatar answered Oct 26 '22 14:10

gprusiiski