Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Mongodb ObjectID as a document ID?

I'm trying to make a board with mongoDB.

I want to assign document ID with ObjectID.

If a user can access to a document page by http://www.example.com/4easdf123123 where "4easdf123123" is a mongoDB ObjectID.

Is there any possible security threat, If I use and show mongo ObjectID in URL and using it as a document id?

And any suggestion with assigning document ID with mongoDB?

like image 205
jwchang Avatar asked Aug 16 '11 01:08

jwchang


People also ask

Why ID is ObjectId in MongoDB?

Every document in the collection has an “_id” field that is used to uniquely identify the document in a particular collection it acts as the primary key for the documents in the collection. “_id” field can be used in any format and the default format is ObjectId of the document.

Is ObjectId a UUID?

A MongoDB ObjectID is 12 bytes in size, is packed for storage, and its parts are organized for performance (i.e. timestamp is stored first, which is a logical ordering criteria). Conversely, a standard UUID is 36 bytes, contains dashes and is typically stored as a string.

Does MongoDB auto generate ID?

MongoDB is a NoSQL database that operates with collections and documents. Each document created on MongoDB has a unique object ID property. So when creating a document without entering an ID, the document will be created with an auto-generated ID.

Is ObjectId deprecated?

ObjectID is deprecated in MongoDB Node.


1 Answers

That doesn't look like a MongoDB ObjectID -- an ObjectID is 12 bytes of binary data, and when rendered as a hexadecimal string (the usual way to use it in a URL) it would be 24 characters long. I assume you're using the official PHP Mongo Driver, in which case the MongoId class's constructor will ignore invalid values and generate a new one for you. In any event, it's best to let the driver generate an ObjectID/MongoId for you, as it will do so in a way that avoids collisions.

As for the safety of using it in your URLs, you should be fine. You should, of course, use the usual precautions about implementing code to ensure that the current user has access to view the object being displayed, etc, but there is no more security risk of using an ObjectID in URL than any other database identifier (string, integer, etc), and often there is less, as the ObjectID has no semantic value (whereas a string like "adminuser" in a URL might convey that that URL relates to a user with elevated privileges).

like image 180
dcrosta Avatar answered Sep 30 '22 04:09

dcrosta