【发布时间】:2014-12-29 01:37:02
【问题描述】:
文档建议使用 Meteor.methods 创建安全的方法。
用户是否可以在浏览器的控制台中编写代码,以允许用户绕过安全性并将任意数据发送到服务器数据库 (MongoDB)?如果没有,为什么不呢?
【问题讨论】:
标签: meteor
文档建议使用 Meteor.methods 创建安全的方法。
用户是否可以在浏览器的控制台中编写代码,以允许用户绕过安全性并将任意数据发送到服务器数据库 (MongoDB)?如果没有,为什么不呢?
【问题讨论】:
标签: meteor
是的,但前提是该方法无法检查其输入和上下文。幸运的是,meteor 提供了使这项任务非常简单的工具。让我们看一个我用来回答this question 的示例方法。 sendMessage 是一种让用户在聊天室中发送消息的方法:
Meteor.methods({
sendMessage: function(message, roomId) {
check(message, String);
check(roomId, String);
if (!this.user)
throw new Meteor.Error(401, 'You must be logged in.');
if (_.isEmpty(message))
throw new Meteor.Error(403, 'Message must not be empty.');
var room = Rooms.findOne(roomId);
if (!room)
throw new Meteor.Error(404, 'Room not found.');
if (!_.contains(room.members, this.userId))
throw new Meteor.Error(403, 'You are not in the room.');
return Messages.insert({
userId: this.userId,
roomId: roomId,
message: message
});
}
});
以下是验证:
将此与 sendMessage 的幼稚实现进行对比:
Meteor.methods({
sendMessage: function(message, roomId) {
return Messages.insert({
userId: this.userId,
roomId: roomId,
message: message
});
}
});
在这里,任何连接的客户端都可以打开终端并开始将消息注入任何聊天室。更糟糕的是,message 可能是一个对象并对其他客户端造成各种意想不到的后果。
没有免费的安全午餐 - 您应该验证一切并假设最坏的情况。但是,如果您努力,您实际上可以生成高度安全的方法。
我强烈建议您查看 Emily Stark 的 Meteor Meets Mallory 演讲,其中她更详细地介绍了这些要点。
【讨论】: