Using Mongodb ObjectID as a document ID?

梦想的初衷 提交于 2019-12-04 01:03:40

问题


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?


回答1:


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).



来源:https://stackoverflow.com/questions/7072680/using-mongodb-objectid-as-a-document-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!