使用 Mongodb ObjectID 作为文档 ID?
我正在尝试用 mongoDB 制作一个板。
我想用 ObjectID 分配文档 ID。
如果用户可以通过 http://www.example.com/4easdf123123 访问文档页面,其中 " 4easdf123123”是一个mongoDB对象ID。
如果我在 URL 中使用并显示 mongo ObjectID 并将其用作文档 ID,是否存在任何可能的安全威胁?
对于使用 mongoDB 分配文档 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来不像 MongoDB ObjectID —— ObjectID 是 12 字节的二进制数据,当呈现为十六进制字符串(在 URL 中使用它的常用方法)时,它将是 24 个字符长。我假设您使用的是 官方 PHP Mongo 驱动程序,在这种情况下
MongoId
类的构造函数将忽略无效值并为您生成一个新值。无论如何,最好让驱动程序为您生成一个 ObjectID/MongoId
,因为它会以避免冲突的方式生成。至于在 URL 中使用它的安全性,应该没问题。当然,您应该使用有关实现代码的常见预防措施来确保当前用户有权查看正在显示的对象等,但在 URL 中使用 ObjectID 并不比任何其他数据库标识符(字符串、整数等),而且通常更少,因为 ObjectID 没有语义值(而 URL 中的“adminuser”等字符串可能表明该 URL 与具有提升权限的用户相关)。
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).